Hello I have had a tough time styling rate via css. The first issue was that the ID was not unique and the second was that i needed to style the active vote. No doubt I have a pretty particular scenario but I feel that adding a more unique ID and an active class would benefit others and the module.

Unique ID

The problem I have is that I am using x2 "yes / no" rating widgets and while on initial render they have a unique count 1 - 4, on an ajax refresh it only refresh a pair so I end up with 1,2 and 1,2.

To resolve this issue I replaced the "$id" with the "value".

// Added "value" to preprocess in function rate_preprocess_rate_template_*
  $button = theme('rate_button', array('text' => $link['text'], 'href' => $link['href'], 'class' => 'rate-yesno-btn ' . $voted . ' ' . $text, 'value' => $link['value']));

// Swapped id for value in function theme_rate_button
$value = $variables['value'];
return '<a class="' . $classes . '" id="rate-button-' . $value . '" rel="nofollow" href="' . htmlentities($href) . '" title="' . check_plain($text) . '">' . check_plain($text) . '</a>';

Active Class

I wasn't able to figure out how to do an active class in the code and used jQuery, for reference I used:

    $('#rate-node a').click(function() {
        	var Selected = $(this).attr('id'); 
              $(this).parents('.field-item').addClass(Selected);
});

I then added some css, however I dont like that I had to add the class so high up (due to the refresh)

However I think the module itself should put an "active-vote" class on the item selected in the voting. If you can point me in the correct direction on how this is possible Ill submit a patch if you like?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

leschekfm’s picture

Status: Active » Needs review
FileSize
1.98 KB

I had a similar problem with outputting multiple of the same widget. as the id is the same for each instance of the widget you cannot select it with jQuery, because jquery.find() stops at the first occurence of an id.

I found a solution to this by using the id of a widget as class and making the former id really unique by calling drupal_html_id. Furthermore this allows to refresh all widgets if you vote on one.

Please have a look at my patch. It should work against 7.x-1.3 and 7.x-1.x .

Rob230’s picture

I've not tried the patch because it's a bit old now (I'm on 7.x-1.6) but I think this should have been committed. If you have multiple vote widgets on a page then it will fail HTML validation due to duplicate HTML ids and only one of the widgets will get updated when you vote. Furthermore it cannot be changed by changing the template because this div container is added as #markup in the module.

For an example use case of why you would have the same widget twice on a page, on a homepage we have some views which show recently added nodes or highest voted nodes, and the same one could naturally appear in both lists.

The fix in leschekfm's patch makes a lot of sense.

jaydub’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
FileSize
1.65 KB

I've rerolled against the latest code in git which should be more or less 1.6.

mauritsl’s picture

Status: Needs review » Fixed

Patch is committed to 7.x-1.x and included in the 1.7 release. Thanks!

Rob230’s picture

Thanks.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

rjacobs’s picture

It's possible that this fix may not have been complete in implementation. Though the widget id is now unique, there may be circumstances when the AHAH js cannot find the new id, and form refreshes are not triggered. In other words it looks like this fix introduced a new bug.

The details are here: #2213317: Manual use of rate_generate_widget leads to mis-matched widget ids, breaking AHAH updates. Apparently triggerd by fix in Issue #1676746

Since this issue has a commit, it makes no sense to re-open it. However, perhaps someone will be able to comment on this over in that new open issue.

quotesBro’s picture

Patch from https://www.drupal.org/node/2213317#comment-9284859 fixed this bug, thanks @rjacobs.

kiss.jozsef’s picture

The previous patch with the unique id brakes the ajax for the vote because in rate.js -> widget = $('.' + widget.attr('id')); and in the rate module we use the $div_class for both class and id but only for the id was added drupal_html_id().

_dcre_’s picture

Hello

I verify Joski's comment. I am facing exactly the same issue and the solution i managed to implement is exactly what Joski is proposing through his patch.
Is this going to be dealt with and included on a next release?