Skip to main content

Working with Drupal 7

Drupal 7 is the last major release of the Drupal CMS and is will be supported by the Drupal community until Drupal 9 gets released. Drupal 7 is similar to previous versions of Drupal using a system of hooks to allow us to do much of the heavy lifting and to seamlessly tie in to the Drupal way of doing things. Take a look at some of our Drupal 7 articles below...

Read some of our articles about Drupal 7

Render a block programmatically

22nd Mar 2012

This is a real quick one, but so useful! We often want to render a block within content, perhaps as part of a node (maybe in hook_node_view, and then made configurable like a field), but there's no obvious way to do this correctly for any block. Drupal normally renders its blocks per region, so there is no single function to embed a block. I came across this really simple solution by Damien Tournoud in a Drupal core issue, which I feel deserves more exposure:


$block = block_load($module, $delta);
$render_array = _block_get_renderable_array(_block_render_blocks(array($block)));
$output = render($render_array);

That's just three...

Read more

Add stuff to a node and configure it like fields

28th Feb 2012

UPDATE: The Extra Field and Extra Field Settings Provider modules facilitate doing this sort of thing in Drupal 8. Read on for older Drupal sites, and for the concepts behind this.

We often want to add things to the content of a node or any other entity in Drupal 7 using hook_node_view(), hook_node_view_alter() or a similar hook in a custom module. This could be anything from a custom social media link, a field rendered in a custom way, additional author information or virtually anything else.

The rendered node with our pseudo-field

Here's the code used to add that Facebook like button:


/**
 * Implements...
Read more

Rendering Drupal 7 fields (the right way)

Stephen Tweeddale
7th Sep 2011

Drupal 7 brought us Entities, and with them the powerful Field API for 'storing, loading, editing, and rendering field data.' attached to them. If you're managing everything through 'manage fields' and 'manage display' tabs of your content type, then every part of that process is rather wonderfully taken care of for you.

We often, however, come across the need to render a field outside the context of it's entity. A common example might include rendering a node's author in a sidebar block. Sure, modules like Panels and CCK Blocks will do this for you, but doing it manually is actually not that hard.

Read more

Drupal node load references : Drupal 7

12th Jul 2011

In Drupal 6, if you called node_load then what you'd be returned would be your own copy of the node to change and modify as you pleased, but in Drupal 7 this was changed. By default, what you actually get back from a node_load is essentially a reference to a global singleton for that node. This has the interesting side effect of meaning that if you change anything in the node object, you are probably changing everyone else's copies of that node too. This is explained in this issue: http://drupal.org/node/154859

So, why should you care? Well, in the right circumstances...

Read more

Multiple conditions for dynamic forms in Drupal 7

5th Jul 2011

Here's a quick follow-up to my original post on Dynamic forms in Drupal 7, as a reply to Wappie08, who asked me about combining conditions in the #states array to add increased control over the display of your form elements. The question:

Hi James Williams, I read your blog post about d7 & #states in the FAPI which is really cool! One problem is that the information is also listed in the drupal.org example module, I was missing one important extra hint: how can you make an IF statement?

I mean:
IF field_1 is '1' or '2'...

Read more

Setting default drupal theme during installation : programatically install a drupal theme

25th Mar 2011

Here's a quick post that will be a reminder for us as much as anyone else! Setting the default theme during installation using an installation profile is surprisingly hard in Drupal 6, and easier though not obvious in Drupal 7. In Drupal 6, we used the wonderful Install Profile API module, which allowed us to do it in just a few lines in an install task:


  install_enable_theme(array('my_theme', 'garland', 'rubik'));
  install_default_theme('my_theme');
  install_admin_theme('rubik');

In Drupal 7, here's the code I use in my install task:


  // Any themes without keys here will get numeric keys and so will be enabled,
  //...
Read more

Apache Solr boost Drupal search results at query-time

11th Mar 2011

Boosting terms in Solr search results produced by the Apache Solr Search module that integrates Solr with Drupal is something we had to do for a project recently. If a user has come to our website from a search engine, we can pick up the terms that they had originally searched for - and then boost any documents containing those terms in our own search pages, regardless of what they search for on our pages. (So for example, a user searches Google for 'Ski Holidays', comes to our site, and anything ski-related items should be more prominent than they would...

Read more

Creating new field formatters in Drupal 7

3rd Feb 2011

Creating extra field formatters in Drupal 7 is fairly simple, but it can take some poking around in the new Fields code. So here is some sample code to make it easier for you! You can add formats to your own fields -- or existing fields that are defined by other Drupal modules. This is the case for the following example - a formatter for a link field to display the URL as an absolute URL.

First, implement hook_field_formatter_info() to declare your field formatter to the fields API, and implement hook_field_formatter_view() which tells Drupal what to do when viewing the...

Read more

Dynamic forms in Drupal 7

28th Jan 2011

When building forms, you will often want to only provide certain options if other options are chosen by a user. For example, there's no need to show the 'open link in new window' checkbox, if the 'make this into a link' checkbox hasn't been ticked. These kinds of dynamic forms haven't been easily available for Drupal... until now, with the #states for form elements in Drupal 7.

Take a peek at this example of dynamic forms in Drupal 7.

To make one form element be dependent on another element, you may have delved into AJAX, or tried using CTools...

Read more

Certified to Rock field

19th Jan 2011

Certified to Rock is a site that allows you to look-up Drupal.org user names, and
see their 'certified to rock' score.

CertifiedToRock.com is a way that community members and employers can get a
sense of someone's involvement with the Drupal project. The site uses a custom
algorithm that gathers publicly available information which is then distilled
down into a score on a scale from one to eleven.

I've been wanting to build an easy way to bring this rating into a Drupal site to add an additional metric to user profiles. Until recently this wouldn't have been a simple undertaking...

Read more