0

I create custom template for node, and put variable in template.php

$colors = array('black', 'white');
$variables['group']['color'] = $colors;
$variables['size'] = 'double';

then i also create module and want to do hook to node template.

mymodule_preproccess_node() {
$colors = array('blue', 'red', 'green');
$variables['group']['color'] = $colors;
$variables['size'] = 'regular';
}

i want to show in my result:

//colors are blue, red, green instead of black, white
foreach ($group['color'] as $color) {
print $color.', ';
}
//and size is regular instead of double
print $size;

am i right? i want to override variables in template.php with variables from my module. then if i disable my module color will back from template.php.

2
  • Hello, and welcome on Drupal Answers. To be clear, the template.php file contains functions; overriding a variable in template.php is not possible, since each function has different variables. There aren't global variables defined in template.php. – kiamlaluno May 23 '13 at 20:15
  • so what i have to do? my aim is i want to show colors and sise in my custom node template. if my module is enabled then variables taken from mymodule_preproccess_node(), if disabled variables taken from template.php thanks – Rijalul fikri May 23 '13 at 20:20
0

If I am understanding/reverse engineering your question correctly, in your module, you can do something like this:

function YOURMODULE_preprocess_node(&$variables) {

  if ( // whatever logic ) {

    $colors = array('blue', 'red', 'green');
    $variables['group']['color'] = $colors;
    $variables['size'] = 'regular';

  }

}

This will create variables that can be accessed inside node.tpl.php and its variants.

In template.php you can then do something like this:

function YOURTHEME_preprocess_node(&$variables) {

  if (!isset($variables['size'])) {
    $variables['size'] = 'double';
  }

  if (!isset($variables['group']['color'])) {
    $colors = array('black', 'white');
    $variables['group']['color'] = $colors;
  }

}

which will set them to your defaults if they haven't already been set.

Then you can do whatever you want with them in node.tpl.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.