Hey!

Would be cool if I could add a title attribute via the rate configuration.

Thanks

Yannick

CommentFileSizeAuthor
#13 rate-theming-940532.patch10.89 KBymakux
#5 rate.module.patch348 bytesfoxfabi
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

walker2238’s picture

Status: Active » Closed (fixed)

You can, just put theme_rate_button() into your template.php file and add the title. Use $text which will display the labels.

function MYTHEME_rate_button($text, $href, $class = NULL) {
  static $id = 0;
  $id++;

  $classes = 'rate-button';
  if ($class) {
    $classes .= ' ' . $class;
  }
  if (empty($href)) {
    // Widget is disabled or closed.
    return
      '<span class="' . $classes . '" id="rate-button-' . $id . '">' .
      check_plain($text) .
      '</span>';
  }
  else {
    return
      '<a class="' . $classes . '" id="rate-button-' . $id . '" title="' . $text . '" rel="nofollow" href="' . htmlentities($href) . '">' .
      check_plain($text) .
      '</a>';
  }
}
josh@pixael.com’s picture

Status: Closed (fixed) » Needs review

i think the meaning of the question was different... i had to insert the title above each fivestar widget
i found a "quick" solution changing the theme_rate_button function
it's very strange that the title is not printed by default because with more widget is different to understand what an user is voting...

this is my snippet

function phptemplate_rate_button($text, $href, $class = NULL) {
  static $id = 0;
  $id++;

  $widgets = variable_get(RATE_VAR_WIDGETS, array());
  if(!strstr($href, "widget_id")) {
    if(is_int(($id-1) / 5)) echo '<h4>' . $widgets[((($id-1) / 5) + 1)]->title . '</h4>';
  } else {
    if(is_int(($id-1) / 5)) echo '<h4>' . $widgets[$_GET['widget_id']]->title . '</h4>';  
  }  

  $classes = 'rate-button';
  if ($class) {
    $classes .= ' ' . $class;
  }
  if (empty($href)) {
    // Widget is disabled or closed.
    return
      '<span class="' . $classes . '" id="rate-button-' . $id . '">' .
      check_plain($text) .
      '</span>';
  }
  else {
    return
      '<a class="' . $classes . '" id="rate-button-' . $id . '" rel="nofollow" href="' . htmlentities($href) . '">' .
      check_plain($text) .
      '</a>';
  }
}
dreamdust’s picture

Title: Add title attribute support » How to display a title for widget?
Category: feature » support

Displaying a title is a theme issue, so I'm changing this issue category to "support request."

In response to j0sh's comment, I have to disagree that the lack of a default widget title is strange. The majority of rating widgets on the web do not have a title (youtube for example).

dreamdust’s picture

Status: Needs review » Closed (fixed)

Also, it looks like both j0sh and walker2238 have good solutions for this, so let's mark it "closed (fixed)."

foxfabi’s picture

FileSize
348 bytes

i have multiple voting options for node and views. also of the same type.
so i need to display the title of the widget in views and nodes.
could an option field be usefull: display title?
my patch, please review. may not be complete.

kvguser’s picture

#2 works perfectly, #5 - not at all (at least for me)

zazinteractive’s picture

#2 didn't work for me. It would only display a title on the first widget of the page.

IRoar’s picture

Hi. I agree with j0sh that it seems strange that this module doesn't support widget titles. I understand dreamdust's point that sites like youtube don't have titles displayed above their voting widget, but youtube doesn't rate each video on multiple criteria, where as this module does allow that. Anyways, that is not really important. I am posting to request a little help. I am not a coder, but I have muddled my way through pasting code and testing and retesting until I get things to work, but I am working on a site that I would like to do properly. So, I am wondering if someone could hold my hand through implementing j0sh's solution, because I think that is what I am looking to do (have a criterion listed above each 5 star rating option).

My problems are:
1) I don't know where exactly to paste the snippet that j0sh posted.
2)When this snippet is pasted, just to be clear, I am assuming that it doesn't require any deleting of the original module code.

That is all. Great module, thanks very much for the work!

IRoar’s picture

Drat, just noticed that this is a Drupal 6 fix, but I am using Drupal 7 so I guess you can ignore my previous post.

oxigen’s picture

None of the tricks above worked for me so I made my solution which is actually working!

rate.module

line 313:   $widget_html = theme($theme, $links, $results, $widget->mode, $just_voted, $content_type, $content_id);
to
  $widget_html = "<h4>".$widget->title."</h4>".theme($theme, $links, $results, $widget->mode, $just_voted, $content_type, $content_id);

Hope it helps for others...
Cheers.

bassem’s picture

any solutions for the rate widget title for drupal 7?

geert’s picture

This is what I did for the 7 version, in rate.module, line 325

$widget_html ="<h4 class='rate_title'>".$widget->title."</h4>".theme($theme, array('links' => $links, 'results' => $results, 'mode' => $widget->mode, 'just_voted' => $just_voted, 'content_type' => $content_type, 'content_id' => $content_id, 'display_options' => $display_options));

Basically, I added

 "<h4 class='rate_title'>".$widget->title."</h4>". 

in front of the theme part

It provides a workaround as It displays the "admin" title, where I am looking for a user-focused title

ymakux’s picture

therobyouknow’s picture

http://drupal.org/node/1891422

