2

I have installed both the Views and Rate modules. In the views-view-fields.tpl.php file, I put the following code.

$node = node_load($row->nid);   
echo $node->content['rate_poll']['#value'];
echo $node->title;

There is no output for $node->content['rate_poll']['#value'], while the code outputs the node title. Why?

How can I output $node->content['rate_poll']['#value'] in views-view-fields.tpl.php, or other Views files?

1 Answer 1

0

The code you wrote doesn't work because $node->content['rate_poll']['#value'] is only present when the node is being viewed. The code that adds $node->content['rate_poll']['#value'] is in the implementation of hook_nodeapi(), and the code is executed when the operation is "view."

/**
 * Implements hook_nodeapi().
 */
function rate_nodeapi(&$node, $op, $a3, $a4) {
  // Adding the form to the node view
  if ($op == 'view') {
    $widgets = rate_get_active_widgets('node', $node->type, $a3);

    foreach ($widgets as $widget_id => $widget) {
      $widget_name = 'rate_' . $widget->name;
      _rate_check_widget($widget);

      $display_mode = $a3 ? $widget->teaser_display_mode : $widget->node_display_mode;

      $widget_code = array(
        '#value' => rate_generate_widget($widget_id, 'node', $node->nid, $display_mode),
        '#weight' => $widget->node_display == RATE_DISPLAY_ABOVE_CONTENT ? 0 : 50,
      );

      if ($widget->node_display != RATE_DISPLAY_ABOVE_CONTENT && $widget->node_display != RATE_DISPLAY_BELOW_CONTENT) {
        $node->$widget_name = $widget_code;
      }
      else {
        $node->content[$widget_name] = $widget_code;
      }
    }
  }
}

For the code to work, you should change it to the following one.

$node = node_load($row->nid);
node_invoke_nodeapi($node, 'view', FALSE, TRUE);
echo $node->content['rate_poll']['#value'];
echo $node->title;

node_invoke_nodeapi() invokes any implementation of hook_nodeapi(), using its first parameter as operation value. Once the function returns, $node will contain any $node->content value added by the modules, including $node->content['rate_poll']['#value'].

1
  • you're my hero! i have thought this problem long time.many many thanks.
    – enjoylife
    Commented Apr 12, 2011 at 10:59

Your Answer

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

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