drupal

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.

#glare

Blog
$ grep -r notifications_subscription_list_form contrib
contrib/notifications/notifications.module: $form['#submit'][] = 'notifications_subscription_list_form_validate';
contrib/notifications/notifications.module: $form['#validate'][] = 'notifications_subscription_list_form_submit';
$ 

Generate content of specific node type

Use drush with the devel module to generate content, limiting to a specific node type with the --types option:

drush generate-content 10 --types=page

As always, --help shows the options:

[khodsden@sportsvideohub html]$ drush generate-content --help
Create content.

Arguments:
 number_nodes                              Number of nodes to generate.            
 maximum_comments                          Maximum number of comments to generate. 


Options:
 --feedback                                An integer representing interval for insertion rate logging. Defaults to 500 
 --kill                                    Delete all content before generating new content.                            
 --languages                               A comma-separated list of language codes                                     
 --skip-fields                             A comma delimited list of fields to omit when generating random values       
 --types                                   A comma delimited list of content types to create. Defaults to page,article. 


Aliases: genc

Drupal Programmaticaly Revert Features

Blog
    // Revert individual components of one or more Features
    features_revert(array('module' => array('component')));
     
    // Revert all components of a single Feature
    features_revert_module('my_module');

Useful in update hooks.

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.

Pages