1

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 "
                  }
                }
              }
BVSK
  • 133
  • 2
  • 12
  • just a tip... there is a function `get_defined_vars()` that can be used to see what variables are available in the tpl file. so, if you have the devel module enables, you could put `dpm(get_defined_vars())` into the tpl file and reload the page to get a pretty list of all the variables. – 2pha Oct 25 '16 at 06:47

4 Answers4

1

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

Community
  • 1
  • 1
BVSK
  • 133
  • 2
  • 12
0
$node = node_load($nid);
$example_value = $node->field_EXAMPLE[$node->language][0]['value'];

<h2> <?php print $example_value;?> </h2>

or,

$node = node_load($nid);
$values = field_get_items('node', $node, 'EXAMPLE');
if ($values != FALSE) {
  $val = $values[0]['value'];
}
else {
  // no result
}
<h2> <?php print $example_value;?> </h2>
  • I doubt the node needs to be loaded as it is probably already in a variable. See my comment about the `get_defined_vars()` function above. – 2pha Oct 25 '16 at 06:50
  • According to the BVSK's question, need to pull value of other node's field in the page.tpl. So load node is necessary. – Vivek Kumar Oct 25 '16 at 07:36
  • @VivekKumar I get an error ` Undefined variable: nid in THEME_preprocess_page()` in `page.tpl.php` for this line `$node = node_load($nid);` – BVSK Oct 25 '16 at 16:06
  • EntityMalformedException Missing bundle property on entity of type node. `in entity_extract_ids()` on line 7907 `....\ROOTFOLDER\includes\common.inc)` – BVSK Oct 25 '16 at 16:26
  • @BVSK You have to pass node id that you want to render or there must be some relation between article and custom content type to fetch node id. – Vivek Kumar Oct 28 '16 at 03:14
0

You can place your code directly into page template, but you can also place it in page preprocess hook, set the template variable and use that variable from your page template:

https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x

It's kinda cleaner way, but both should work.

Also, try deleting Drupal's cache if you don't see your change immediately on front-end.

And for getting node id try:

global $node;
$nid = $node->nid;
$node = node_load($nid);
...

And if that doesn't work try this:

    if ($node = menu_get_object()) {
      // Get the nid
      $nid = $node->nid;
      $node = node_load($nid);
      ...
    }

or this way:

if (arg(0) == 'node' && is_numeric(arg(1))) {
  // Get the nid
  $nid = arg(1);
  $node = node_load($nid);
  ...
}
MilanG
  • 6,994
  • 2
  • 35
  • 64
  • It doesn't like `$nid = $node->nid;` . Notice: Trying to get property of non-object in `THEME_preprocess_page()` – BVSK Oct 25 '16 at 17:36
  • Ok, then try other options. First get node id then load the node it self if you need it. – MilanG Oct 26 '16 at 06:46
0

You can use a preprocess to add value into $variables passed to template

https://api.drupal.org/api/drupal/includes%21theme.inc/function/template_preprocess_page/7.x

In template.php :

MYTHEMENAME_preprocess_page(&variable){
 $values = current(field_get_items('node', $variable['node'],'field_machine_name'));
 $variable['myvar'] = $values['value'];
}

In your template.tpl.php

echo $myvar; // contains your value
Fky
  • 2,133
  • 1
  • 15
  • 23