drupal7

Drupal 7 redirect to last published node of a specific node type

Snippet

Okay, you have a number of node of a particular content type, say, a Daily Photo, and you want a link, say, a menu link, that will always take you to the latest published node of that content type, but do it with a redirect, instead of a view with the latest node. The former says, when you go to this URL, redirect to the correct URL; while the latter says, when you go to this URL, display the content from that other URL here. Sometimes the latter is better, I prefer the former for my daily photos.

So, here's how you'd create that redirect in Drupal 7. You can put this into a page, or create a new module and add this to it, or use an existing module (say, a features module or something).

<?php
 
    $query = new EntityFieldQuery();
    $query
    ->entityCondition('entity_type', 'node')
    // of course, this would change to your node type
    ->propertyCondition('type', 'dailyphoto')
    // only published ones
    ->propertyCondition('status', 1)
    // 'posted' can also be 'created' if you prefer
    ->propertyOrderBy('posted', 'DESC');
 
    $result = $query->execute();
    if (isset($result['node'])) {
      // yup, ugly.  Works.
      $nids = array_keys($result['node']);
      drupal_goto('node/' . $nids[0]);
    } else {
      // well shit, go home
      drupal_goto('/');
    }
 
?>

Drupal 7 modifying field output before rendering

Snippet

Or, how to add a # to a tag vocabulary field, making it a #hashtag

#hashtag!

Okay, there are times when you want to modify the text or value of a field before dumping it on the page. Sometimes, for whatever reason, you can't do it in a place other than just before rendering. Take, for example, the case of tags. Maybe you want to add a # in front of the tag text so that it looks like a #hashtag.

In this case, you want to use the template_preprocess_field function.

To use, add the following HTML to your theme's template.php file, changing THEME to your theme's name, FIELDNAME to the name of the field you want to modify, and the modification you want to do.

/**
 * template_preprocess_field()
 * https://api.drupal.org/api/drupal/modules%21field%21field.module/function/template_preprocess_field/7
 *
 * @param array $vars
 * @param string #hook
 */
function THEME_preprocess_field(&$vars, $hook) {
  $element = $vars['element'];
  if (isset($element['#field_name'])) {
    if ($element['#field_name'] == 'field_FIELDNAME') {
      foreach ($vars['element']['#items'] as $i => $e) {
        // this is where the manipulation actually happens.
        $vars['items'][$i]['#title'] = '#' . $vars['items'][$i]['#title'];
      }
    }
  }
}

Adding Human Readable Content Type to Node display in Drupal 7

Blog

In the mismash of my site's content, I have a bunch of different content types on the front page, and it isn't clear what posts are what type. I've been reading a lot of books, so there are more book reviews than blog posts, and come on, who wants to read all of those reviews (hint: not me, I'm writing them so that I don't read the books again because I forgot that I read them the first time). So, on my vacation, I decided to get to the niggly little things that annoy me in various systems. First up: human readable content types in the posts.

I'm talking about these things:

Okay, so, to do this, in your theme's template.php file, add a node preprocess hook function. You may already have one. Look for THEMENAME_preprocess_node.

Add these two lines:

Deleting contexts in Drupal 7

Blog

When removing contexts, use context_load to retrieve context object from the database, then context_delete to remove it.

A specific example:

function example_update_7001() {
  // 'example_one' is the value at /admin/structure/context in the Name column
  $contexts = array('example_one', 'example_two');
  foreach ($contexts as $context_name) {
    // load the context from the DB
    $context = context_load($context_name);
    // context_load returns FALSE if the context doesn't exist
    if ($context) {
      context_delete($context);
    }
  }
}

You can also check the module is even enabled with module_exists('context') around those lines.

Turning off Drupal 7 admin toolbar

Blog

Drupal 7 has this nice lovely toolbar at the top of admin pages, right?

It's a great idea. It adds easy access to important parts of the website. It's a great improvement from the standard administration workflow.

It also doesn't provide the delightful dropdown menus of the Administration menu toolbar. I don't like the Drupal 7 admin tool bar. Yes, it stays put (position:fixed), but it's the bastard half brother of what I'm used to having.

The easiest way to disable the default admin toolbar and enable the admin_menu toolbar?

After installing the Administration menu, in admin/people/permissions:

1. Enable Administration menu » Access administration menu for the role you want (I updated the Administrator role).

2. Disable Toolbar » Use the administration toolbar for the role you want (I updated the Administrator role).

No code modification. Yay!

Get the bundle of a Drupal 7 entity

Snippet

Drupal 7's EntityAPI allows entities to be divided into different "bundles" based on their properties. Unfortunately, there's no way to easily determine what bundle a particular concrete entity object belongs to. This helper function appears to work for the entity types that ship with core.

function _entity_get_bundle($entity, $entity_type) {
  $info = entity_get_info($entity_type);
  if (empty($info['entity keys']['bundle'])) {
    return $entity_type;
  } else {
    return $entity->{$info['entity keys']['bundle']};
  }
}

Drupal 7 Multiviews Error fix

Blog

Installing Drupal 7 for a project, received a 500 Server configuration error.

Looking in the Apache error log, I saw the error:

[Wed Jan 12 19:40:24 2011] [alert] [client xxx.xxx.xxx.xxx] /var/www/httpdocs/.htaccess: Option Multiviews not allowed here

Turns out, in the apache2 config file, the following is not sufficient in the Directory section:

AllowOverride Options FileInfo AuthConfig Limit Indexes

In particular, the MultiViews option is needed:

AllowOverride Options=All,MultiViews FileInfo AuthConfig Limit Indexes