0

I have been building a site in Drupal 7. I have the content type "Basic Page" but added taxonomy terms for the user to be able to select "Standard" or "Full Width" page.

If the user selects "Full Width" the page.tpl.php file has some code which recognises the value chosen for the page removes a sidebar (usually seen on the "Standard" page).

It is working perfectly on the frontend but has an error in the backend.

Here is the code in the page.tpl.php page:

<?php $field_page_width = field_view_field('node', $node, 'field_page_width'); ?>

            <?php if ($field_page_width['#items'][0]['taxonomy_term']->name == 'Standard') { ?>         

                <div class="col-xs-12 col-sm-12 col-md-8 col-lg-8 internal_content">

                    <?php print render($page['content']); ?>

                </div>

                <div class="hidden-xs hidden-sm hidden-md col-lg-1"></div>

                <div class="col-xs-12 col-sm-12 col-md-4 col-lg-3 side_content">

                    <?php print render($page['side_content']); ?>               

                </div>

                <div class="clearfix"></div>

            <?php } else { ?>

                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 internal_content">

                    <?php print render($page['content']); ?>

                </div>

                <div class="clearfix"></div>

            <?php } ?>

I am receiving this error:

Notice: Undefined variable: node in include() (line 76 of /home/public_html/sites/all/themes/zen/child_theme/templates/page.tpl.php).

Why do I get this error? Do I need to have something which establishes $node in the template.php file?

0

The page.tpl.php template file will receive a $node variable only if the user is visiting a node page; in other cases, it will not get any $node variable, even if the template file is still used (if a module doesn't suggest another template file that takes the precedence).
In fact, the documentation for page.tpl.php clearly describes the $node variable as follows. (Emphasis is mine.)

$node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).

Before accessing $node, you should be sure it has been set.

if (isset($node)) {
  $field_page_width = field_view_field('node', $node, 'field_page_width');
}

Also, field_view_field() is used to render a field. If you just need to get its value, you should use field_get_items().

if (isset($node)) {
  $items = field_get_items('node', $node, 'field_page_width');
}
  • Thank you. I've changed my templating to node-- rather than page-- and everything working perfectly! – user3711642 Jul 25 '16 at 14:58

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.