Initialiser un fichier template.php
à la racine de votre thème :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php /** * @file * template.php file for <my_theme>. * * Implement preprocess functions and alter hooks in this file. */ /** * Preprocess function for page.tpl.php */ |
Afficher les hooks disponibles dans la page
1 2 3 |
function <my_theme>_preprocess(&$variables, $hook) { kpr($hook); } |
Afficher l’ordre dans lequel les hooks sont chargés dans la page
Petite info supplémentaire par rapport à la fonction précédente :
1 2 3 4 5 |
function <my_theme>_preprocess(&$variables, $hook) { static $i; kpr($i .' '. $hook); $i++; } |
Seulement quand le hook page
est chargé (1x par chargement de page) :
1 2 3 4 5 6 7 |
function ninesixtyrobots_preprocess(&$variables, $hook) { if ($hook == 'page') { static $i; kpr($i .' '. $hook); $i++; } } |
On passe le hook page
dans le nom de la fonction. Plus besoin de le passer en argument.
1 2 3 |
function ninesixtyrobots_preprocess_page(&$variables) { print 'wooooooooo'; } |
Voir quelles variables sont disponibles
1 2 3 |
function ninesixtyrobots_preprocess_page(&$variables) { kpr($variables); } |
Changer la valeur de la variable « site_solgan » dans une preprocess function
1 2 3 |
function ninesixtyrobots_preprocess_page(&$variables) { $variables['site_slogan'] = 'my new value'; } |
Un slogan qui s’affiche en random
On crée un tableau $slogans qu’on remplit avec différents slogans qu’on fera s’afficher de manière choisie au hasard et alternée :
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function ninesixtyrobots_preprocess_page(&$variables) { $slogans = array( t('lorem ipsum'), t('dolor sit amet'), t('today is gonna be the day'), t('that they_re gonna throw it back to you'), ); // Select a random slogan from the list and set the $site_slogan variable to // the new random slogan $slogan = $slogans[array_rand($slogans)]; $variables['site_slogan'] = $slogan; } |
Des locales avec la fonction t()
Utiliser la fonction t() pour mettre en place du texte traduisible en plusieurs langues :
1 |
$slogans[] = t('lorem ipsum'); |