Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
 
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 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 at 20:20
1  
You should first revise your question, taking the information given you by kiamlaluno into account. Having a clear question without obvious misunderstandings will increase your chance of being helped. Delete your comment when done revising - comments are second-class citizens here. –  Gisle Hannemyr May 23 at 20:37
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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