drupaldrupal-7drupal-themingdrupal-fieldsdrupal-entities

Add a field from a node into page.tpl.php, drupal 7


Creating a sub-theme in Drupal 7's page.tpl.php and needing to pull the value (plain text) from field_EXAMPLE from a custom content type outside of where the rest of the content would normal be.

<!-- Adding $title as normal-->
    <?php print render($title_prefix); ?>
        <?php if (!empty($title)): ?>
            <h1><?php print $title; ?></h1>
        <?php endif; ?>
    <?php print render($title_suffix); ?>

<!-- THE ISSUE: Adding field_EXAMPLE -->
    <h2> <?php print render($field_EXAMPLE;);?> </h2>
    ...
<!-- Where the rest of the content loads by default -->
    <div><?php print render($page['content']); ?></div>

Would field_get_items work?

function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
  $langcode = field_language($entity_type, $entity, $field_name, $langcode);
  return isset($entity->{$field_EXAMPLE}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE;
}

Or this?

$node = node_load($nid);
$node->field_EXAMPLE[$node->language][0]['value'];

Do I put this in page.tpl.php? Tried them but no dice. -Novice

Here is var_dump(get_defined_vars());

              ["field_thestring"]=>
              array(1) {
                ["und"]=>
                array(1) {
                  [0]=>
                  array(3) {
                    ["value"]=>
                    string(44) "This is a string of text please refer to me "
                    ["format"]=>
                    NULL
                    ["safe_value"]=>
                    string(44) "This is a string of text please refer to me "
                  }
                }
              }

Solution

  • Lets assume that you created a field called field_thestring that you want to render for a content type article's page at a location outside of THEME's outside of where page's content renders.

    Step 1. Copy the theme's page.tpl.php. and rename it page--article.tpl.php.

    Step 2. In page.var.php,

    function THEME_preprocess_page(&$variables) {
    
    // To activate page--article.tpl.php 
    if (isset($variables['node']->type)) {
     $nodetype = $variables['node']->type;
     $variables['theme_hook_suggestions'][] = 'page__' . $nodetype;    
    }
    
    // Prevent errors on other pages
    if ($node = menu_get_object()) {
    
    if ( !empty($node) && $node->type == 'article') {
    $fields = field_get_items('node', $node, 'field_thestring');
    $index = 0;
    $output = field_view_value('node', $node, 'field_thestring', $fields[$index]);
    $variables['thestring'] = $output;
    }
    else{
    $variables['thestring'] = 'Angry Russian: How this error?';
    }
    }
    }
    

    Step 3. In page--article.tpl.php add <?php print render($thestring); ?>

    Initially, I wanted to require all content types to have another field since all Content Types has a Title. Determined it wasn't a great idea for further development.

    Source