Drupal: assign block to a specific content type - drupal

I made a customized template called node-mynode.tpl.php
Whenever a node of type mynode is requested, then node-mynode.tpl.php
is automatically used.
However, now user wants to see a specific menu block in this case.
Question:
How can I assign a block to a specific content type?
Hint: I have started to look at URL aliases with Pathauto.
I suspect one solution may lie in this direction.

In Drupal 6, you can configure the visibility settings of blocks. Click on the 'configure' link next to your block in the administrator backend and follow these steps -
1) Select the 'Show if the following PHP code returns TRUE (PHP-mode, experts only)' option under the 'Page specific visibility settings' tab.
2) Enter the following PHP code which checks the node type of the current node and returns TRUE accordingly -
<?php
if( arg(0) != 'node' || !is_numeric(arg(1)) )
{ return FALSE;
}
//load a fully-populated Drupal node object
$temp_node = node_load(arg(1));
if( $temp_node->type == 'mynode' ) //check the node type
{ return TRUE; //display block
}
?>
This should work....

you can use the context module

Give all of your mynode type nodes an automatic alias that starts with /mynode and use the page specific visibility settings for the block, showing only on the pages that start with /mynode/*.

Related

Get $node variable in html.tpl.php - Drupal 7

I'm trying to allow users to update head titles and meta descriptions for each page. I thought that an easy way to achieve this would be to add a field to the 'Basic page' content type for the page title, then check if that field is not empty in html.tpl.php and if it is not, override $head_title with this user-defined value.
However, it appears that the $node variable is not available in html.tpl.php. Can anyone suggest a way for me to make this data available in this template file, or alternatively, alter $head_title before it is sent to html.tpl.php? Thanks for reading.
Taken in part from this thread that I found: http://drupal.org/node/1041768...
In your template.php, you can do the following:
function yourtheme_preprocess_html(&$variables) {
// If on an individual node page, add the node type to body classes.
if ($node = menu_get_object()) {
$variables['head_title'] = $node-> // find your cck field here
}
}
Bit messy, but would work:
if(arg(0) == 'node' && !empty(arg(1))) {
$node = node_load(arg(1));
}
However, you might prefer http://drupal.org/project/metatags_quick (an interrim module until the full http://drupal.org/project/metatags is finished).

How to setup drupal for content relationship?

Using Drupal 6.x I have created two content types: Person and Event. Event has a custom field called Attendees (of type: Node Reference; unlimited number of values to person). When viewing a specific person how does one show all their events?
I have created a view (Personal Events) and added a block display. I enabled the block to show for content type Person. How should the view be defined? Or is there a better way?
Modules installed: CCK; Node Relationships; Views
I think one of these modules might be of help to you:
Reverse Node Reference
NodeReferrer
I have an answer to my own question. However, there maybe better answers... I can only hope.
Created content block (Personal Events)
Added this code to the body of the block. This code passes the node id argument to a view
<?php
if ( arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2) ) {
$node = node_load(arg(1));
$args = array($node->nid );
$view = views_get_view('PersonalEvents');
print $view->preview('default', $args);
}
?>
Added this code to the Pages of the block [by selecting: Show if the following PHP code returns TRUE (PHP-mode, experts only)]... this drives the block to only appear person content.
<?php
//Read URL
$path=$_GET['q'];
//If URL is node page
if ( strpos($path,'node')===0){
//Parse URL to get nid
$links=explode("/",$_GET['q']);
$nid=$links[1];
//Load node
$node=node_load($nid);
//Display block only if node is of certain content type
if($node->type=='person'){
return TRUE;
}
}
return FALSE;
?>
Then created view with:
Style: Table
Relationship Content: Attendees (field_attendees); requires this relationship (checked); and Delta set to ALL.
Argument: Node: Nid; Relationship: Attendees; Hide view / Page not found (404) [selected]
Fields... simply selected Node Title and Date (for now)
Filter: Node Type = Event
Anyone have a better way?

Drupal location gmap node map not showing up on the node page

I am using gmap and location modules to support my content type's location.
I have configured those modules (Map API key, etc...) so when I edit / add new node I get pretty google map to choose/set lattitude jus by mouse pointing.
I can put also address, etc..
But when I view my node map is not showing up. I can only see address fields.
Why it is so confusing?
I have check display fields settings in admin/content/types settings page and all fields are set to be visible.
What am I missing here? Why map is not simply visible as the same as in the node edit page?
The standard theming doesn't include an actual map, but it's pretty easy to do. All you need to use to create the map is the gmap_simple_map function. I've done this in the location.tpl.php in the past as I usually want to override that anything. So I add something like this in the bottom of it to include the map:
<?php
// "Geo" microformat, see http://microformats.org/wiki/geo
if ($latitude && $longitude) {
// Assume that 0, 0 is invalid.
if ($latitude != 0 || $longitude != 0) {
$marker = 'Whatever you want the marker to be.';
print gmap_simple_map($latitude, $longitude, '', $marker, 'default');
}
}
?>
Please see this link: GMap, Location Module and Views
Here are the steps you need to take:
Install view.module
Set view.module configuration
You can set the map as a page or block, and set the block show up in content area
Set the block will show up in which section.

drupal views block arguments

I currently have a view (Drupal 6 using Views2) that properly aggregates a custom content type (videos) and filters them for a page display. When I create a block display, it previews the results in live preview just great, but when i go to the page expecting to see the block it doesn't appear.
I'm fairly certain the argument I'm attempting to pass it fails because when I select "Display all results" for "Action to take if argument does not validate:" the block shows up on the page just fine.
Any advice definitely appreciated.
you can use PHP code in the 'provide default argument' section. assuming you're using a menu callback to apply the argument you can do this:
<?php
if(args(0) == 'your-menu-path' && args(1) != ''){
return args(1)
}
?>
make sure you pass a default argument to 'views-embed-view()' if you're using it to place the view on your page otherwise it won't show up at all.
In Drupal 7, to figure out the node you're on, follow corneliusk's answer about making it your default argument. However, do NOT include the php tags, and it's just "arg" not "args" for d7. Ex:
if(arg(0) == 'node' && arg(1) != ''){
return arg(1);
}
else {
return "";
}

Hide link to a Views' view if the view is empty

I have a Drupal 6.14 site with Views module. I have a view and on the primary links I put a link to the view.
There is a way to hide the link in the primary menu only if the view is empty?
You could probably do this either via a theme or module implementation of preprocess_page (THEMENAME_preprocess_page(&$vars) or MODULENAME_preprocess_page(&$vars)), but mac above is correct in that views are not known to be empty or not until they are run, so there will be a performance hit.
Within the function, you should have access to the structured primary links array, so you can run the view:
$view = views_get_view('view_name');
// Swap out 'default' for a different display as needed. Also, $args are arguments, and can be left out if not applicable.
$output = $view->preview('default', $args);
if (empty($view->result)) {
// The view has no results, alter the primary links here to remove the link in question.
}
I am ready to be contradicted any moment as I never implemented anything like that, however I am under the impression that since views are essentially queries against the DB, you can't actually know if a view is empty until you actually invoke it.
Consider that - given you are speaking about primary links (shown on nearly every page of your site) this might be a serious performance hit, depending on the complexity of the view and on its "cacheability".
You should also consider whether the content of that view can be changed by other users browsing the site at the same time that "our" user: should the view become populated after "our" user has loaded the page, "our" user won't ever know.
As on how to achieve what you want, please see the accepted answer.
HTH!
I override views_embed_view() to only provide output if there is content, and then call my override from the theme layer:
function mymodule_embed_view($name, $display_id = 'default') {
// handle any add'l args (this hook supports optional params)
$args = func_get_args();
array_shift($args);
if (count($args)) {
array_shift($args);
}
$view = views_get_view($name);
$output = $view->preview($display,$args);
if ($view->result) {
return $output;
}
}
Then in the template file:
<?php
$view = mymodule_embed_view('view_name');
if (strlen($view) > 0) {
print $view;
}
?>

Resources