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. 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:

#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';
$ 

Submit as validate and validate as submit. This is going to be interesting as I dig into the code.

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.

Currently looking for small Drupal projects

Blog

I'm currently looking for Drupal contract work. In particular, I'm looking for smaller projects, short-term work. A smaller project can be an entire project start to finish for a site that is well defined in terms of features and can use most of the modules that currently exist (that is to say, minimal custom work); or a part of a larger project. The small part of a larger project appeals to me, as I enjoy working in groups and seeing parts come together into a fantastic whole.

My resume is Drupal heavy as of late, and I'd like to continue that. I am not, however, opposed to other PHP work. I'm good with PHP, MySQL, HTML, CSS, browser Javascript (drifting towards jQuery), integrating sites together (consuming APIs, so to speak), and Linux/Apache/SSL site setup.

Please do contact me if you have a Drupal project (or maybe another project that could be otherwise interesting, no sense in limiting myself to just Drupal!).

drush skip svn checking

Snippet

Skip SVN checking of current status when updating

drush upc --version-control=backup --no-backup

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']};
  }
}

Pages