Custom add another

6th Mar 2012

Drupal 7 fields are great, but using multiple valued fields and field collection you can quickly reach the point of having a form that looks like this:

A complex field collection form

In this contrived example I've got two field collections and a multi-valued 'Links' field within that.

Although it's reasonably clear what clicking 'Add another item' will do in each case, when the form is full of data it can become less so.

We can give content editors a helping hand by changing the buttons from 'Add another item', to something more useful like 'Add another carousel item' etc. With the Custom add another...

Read more

Add stuff to a node and configure it like fields

28th Feb 2012

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 hook_node_view().
 * Adds a Facebook like button to page nodes.
function MYMODULE_node_view($node, $view_mode, $langcode) {
  switch ($node->type) {
    case 'page':
      $node->content['MYMODULE_fb_like'] = array(
        '#type' => 'item',
        '#title' => t('Like...
Read more

Quick tips: Adding an empty master branch

21st Feb 2012

When I create a git repository for a new project on I don't bother to create a master branch, branches named 6.x-1.x or 7.x-1.x have special meanings and are the ones that we're encouraged to use. However, doesn't allow us to change the default branch on d.o itself, so even though there may be no branch called 'master', it's still the default branch, so sometimes cloning a repo will fail:

git clone
Cloning into 1268648...
remote: Counting objects: 177, done.
remote: Compressing objects: 100% (176/176), done.
remote: Total 177 (delta 103), reused 0 (delta 0)
Receiving objects:...

Read more

Create a Drupal ajax link in Drupal 7 (it's easy)

14th Feb 2012

Part 1: Make the link work without javascript

In this example we have a table listing log entries of some kind. We want to put a delete link against each log entry allowing the administrator to delete the log entry:

The following code is responsible for printing out each link onto the page:

$query = array(
  'tok' => drupal_get_token('delete_log_item' . $flid),
) + drupal_get_destination();
$output[] = l(t('Delete'), 'admin/my-custom-log/delete/' . $flid, array('query' => $query));

The callback of which is defined by the following hook_menu item:

  $items['admin/my-custom-log/delete/%'] = array(
    'page callback' => 'my_custom_log_entry_delete',
    'page arguments' => array(3),
    'access arguments' => array('permission name'),...
Read more