d7

Attachment Size
rate-theming-940532.patch 10.89 KB

To the patch maker (primarily or other folks please comment too):
I presume this patch enables us to override the output of rate in our own theme such that we can insert markup for a title above the five-star, for example?

i.e. the override code would go in our own theme, here, for example: sites/all/themes/mytheme/template.php

If so can you supply the function to override?

therobyouknow’s picture

j0sh - some advice requested please on your comment #2 ( https://drupal.org/comment/reply/940532/3779918 )
- where is this snippet placed? I placed in the template.php file in my theme - in path: sites/all/themes/mytheme/template.php where 'mytheme' is the name of my theme. I aldo renamed the function from phptemplate_rate_button to mytheme_rate_button - is this correct?
- what version of rate are you using? I am using 7.x-1.x-dev
- does your fix require patch rate-theming-940532.patch (comment #13 / https://drupal.org/node/940532#comment-6950932 / https://drupal.org/node/1891422 ) or if it does not require this patch will your fix still work with it?

When I tried placing your code in template.php as I described above I got the following errors below, specifically complaining about parameter 1 not being a string and missing argument 2 - these errors are unexpected to me as your function defines all three parameters that are required by the function prototype.

Warning: Missing argument 2 for mytheme_rate_button(), called in /var/www/mysite/rob/includes/theme.inc on line 1151 and defined in mytheme_rate_button() (line 3 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Notice: Undefined variable: href in mytheme_rate_button() (line 8 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1565 of /var/www/mysite/rob/includes/bootstrap.inc).
Warning: Missing argument 2 for mytheme_rate_button(), called in /var/www/mysite/rob/includes/theme.inc on line 1151 and defined in mytheme_rate_button() (line 3 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Notice: Undefined variable: href in mytheme_rate_button() (line 8 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1565 of /var/www/mysite/rob/includes/bootstrap.inc).
Warning: Missing argument 2 for mytheme_rate_button(), called in /var/www/mysite/rob/includes/theme.inc on line 1151 and defined in mytheme_rate_button() (line 3 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Notice: Undefined variable: href in mytheme_rate_button() (line 8 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1565 of /var/www/mysite/rob/includes/bootstrap.inc).
Warning: Missing argument 2 for mytheme_rate_button(), called in /var/www/mysite/rob/includes/theme.inc on line 1151 and defined in mytheme_rate_button() (line 3 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Notice: Undefined variable: href in mytheme_rate_button() (line 8 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1565 of /var/www/mysite/rob/includes/bootstrap.inc).
Warning: Missing argument 2 for mytheme_rate_button(), called in /var/www/mysite/rob/includes/theme.inc on line 1151 and defined in mytheme_rate_button() (line 3 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Notice: Undefined variable: href in mytheme_rate_button() (line 8 of /var/www/mysite/rob/sites/all/themes/mytheme/template.php).
Warning: htmlspecialchars() expects parameter 1 to be string, array given in check_plain() (line 1565 of /var/www/mysite/rob/includes/bootstrap.inc).

However I changed a few lines of your code to retrieve the argument parameters in slightly different approach and this remedied the problem as these errors did not now occur (however still not provided title above the rating):

<?php
// was: function phptemplate_rate_button($text, $href, $class = NULL) {
function cgsubboot_rate_button($variables) {
// fetch arguments like this - so that no errors appear:
  $text = $variables['text'];
  $href = $variables['href'];
  $class = NULL; // usually assigned $variables['class'];
  static $id = 0;
  $id++;

  $widgets = variable_get(RATE_VAR_WIDGETS, array());
  if(!strstr($href, "widget_id")) {
    if(is_int(($id-1) / 5)) echo '<h4>' . $widgets[((($id-1) / 5) + 1)]->title . '</h4>';
  } else {
    if(is_int(($id-1) / 5)) echo '<h4>' . $widgets[$_GET['widget_id']]->title . '</h4>';  
  }  

  $classes = 'rate-button';
  if ($class) {
    $classes .= ' ' . $class;
  }
  if (empty($href)) {
    // Widget is disabled or closed.
    return
      '<span class="' . $classes . '" id="rate-button-' . $id . '">' .
      check_plain($text) .
      '</span>';
  }
  else {
    return
      '<a class="' . $classes . '" id="rate-button-' . $id . '" rel="nofollow" href="' . htmlentities($href) . '">' .
      check_plain($text) .
      '</a>';
  }
}

?>

I see the widget title appear BUT near the top of the page and not close to the five star widget at all.
Also the five star widget has its styling removed so now appears as numbers 12345 but hovering on them restores the star graphic symbols.

(Testing on Chrome)

therobyouknow’s picture

The problem with the incorrect position of the title is that echo() function is being used, the title markup is not being output by the same method that the widget is being used to output - the other already existing markup is outputted as what function returns as its return value.

Also, this
_rate_button override gets called several times (up to 5) for each star printed on the screen. So not the ideal place to put display title functionality. Thoughts?

I think geert had a better idea to output the title markup along with the other markup, here: https://drupal.org/node/940532#comment-5094640

An even better solution would be to add a check box 'show title' in the edit form for the widget. I've made a start on this but now need to learn about how this is installed in the database AND also the database upgrade path for adding this extra boolean path if I make a patch with this feature.

Thoughts?