Things I can never remember but always need...
...That's actually the name of a note I have saved on my computer. I'm not the most technically savvy person, nor do I have a great memory. Being that doing certain things in Drupal aren't always easy (or easy to remember), I usually have to write things down so I can do it again later. So with my new fancy (but the same) Drupal 7 site, I decided to share my notes about the most common things I do in Drupal that I always forget how to do!
The L Function l();
This is one of the most basic functions used by themers in Drupal and while the basic syntax is quite easy to remember, adding classes, alt attributes, and images aren't as easy to remember. To make just a regular link you need text (always being run through the T function) and a path.
<?php
l(t('This is my link'), 'path');
?>Cool, but what if you want to have an image clickable, with a class of 'image' and maybe a rel for such things as lightbox. This becomes confusing because the first 2 arguments in the L function are just written right into the function separated by a comma, whereas the rest of the data for the link needs to be nested in arrays. First if you want to make an image clickable, you need the themed image, path, and to tell drupal to render the html instead of just printing the html. This is where it gets slightly more complicated because you're usually passing variables you've made into the L function. Then you also need another array of all attributes for the link.
<?php
$image = theme('imagecache', 'preset', 'imagepath');
l($image, 'path', array (
'html' =>'true',
'attributes' => array (
'rel' => 'lightbox',
'class' => 'image'
)
)
);
?>In Drupal 7 the L function is basically the same, but when using images I usually do this with theme_image_formatter()
<?php
theme_image_formatter (
array (
'item' => 'imagepath',
'image_style' => 'preset',
'path' => 'path',
'options' => array (
'html' => TRUE,
'attributes' => array (
'rel' => 'lightbox',
'class' => 'image'
)
)
)
);
?>Printing the node body
A lot of the time in template files you don't want to just print $content, sometimes there are other fields and you just want to be able to print the body without having to edit the node display settings. In Drupal 6 this is easy but not very straight forward.
<?php print $node->content['body']; ?>In Drupal 7 this is even easier:
<?php print render($content['body']); ?>A region within a node
This is a very common need within Drupal themes and is something that took me quite a bit to remember. First thing you need to do is declare the region in your theme's .info file, just like you would declare any other region.
region[node_region] = Region within nodesFrom there you will need to open up your template.php file and locate the preprocess_node() function. In here you want to create a variable to print out in your template.
<?php
$vars['node_region'] = theme('blocks', 'node_region');
?>Then all you need to do is open your node.tpl.php file, which you'll need to create if it doesn't exist, and add some logic and html:
<?php if ($node_region): ?>
<div class="node_region">
<?php print $node_region; ?>
</div>
<?php endif; ?>In Drupal 7 this is done essentially the same way. Add the region to your .info file, create the variable in preprocess_node, and print the variable in node.tpl.php. Only difference is when creating the variable in preprocess_node you use block_get_blocks_by_region()
<?php
$vars['node_region'] = block_get_blocks_by_region('node_region');
?>Then when printing in the template you need to wrap in a render function:
<?php if ($node_region): ?>
<div class="node_region">
<?php print render($node_region); ?>
</div>
<?php endif; ?>Block visibility based on node type or taxonomy term
The block visibility settings can sometimes not be quite enough to get the job done. Using the php code section you can do a lot but you are limited by your knowledge of php. I am not the best at it so I have in my notes how to show a block based on node and by taxonomy. To show based on node type is pretty easy:
<?php
if ($node->type == 'node_type') {
return TRUE;
}
?>If you want to show a block based on taxonomy term(s) that requires knowing the term id(s) and loading the node object so you can access its taxonomy.
<?php
$term_id = array(20);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
foreach ($node->taxonomy as $term) {
if (in_array($term->tid, $term_id)) {
return TRUE;
}
}
}
?>Here we are creating an array of term id's that we want to search for with $term_id. Then the first conditional statement is checking to see if we are dealing with a node page and if we are then loading the node object using its id from the url. Note that even if you are using a url alias this will still work. Following that we do a for each to see if any of the terms in our array are in the node taxonomy array, and if they are it returns true and shows your block.
In Drupal 7 you have the option right from visibility settings to show/hide based on node type. As far as by term, I haven't actually figured that out yet.
Adding a view to a template
Printing a view in a template is one of the most useful snippets I could probably share. Its really helpful to add a view in a template in order to send arguments to it like the node id or term ids. Lets say you want to add a view to a node and pass in the node id as an argument, check if it has content and then print in the template. To start you need to add the following to your preprocess_node() function:
<?php
$view = views_get_view('my_view_name');
$view_display = $view->preview('default', array($vars['node']->nid));
$view_result = $view->result;
if ($view_result != NULL) {
$vars['my_view'] = $view_display;
}
?>If you don't follow reading the code, we're first grabbing the view and assigning it to a variable. Then we preview the view using the display (default, block_1 etc) and node id. We then assign a variable to the result, which will allow us to check if it has content. Then assign a theme variable to the view preview. The next step is to print this in the node template, one thing to note is that when embedding a view in a template the title field does not get printed. You could create a variable for the view title and print that where necessary.
<?php if($my_view): ?>
<div class="my_view">
<h3><?php print t('my view'); ?></h3><!-- or view title variable -->
<?php print $my_view; ?>
</div>
<?php endif; ?>In Drupal 7 I haven't actually attempted this yet, but I assume the process would stay essentially the same as the views_get_view() function has remained the same.
That's all I've got for now! Hope you can enjoy and learn something. :)
Hello, my name is Amanda Rodriguez and I am a Web Designer and Tattoo Artist living in Brooklyn, NY. I graduated from RISD in 2005 with a BFA in Furniture Design. This site is meant to display my work in the several fields I have pursued thus far in my life. I am very passionate about the things that I do and I am constantly striving to be better. I am always looking for someone new to tattoo... so 
Comments
This is a fantastic list! Very well-done, and extremely handy. I also have a similar text file with common tasks, but now I'm just going to bookmark this page. :D
Great post. I too, ALWAYS forget how
l()is supposed to work and I'm coming up on five years of Drupal development. Glad to know I'm not the only one.I have a quibble with the Views advice. You can accomplish the same thing using the `views_embed_view('view_name', 'display_name')` function, which handles all of Views rendering steps (great example of object oriented encapsulation ... NOT!). In your example you could use `$my_view = views_embed_view('my_view_name', 'default')' to get the rendered output of your View. I don't know of any official documentation explaining this, though some Drupal.org and blog posts demonstrate the technique.
Hey, great post! I love some of your shortcuts, and find myself using them all the time too, but trying to remember the print render($node_region) one for D7 is especially hard! :)
A word of note? Your snippet for
theme_image_formatter (
array (
'item' => 'imagepath',
));
might be better written as
theme_image_formatter (
array (
'item' => 'path/to/image',
));
In order to remind people that it's an actual file path, and not a string or the name of some other module. :)
Cheers!
Interesting stuff, haven't jumped into D7 theming just yet. Thanks for taking the time to write this up.
Great post I also forget these. I have bookmarked for easy access. D7 is fun but taking a little getting used to.
Your site is great Amanda! You seem to be close to an answer I have been struggling with. I too want to use Drupal to showcase my work and projects and am interested in showing off images I upload to the site. The problem is I want to add images to a body inline with the media module that I have already been loaded and have these images be linkable to the source node. The functionality I want is something close to what wikipedia does, the main article has images and by clicking on the image you are brought to the node containing the image that has a bunch of great meta data.
If that doesn't make any sense I apologize I have trouble speaking Drupal as I'm a newbie.
If you have time I would appreciate any advice you can give.
Very handy list! Drupal web designers and developers owe you one. Great content found from a search...just can't remember the search term :)
Add new comment