template.php > THEMENAME_field_CONTENT_TYPE(&$variables) I am trying to check each field as it passes through...

if( $variables['element']['#field_name'] == 'field_I_am_looking_for' )
    if( $variables['element'][0]['#title'] == 'value_I_am_looking_for' )
    $variables['newVariableName'] = 'NewVariableValue';

or for Booleans...

$variables['newVariableName'] = True;

But in *.tpl.php file, when I do this...

<?php if( isset( $content['isIcebreaker'] ) ): ?>
<pre>It's What You're Looking For!</pre>
<?php endif; ?>

...Nothing happens.

Specific Details... The custom content type is called 'Netrunner Card' and all cards have 0 or more 'subtypes'. The subtypes field is a taxonomy_reference field. One of those subtypes is 'Icebreaker' and I'm trying to set $variables['isIcebreaker'] = True; whenever a card has the subtype of 'Icebreaker' and then test against that in node--netrunner-card--full.tpl.php.

Basically, in regular-speak, when a card has 'Icebreaker' as one of its subtypes, I want to add in some custom div's and other content in the node--netrunner-card--full.tpl.php for those cards. But the custom variable isn't making it over. I'm uncertain as to what I'm doing wrong.

SIDE NOTE If there is a better way to do what I am trying to do, I would appreciate those answers as well. However, regardless, I would still love to know the appropriate, Drupal Best Practices way of passing custom variables from template.php to a *.tpl.php file.

CODE VIEW I have split off my template.php and included the new file as sort of an extended template.php. Here are posts of both my template.php and my tpl file.



ANSWER: I used the preprocess_node function like so...

  • The function you posted in your template.php isn't a preprocess function. May 30, 2013 at 16:00
  • Ok. So... forgive my noobishness... what am I doing wrong? I don't understand the preprocess versus what I did... (aka how is that relevant?) Does this mean I am using a 'process' function? And is what I'm doing there make more sense in a preprocess? and how would I make it a preprocess function? May 30, 2013 at 16:06
  • 1
    You'll want to implement hook_preprocess_field to adjust variables passed to a field. This will get called for all fields so you need to test the field name - $variables['element'][field_name'] I think - to do anything that's specific for a particular field. May 30, 2013 at 16:13
  • So could I just change the name of my current function in template_netrunner_card.php and go? Or are we talking about a complete rework? Or just add in a preprocess function for the part I am working on? May 30, 2013 at 16:23
  • 1
    This has been discussed before for node preprocess: drupal.stackexchange.com/questions/27947/… May 31, 2013 at 8:27

1 Answer 1


Every element key in the root of the $variables array will be available in the template file as an actual variable. The mechanics behind this is in theme_render_template(), which uses PHP's extract() function on the theme's variables.

So when you look at $content in the template file, that corresponds to $variables['content'] in the preprocess function.

Similarly, if you add $variables['newVariableName'] in the preprocess function, it will be available at $newVariableName in the template file.

If you want to add it to the content array, you need to do so explicitly in the preprocess function, e.g. $variables['content']['newVariableName'] = ...

Bear in mind that the content array is supposed to be a render array, so that's probably not the best place for it. The way you have it currently is fine, just use if (isset($newVariableName)): in place of your current array_key_exists condition.

Your current method is considered best practice for passing variables from preprocess functions (whether in template.php or a module file) to template files, you don't need to change anything in your thinking there.

  • 1
    It's hard to say what the problem is without seeing the full code. Could you dump the whole function into the question (including the signature)? Could just be you're not passing $variables by reference
    – Clive
    May 29, 2013 at 20:09
  • Here is a sample page: netrunner.randomblink.com/node/143 This is an Icebreaker. There should be a little PRE tag loaded with 'It's an Icebreaker!' showing up on the page... but there isn't. May 29, 2013 at 20:14
  • I can't tell whether you responded by editing your answer or not... but it still isn't working... May 29, 2013 at 20:49
  • I'm using this: if( $variables['element'][0]['#title'] == 'Icebreaker' ) { $variables['isIcebreaker'] = True; } And it's still not going through... When I replace the $isIcebreaker variable = True with a dvm, the dvm runs. So I know I am firing at the right time. But in my tpl, it isn't rolling through. May 30, 2013 at 13:15
  • Ok... to test even further... I went into THEMENAME_field_CONTENT_TYPE(&$variables) and I this: $variables['TheTest'] = '<pre>Poop!</pre>'; just to see if I could pass ANYTHING back and forth... Then in my tpl.php file I added: <?php print render( $TheTest ); ?> and I got NOTHING. I even tried $content['TheTest'] and got nothing... I'm thinking something is going wrong with my &$variables... May 30, 2013 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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