0

I want to combine two fields (say first name and last name) of a particular type of node so that the name displays correctly and I still have the individual fields to work with in different areas.

I saw this post What is best way to combine multiple fields in template preprocess? that explains how to combine two fields, but one I declare this new combined variable, how to I get it to display as the node?

Do I need to edit my node.tpl file? Should I have a node.tpl for this particular content type?

1

Once you have preprocessed your variables in theme_preprocess_node() you can print them in node.tpl.php

The best practices would imply to declare one node.tpl.php per content type like this : node--my-content-type.tpl.php replacing my-content-type by the name of your own content.

In theme_preprocess_node() the best way to get the value of a field would be like this : In template.php the first function is only here so you can have one preprocess function per content type.

/**
 * Override or insert variables into the node templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)
 */
function mytheme_preprocess_node(&$vars, $hook) {
  // Optionally, run node-type-specific preprocess functions, like
  // mytheme_preprocess_node_page() or mytheme_preprocess_node_story().
  $function = __FUNCTION__ . '_' . $vars['node']->type;
  if (function_exists($function)) {
    $function($vars, $hook);
  }
}

Then you can declare your function for your content type and get the value of your fields with field_get_items() and field_view_value() in order to only get the value and not all the divs all around the field value.

/**
 * Override or insert variables into the node templates.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("node" in this case.)
 */
function mytheme_preprocess_node_my_content_type(&$vars, $hook) {
  $node = &$vars['node'];
  $first_name = field_get_items('node', $node, 'field_first_name');
  $last_name = field_get_items('node', $node, 'field_last_name');
  $vars['first_name'] = field_view_value('node', $node, 'field_first_name', $first_name[0]);
  $vars['first_name'] = field_view_value('node', $node, 'field_last_name', $last_name[0]);
}

and then in your node--my-content-type.tpl.php you can print your variable like this :

<?php print render($first_name); ?> <?php print render($last_name); ?>
  • Ok. A best practice question. I thought that a better practice was t edit the template file and leave the node.tpl file alone. No? So no matter what, I will need to insert my custom variable into the node.tpl file? – Baybook Nov 18 '14 at 14:19
  • Yep exactly you can copy and paste node.tpl.php as often as you want for any content types as lons as you rename it to target your content type and respect the file naming. Anyway, node.tpl.php will always still be in node core module. – pbonnefoi Nov 18 '14 at 14:23
  • I'm not sure to understand the end of your comment as it was eddited after my comment, but it's the best drupal way if you want to do it clean. – pbonnefoi Nov 18 '14 at 17:30
0

If you add some login to your THEME_preprocess_node function, in template.php, so vars declared on that function are available on node.tpl or other templates declared for that node (Here instructions about theme hook suggestions).

<?php  
/**
 * Implements hook_preprocess_node().
 */
function mytheme_preprocess_node(&$vars) {
  $vars['my_custom_variable'] = 'This is the value!';
}
?>

On node.tpl you could access this variable in this way:

<?php
print $my_custom_variable;
?>

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.