2

I am interested in learning what disadvantage, if any, there are in calling variable fields directly within the template.tpl.php files.

Is it ok to place a field variable within the template.tpl.php file if doing it as simply as:

<?php 
//--------------------------------
// COLOR
if(!empty($content['product:title']['#product']->field_taxonomy_color['und'][0]['taxonomy_term']->name)): ?>
    <div id="color-wrapper">
    <?php
        $max_length = count($content['product:title']['#product']->field_taxonomy_color['und'])-1;
        for($x = 0; $x <= $max_length; $x++){
            echo '<div class="color">'. $content['product:title']['#product']->field_taxonomy_color['und'][$x]['taxonomy_term']->name .'</div>';
        }  
    ?>
    </div>
<?php endif; ?>

My understanding is that this code should be placed a preprocess function and called into the tpl.php file such as:

TEMPLATE.PHP

function theme_preprocess_node(&$variables) { $variables['color'] = ... }

TEMPLATE.TPL.PHP

<?php echo $color; ?>

If this is the case, how would i return all values of the loop from template.php into template.tpl.php?

---------EDIT---------
Should I create complex logic within its own field--template.tpl.php file, set the field to a variable in template.php and call upon the variable inside the node--template.tpl.php? Would there be a problem with creating over a hundred field--template.tpl.php files?

  • 1
    AFAIK you shouldnt be using a template.tpl.php because it isn't attached to anything in Drupal... look at this DO article you should only use template.php for general theme functions and YOUR_TEMPLATE_NAME.tpl.php for specific pages which you need to let Drupal know using theme suggestions when to use (ie with a specific content type, a specific view, or panel etc) – longboardnode Sep 28 '17 at 16:37
  • 2
    Thank you, If I declare all the variables within template.php, wont the file get rather large and bog down on site speed? Also, within the preprocess_node function, what would the above code look like? – CleanCode Sep 28 '17 at 18:30
  • The way you have it is fine and I don't see how this would bog down your site speed... you're just aliasing the variables for ease of access. The only thing you have to change is make your template.tpl.php into page.tpl.php or node--YOUR_CONTENT_TYPE_NAME.tpl.php for example, it depends upon your theme suggestions schema. – longboardnode Sep 28 '17 at 19:19
  • You do not want code like that in the first block at all in templates. Do that logic in template.php thats what it is there for. Its up to you to decide how you want to create new variables to pass into the template to render. Example, just create an array of color names in template.php, loop that in the template and output it. Easy... and less spaghetti code. – Kevin Sep 29 '17 at 1:33
2

The disadvantage is you leak a lot of PHP code processing Drupal inner structures to the template file.

I would preprocess $content in template.php to expose a simple array of strings as a custom variable:

$colors = $content['product:title']['#product']->field_taxonomy_color['und'];
$colorNames = Array();
for ($x = 0; $x < count($max_length); $x++) {
  $colorNames[$x] = $colors[$x]['taxonomy_term']->name;
}
$variables['color_names'] = $colorNames;

Then the template code could be:

<?php if ($color_names): ?>
<div id="color-wrapper">
  <?php foreach ($color_names as $cn)
    print '<div class="color">' . $cn  . '</div>'; ?>
</div>
<?php endif; ?>

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.