Search code examples
phpdrupaldrupal-7drupal-theming

Warning: Cannot use a scalar value as an array in include()


In order to theme a specific page in my site, I created a file called node--2.tpl.php. Per some other tutorials I read, I added this to my template.php file:

function mtheme_preprocess_node(&$vars) {
  if (request_path() == 'node/2') {
    $vars['theme_hook_suggestions'][] = 'node__2';
  }
}

On this page, I wanted the region called schools_landing to be rendered. As such, node--2.tpl.php looks like this and nothing else:

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

After doing so, I began seeing the following error messages in the top of the administrator overlay:

Warning: Cannot use a scalar value as an array in include() (line 1 of /home/something/public_html/project/sites/all/themes/mtheme/node--2.tpl.php).

Additionally, I can write text in the node--2.tpl.php file and it displays fine (instead of the default page content), but I can't get blocks to render inside of the region at all. If I assign a block to the schools_landing block, I see nothing on the page.

  1. Is this the right process to define custom content on a specific page?
  2. How can I fix the error causing the scalar value as an array error message?
  3. How can I get my blocks to begin rendering in the region?

Solution

  • In a node template, $page is a boolean value, not an array. That is the reason you get that error.
    template_preprocess_node() sets it with the following code.

    $variables['page']      = $variables['view_mode'] == 'full' && node_is_page($node);
    

    It is hook_preprocess_page() that gets the variable $page with the value you are expecting.
    template_preprocess_page() contains the following code.

      foreach (system_region_list($GLOBALS['theme']) as $region_key => $region_name) {
        if (!isset($variables['page'][$region_key])) {
          $variables['page'][$region_key] = array();
        }
      }
    

    page.tpl.php describes $page as:

    Regions:

    • $page['help']: Dynamic help text, mostly for admin pages.
    • $page['highlighted']: Items for the highlighted content region.
    • $page['content']: The main content of the current page.
    • $page['sidebar_first']: Items for the first sidebar.
    • $page['sidebar_second']: Items for the second sidebar.
    • $page['header']: Items for the header region.
    • $page['footer']: Items for the footer region.

    Extra regions can be implemented from themes.

    As side note, template_preprocess_node() already suggests the following template names.

      $variables['theme_hook_suggestions'][] = 'node__' . $node->type;
      $variables['theme_hook_suggestions'][] = 'node__' . $node->nid;
    

    There is no need to suggest them for your theme, or in a custom module.