Advanced theming

10
Drupal Advanced Theming DrupalCamp Estonia Kaido Toomingas 14.June 2012

Transcript of Advanced theming

Page 1: Advanced theming

Drupal Advanced ThemingDrupalCamp Estonia

Kaido Toomingas14.June 2012

Page 2: Advanced theming

Problem

● You need more than Drupal theme can offer you out of box

● You dont need some stuff wich Drupal offers You out of box

Page 3: Advanced theming

Solutions

● Lets just put this into templates ;) What does Your designer would say?

● What should I do then?

● Template.php

● Custom module

Page 4: Advanced theming

How can I implement this?

● Hooks? Examples...?

● TEMPLATE_preprocess(&$variables, $hook)

● HOOK_form_alter(&$form, &$form_state, $form_id)

● theme($hook, $variables = array())

● THEMEHOOK_settings() in theme-settings.php

● THEMENAME_form_system_theme_settings_alter(&$form, $form_state)

● hook_theme_registry_alter()

Page 5: Advanced theming

Hooks

Hooks in drupal are functions wich will help you to change or extend default functionality.

Page 6: Advanced theming

Preprocess

● TEMPLATE_preprocess(&$variables, $hook)

● TEMPLATE_preprocess_HOOK(&$variables)

● Manipulate or create variables. Example page variables. You can add these into template.php.

Example:function template_preprocess_foo(&$variables) {

$variables['foo_list'] = array(

'list item 1',

'list item 2',

'list item 3',

);

}

Page 7: Advanced theming

Modify forms

● HOOK_form_alter(&$form, &$form_state, $form_id)

● hook_form_FORM_ID_alter

● Change existing form elements by changing or adding some arrays.

● Create new form elements – There is better way.

Example:

function example_form_alter(&$form, &$form_state, $form_id) { dsm($form_id); // print form ID to messages You need devel module switched for using dsm() function dsm($form); // pretty print array using Krumo to messages}

Page 8: Advanced theming

Theme some data

● theme($hook, $variables = array())

● Usecase: You want to create a table, image or some other element from you data. You should use theme function for that.

● Usage: Can be used in template.php or custom modules.

Example:

theme('table', array( 'header' => $header, 'rows' => $options, 'attributes' => array('class' => array('mytable'))));

Page 9: Advanced theming

hook_theme_registry_alter()

● This is the most powerful themer helper wich should be used only when can't find any way to change your theme.

● You can use this when there is module wich doesnt have proper theme fuctions wich could be overwritten.

Page 10: Advanced theming