New in Drupal 7 for developers (Part 1)

Posted Jan 11, 2011 // 0 comments
Brad :

There are a lot of new things for developers to take advantage of in Drupal 7. These sometimes replace the functionality that was available in Drupal 6 with new functions or hooks, and sometimes implement new functionality.

These are some of the important items that I’ve run across so far while doing D7 development.

New Hooks

template_preprocess_html allows a developer to modify the variables used in the new html.tpl.php template. This includes items such as the page title, favicon, feeds ( using drupal_add_feed ) or the string of classes that goes into the body tag.

hook_html_head_alter is one that might not be needed often, but can be useful for altering certain items because it runs after template_preprocess_html has run. However, there aren’t as many elements for alteration as in template_preprocess_html.

This might be useful if you need to programatically change the favicon, such as if your site has subthemes that do not have a favicon set, and you want to use the favicon from your main theme for those themes. If you try to remove the default favicon in template_preprocess_html and substitute a different one, Drupal will add the default favicon back in. You will need to invoke hook_html_head_alter to remove the favicon.

function mymodule_preprocess_html(&$vars) {
  global $theme;
  if ($theme != 'base' && theme_get_setting('default_favicon')) {
    drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => '/path/to/favicon.ico', 'type' =>'image/vnd.microsoft.icon'));
  }
}

function mymodule_html_head_alter(&$vars) {
  global $theme;
  if ($theme != ' base') {
    if (theme_get_setting('default_favicon')) {
      foreach ($vars as $key => $value) {
        if (strpos($key, 'misc/favicon.ico') !== FALSE) {
          unset($vars[$key]);
        }
      }
    }
  }
}

Image Styles

In Drupal 6 there was Imagecache. In Drupal 7, that has been remade in core into Image Styles. The basics remain the same, but the way to theme an image has changed. It still is done via a theme function, but instead of theme(‘imagecache’...), there is a separate function.

theme(‘image_style’, $variables) has six possible attributes for the variables array. They are:

style_name: Name of the image style.
path: Path of the image file relative to the files directory.
alt: Alt-text to use in the img tag.
title: Title text to use in the img tag.
attributes: Array of attributes to use in the img tag. These can include items such as css classes.
getsize: ( default TRUE ) Whether to retrieve and add the image’s dimensions as width/height attributes in the img tag.

$image_settings = array(
  'style_name' => 'imagestyle_name',
  'path' => $node->main_image[$node->language][0]['uri'],
  'alt' => check_plain($node->main_image[$node->language][0]['alt']),
  'title' => check_plain($node->main_image[$node->language][0]['title']),
  'attributes' => array('class' => 'image'),
  'getsize' => TRUE,
);
$image = theme('image_style', $image_settings);

Machine Names

Vocabularies now have machine names which enables them to be exported as features. It also allows you to easily determine the ID of a vocabulary ( using taxonomy_vocabulary_machine_name_load ) without having to store the ID somewhere such as in the variables table, hard-code it into your code, or perform a lookup using the name of a vocabulary which is more likely to change than the machine name.

These are just a few of the many changes in Drupal 7 that are available to developers. Check back tomorrow when I cover Master/Slave Replication and Query Changes.

About Brad

Web Developer Brad Blake brings a wealth of expertise to our team and our clients whenever he creates software tools and websites on the LAMP platform. For more than five years, he has been using PHP to build cutting-edge technologies that ...

more >

Read Brad 's Blog

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.