1

I have variable in the node preprocessor:

function theme_preprocess_node(&$vars , $hook) {
    $vars['default_product_price'] = $vars['elements']['product:commerce_price'][0]['#markup'];
}

Currently, the variable 'default_product_price' is not available in the region--sidebar-second.tpl.php file. How would i make this variable available in region template file or page template file? Thank You

2 Answers 2

3

The template files for nodes and pages don't share any variable; that is true also for their preprocess and process functions.

Assuming we are talking of a node field implemented with the field API, then what follows applies:

  • The page preprocess and process functions have access to $variables['node'] which contains the node object, in the case the page is a node page

    function mytheme_preprocess_page(&$variables) {
      if (!empty($variables['node'])) {
        $node = $variables['node'];
        // …
      }
    }
    
  • The function to access the content of a entity field is field_get_items(), which returns its raw content

  • The function to render a entity field is field_view_value()

The following code should work.

  function mytheme_preprocess_page(&$variables) {
    if (!empty($variables['node'])) {
      $output = '';
      $node = $variables['node'];
      $items = field_get_items('node', $node, 'field name');
      foreach ($items as $item) {
        $field_output = field_view_value('node', $node, 'field name', $item);
        $output .= render($field_output);
      }
      // Set $variables with the value you need.
    }
  }

Unfortunately, $variables['view_mode'] is passed only to the node preprocess/process functions; that value would be helpful to field_view_value(), as its last parameter is the view mode.

0
0

I'm not sure in what I'm going to say, but maybe you can use the process hook instead of the preprocess, it should be available then.

1
  • It doesn't change: Node preprocess/process functions are going to be called before the ones for the page.
    – apaderno
    Aug 17, 2013 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.