New in Drupal 7 for developers (Part 1)

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.

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.

<code>

  1. function mymodule_preprocess_html(&$vars) {
  2. global $theme;
  3. if ($theme != 'base' && theme_get_setting('default_favicon')) {
  4. drupal_add_html_head_link(array('rel' => 'shortcut icon', 'href' => '/path/to/favicon.ico', 'type' =>'image/vnd.microsoft.icon'));
  5. }
  6. }
  7.  
  8. function mymodule_html_head_alter(&$vars) {
  9. global $theme;
  10. if ($theme != ' base') {
  11. if (theme_get_setting('default_favicon')) {
  12. foreach ($vars as $key => $value) {
  13. if (strpos($key, 'misc/favicon.ico') !== FALSE) {
  14. unset($vars[$key]);
  15. }
  16. }
  17. }
  18. }
  19. }

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.

  1. $image_settings = array(
  2. 'style_name' => 'imagestyle_name',
  3. 'path' => $node->main_image[$node->language][0]['uri'],
  4. 'alt' => check_plain($node->main_image[$node->language][0]['alt']),
  5. 'title' => check_plain($node->main_image[$node->language][0]['title']),
  6. 'attributes' => array('class' => 'image'),
  7. 'getsize' => TRUE,
  8. );
  9. $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.

Brad Blake

Brad Blake