4

In Drupal 7, I have two fields field_cta_button and field_cta_text.

I am using field--field-cta-button.tpl.php to modify the outputs of the field.

I need the field_cta_button to change depending on the values of field_cta_text.

How can I access field_cta_text from field--field-cta-button.tpl.php?

If possible, I would like to do it without installing any extra modules.


Steps I have tired:

1) Node Load

I have used the following code from this page:

 <?php
$node = node_load($nid);
$field = field_get_items('node', $node, 'field_cta_text');
$output = field_view_value('node', $node, 'field_ctac_text', $field[$delta]);
?>

However, it just outputs this error message: Notice: Undefined variable: nid in include() (line 60 of /[SITE NAME]/sites/all/themes/legendary/[MY THEME]/field--field-call-to-action.tpl.php). EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7562 of /[SITE NAME]/includes/common.inc).

2) Entity Field Query I thought I might be able to use EntityFieldQuery(), but I'm not sure how to get it to load the current node.

1
+50

Have you tried using theme_field functions and/or template_preprocess_node() in template.php? For example:

/**
 * Implements theme_field__field_name().
 */
function myThemeName_field__field_cta_button($vars) {
  // Get the current value of cta_text, if it exists.
  if ($field_cta_text = field_get_items('node', $vars['element']['#object'], 'field_cta_text')) {
    $cta_text = $field_cta_text[0]['value'];
  }
  // If cta_text isn't set for this node, set a default. You may not need this.
  else {
    $cta_text = '';
  }

  // Print the button differently depending on whether or not cta_text is available.
  // This could mean you actually return different markup here, or you could alter the variables available to you in the tpl (I would just return the markup here if it's simpler).
  switch ($cta_text) {
    case 'foo':
      return "<button class='foo'>{$vars['element']['#items'][0]['value']}</button>";
      break;

    case 'bar':
    default:
      return "<button class='bar'>{$vars['element']['#items'][0]['value']}</button>";
      break;
  } 
}

OR in template_preprocess_node():

myThemeName_preprocess_node(&$vars) {
  if ($vars['node']->type == 'your_content_type') {
    $field_cta_text = field_get_items('node', $vars['node'], 'field_cta_text');
    if ($field_cta_text[0]['value'] == 'foo') {
      $vars['node']->field_cta_button['custom_key'] = 'custom value'; // This would be available to you in the theme_field__field_cta_button() function as well as in the field .tpl file.
    }
  }
}

You can still do it in the .tpl as is noted in the other answer by accessing $element['#object'], but I would recommend doing it in the theme_field() function or in the preprocess function. From the field.module documentation:

 * Theme developers who prefer to customize templates instead of overriding
 * functions may copy the "field.tpl.php" from the "modules/field/theme" folder
 * of the Drupal installation to somewhere within the theme's folder and
 * customize it, just like customizing other Drupal templates such as
 * page.tpl.php or node.tpl.php. However, it takes longer for the server to
 * process templates than to call a function, so for websites with many fields
 * displayed on a page, this can result in a noticeable slowdown of the website.
 * For these websites, developers are discouraged from placing a field.tpl.php
 * file into the theme's folder, but may customize templates for specific
 * fields. 
  • Thanks for the excellent explanation and the thorough code examples! – big_smile Jan 2 '13 at 11:30
4

You can access to the entity (node) to which the field is attached using $element['#object'] variable from the field template (or preprocess function).

For example, add this code in field--field-cta-button.tpl.php:

<?php
$field_cta_text = $element['#object']->field_cta_text;
echo $field_cta_text[LANGUAGE_NONE][0]['value'];
?>

If you installed devel module you can use dpm($element['#object']) function to inspect the entity variable.

0

Another option is to use hook_field_attach_load early on to adjust the loaded fields on the entity.

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.