Calling rate_embed() only works if the relevant javascript is already included in the page, which isn't always the case.

Currently we're working around this by manually including the javascript ourselves - ideally this should be triggered by rate_embed since it's a dependency for rate_embed to actually work. Specifically, the following code (Found in rate.module, line 284) should be called in rate_embed:

  // Add generic javascript.
  $dest = drupal_get_destination();
  drupal_add_js(array(
    'rate' => array(
      'basePath' => url('rate/vote/js'),
      'destination' => $dest['destination'],
    )
  ), array('type' => 'setting'));
  drupal_add_js(drupal_get_path('module', 'rate') . '/rate.js');

I haven't prepared a patch since I'm not clear if that code should be moved from it's current location, or copied (Taking care not to include it twice, obviously). Happy to do so if there's some guidance on that.

(Note: In our case we're loading content via ajax so we also have to make the call to Drupal.attachBehaviours as well, but I don't consider that a bug)

Comments

mauritsl’s picture

That code is in rate_generate_widget(), which is called from rate_embed() too. Problem may be that inclusion is too late.

The following code fails in devel/php, but works when using in a page callback, for example:

$node = node_load(8);
$widget = rate_embed($node, 'thumbs_up_down');
drupal_set_message($widget);

This is problematic when implementing Rate in the theming layer (that's in fact wrong but not very unusual). Not sure what to do about it yet.

Anybody’s picture

Issue summary: View changes

I can confirm this problem still exists.

The AJAX widget triggers the vote action but the widget is not being replaced by the result when using rate_embed().
How can we properly fix it?

Anybody’s picture

Anybody’s picture

I finally found the clean working solution:

In the configuration of the rate widget select:

Do not add automatically

In node.tpl.php simply use:

// Display node rate widget
print render($rate_fivestar_node_general);

Which consists of $rate_NAMEOFYOURWIDGET which is being prepared by the module already. No need to use rate_embed().

Futhermore the JS in rate_embed() works cleanly as I found out. The problem just was, that Drupal adds --1 to the ID of the widget (if you add the widget a second time even if hidden by calling rate_embed()), which kills the class / id logic used in rate.js file so that the widget is not being replaced by the AJAX result cleanly.

I hope this helped you.

Anybody’s picture