2

I have a custom field called site_category that I am using in a few different content types. The field site_category is an entity reference to a content type which has only one field defined: Title. For the purposes of this example, I have three instances of that content: Things, Widgets, Places

I have another content type called content that has a number of fields including site_category

(Think of this like taxonomy, but using content types rather than terms)

I would like to add in the site_category field to my page.tpl.php so that it not only displays the title of the page, it also displays the site category that it is related to.

So, as an example, if I had a "Super Duper Widget De-Duper" that fell under the widgets category...

example URI:

.com/widgets/super-duper-widget-deduper

Page would render (styled of course):

Header
**Widgets**
**Super Duper Widget De-Duper**
Content
Footer

I have found a number of similar posts here on stackoverflow like this one: Access custom node field in page.tpl.php However, it dosen't work and I don't know what I am doing wrong. There is a lot of php code provided, but I don't know where to put said php code because it is never really referenced.

I have added the output from the Devel module. It appears that the field is being loaded as it is "there." Shouldn't I be able to access it?

Devl Output

I have added the following code with a reference to the field that is displaying in Devel:

if (isset($variables['id'])) {
// Get the node ID and load it.
$n = node_load($variables['id']);

$variables['site_category'] = "Breakpoint 1";

//If "Breakpoint 1" shows up on the page output, we know that (isset($variables['id']))
//evaluates as  "true"

// Check if the node exists
if ($n) {
  $variables['site_category'] = "Breakpoint 2";

  //if "Breakpoint 2" shows up on the page output, we know that the ($n) evaluates 
  //evaluates as true.

I only added the two breakpoints - no other changes were made. When I view the output, only Breakpoint 1 shows up meaning the node doesn't exist.

4

Since you want to function on a "page level" (outside the content area) what you can do is:

  1. Use hook_preprocess_page
  2. Get the node id being viewed
  3. Query the node using node_load or just your "term field" alone using EntityFieldQuery

This would somehow look like:

Your theme or custom module

// This function is inside my template.php
function YOURTHEME_preprocess_page(&$variables) {
  // This array key is only present when a page is a node page(node/<nid>).
  // If this is set, this contains the node object already.
  if (isset($variables['node'])) {
    $n = $variables['node'];

    // Don't forget to change this also to check if the reference field 
    // has at least one reference/target node.
    if (isset($n->body['und'][0])) {
      $variables['site_category'] = 'PARTY! This displays the field we need';
      /**
       * The above line is just an example.
       *
       * You would replace it with the codes to load the target node:
       *   $target_node = node_load($n->field_site_category[......]);
       *
       * Display the title of the target node
       *   $variables['site_category'] = $target_node->title;
       */
    }
  }
}

page.tpl.php

<?php if (isset($site_category)): ?>
  <?php print $site_category; ?>
<?php endif; ?>

I added some screenshots of the outputs:

When viewing a node page

Page display if it's a node page

When not viewing a node page

Page display if it's not a node page

Also to get the array keys inside variables, there is a module called Devel. Install it like any module and use its helper function dsm($your_var). This displays the contents of the variable in a pleasant manner.

  • I really like and appreciate the succinctness of your answer. However, when I put this in my template.php file of my theme, I get the error message: Notice: Trying to get property of non-object in bricks_preprocess_page (bricks is the name of my theme) – Allan Jun 14 '15 at 11:48
  • Thank you! :) Ah! Maybe the URL or page that you're accessing has no $variables['id'] set or no field you are trying to access like a different content type. So node_load would return NULL thus $n->body['und'][0]['value'] would be non-existent. I updated my answer with a condition for both the function and the page.tpl.php file. – Leolando Tan Jun 14 '15 at 13:52
  • I added output from Devel to my original question. It appears the field is there but perhaps I not referencing it correctly? – Allan Jun 14 '15 at 15:16
  • Nice one! So it should be $variables['site_category'] = $n->field_site_category['und'][0]['value'] but to be use what to place on the "['value']" portion, expand the "0" portion to see what are the available array keys inside. – Leolando Tan Jun 14 '15 at 15:24
  • I did try setting it as you described, but it didn't work, so I created some "breakpoints." See above. Also, I want to say how appreciative I am of you taking time to assist. – Allan Jun 15 '15 at 16:08

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.