Drupal

From Elvanör's Technical Wiki
Revision as of 18:23, 9 December 2010 by Elvanor (talk | contribs)
Jump to navigation Jump to search

Themes

  • Themes are technically modules, so it is not allowed to have a theme of the same name of another module.
  • You can have a separate theme for the front end and the back office, but by default the theme is used for both aspects. Go to Administer -> Site configuration -> Administration theme to configure this.
  • When developping a theme, add the following function to your theme template.php file to flush the theme registry (apparently needed):
drupal_rebuild_theme_registry();

Adding regions to Themes

  • This is done in the theme's .info file. After that you can assign blocks to these regions; the region should be outputted in the theme's page.tpl.php file (via print $regionname).
  • Note that when JavaScript is active, the Weight field is not displayed in the Blocks administration page (you can drag & drop via JS to change the weight). It is displayed when JS is not enabled or the scripts are not loaded, so don't be surprised.

Customizing modules

  • Normally each module should register some theme hooks, which basically means that their output can be changed by themes (a default output should be provided as well). This can be done via a function (default) or the use of a template file. Some common registration are found in the common.inc file, for example a registration for the theme_links function. Note that you can automatically convert a theme function to a template (if there was already a function hook registered). The template file should have the same name as the function, where underscores are replaced by dashes. Warning: if you do this, you must not have a theme function in your theme's template.php file.
  • For a template file, you have a preprocess function that can perform some logic before passing data to the template.
  • For more information look at this link or this one. You cannot have a function and a template for theming; it's one or the other.
  • When looking for the default function for a theme function override, use the name of the function preceded by theme. For instance theme_menu_tree; never touch menu_tree directly.

Menus

  • Customizing menus is incredibly difficult in Drupal because the APIs are stupid and don't send enough information to the theme level overriden function. For instance theme_menu_item does not send any details about the level of the item, its parent, etc. If the core is left untouched (which should normally be always the case), the only way to really theme the menus is to use lots of regular expressions on theme_menu_tree (and theme_menu_item) to radically alter the produced HTML.
  • Other solutions (except core hacking) include:
    • use of modules / plugins (not yet investigated yet);
    • restrict links to one level, which make it easier to theme (as you don't have to deal with levels).

Forms

  • Theming forms is easier in Drupal. Very interesting link.
  • The second method is very useful. Changing the $form array is easily done in a preprocess function.
  • Note that for the third method, you still need to register the theme hook as you would do it normally for a template. Example:
function kameleoon_theme(&$existing, $type, $theme, $path)
{
	$hooks['user_login_block'] = array('arguments' => array('form' => NULL), );
	return $hooks;
}

function kameleoon_user_login_block($form)
{
  $output = '';
  $output .= drupal_render($form['name']);
  $output .= '<div class="MyClass">Hello World';
  $output .= drupal_render($form['access']);
  $output .= '<div class="bar">';
  $output .= drupal_render($form['details']);
  $output .= '</div></div>';
  //$output .= drupal_render($form);
  return $output;
}
  • One important thing to understand is that theming forms come after form processing has been done in the From API layer. You can change content of form fields in the theming layer but it is not very clean and some things won't work. For example #default_value won't have any effect as the processing of that parameter has been done before entering the theme layer (the preprocessing function for instance). The clean way of doing things is to hook form_later in a mini module.
  • theme_form_element is useful for removing / adding wrapper divs around form elements.

Content

  • By default Drupal assigns an URL of node1, node2 to all content created. You can see the URL by hovering on the content list.
  • The path module (included in default install) allows to change the URL of created content. Don't forget to activate it first.
  • You can associate a path with the front page on /admin/settings/site-information. Be careful that articles that appear on the front pages (default behavior) is not the same as an association to a page for the front page. In particular teasers are enabled for articles on the front page; it is not the case for a front page content article.