Adding a variable to all node templates

Public
</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * Override or insert variables into the node templates.
  5.  *
  6.  * @param $vars
  7.  * An array of variables to pass to the theme template.
  8.  * @param $hook
  9.  * The name of the template being rendered ("node" in this case.)
  10.  */
  11. function mytheme_preprocess_node(&$vars, $hook) {
  12. $vars['disclaimer'] = t('This sites takes no responsibility for user contributed content and comments.');
  13. }

Comments

paulbooker's picture

#node.tpl.php

  1. <div class="disclaimer">
  2. <?php print $disclaimer; ?>
  3. </div>
paulbooker's picture

In Drupal 7

  1. <?php
  2.  
  3. #theme-settings.php
  4.  
  5. /**
  6.  * Implements hook_form_system_theme_settings_alter().
  7.  *
  8.  * @param $form
  9.  * Nested array of form elements that comprise the form.
  10.  * @param $form_state
  11.  * A keyed array containing the current state of the form.
  12.  */
  13. function myzentheme_form_system_theme_settings_alter(&$form, &$form_state) {
  14.  
  15. // Create the form using Forms API
  16.  
  17. /* -- Delete this line if you want to use this setting
  18.   $form['myzentheme_example'] = array(
  19.   '#type' => 'checkbox',
  20.   '#title' => t('myzentheme sample setting'),
  21.   '#default_value' => theme_get_setting('myzentheme_example'),
  22.   '#description' => t("This option doesn't do anything; it's just an example."),
  23.   );
  24.   */
  25.  
  26. $form['myzentheme_disclaimer'] = array(
  27. '#type' => 'textfield',
  28. '#title' => t('Node disclaimer'),
  29. '#default_value' => theme_get_setting('myzen_disclaimer'),
  30. '#description' => t("This sites takes no responsibility for user contributed content and comments.")
  31. );
  32.  
  33. // Remove some of the base theme's settings.
  34. unset($form['themedev']['zen_layout']); // We don't need to select the layout stylesheet.
  35. }
  36.  
  37. #template.php
  38.  
  39. function myzentheme_preprocess_node(&$variables) {
  40. $variables['disclaimer'] = theme_get_setting('myzen_disclaimer');
  41. }