0

I want to overwrite the $link['text'] value in theme('rate_button', $link['text'], $link['href'], 'rate-btn'), a theme function defined from the Rate module, under a specific content type in template.php. Is there a way to get that? How can I do that?

function rate_preprocess_rate_widget(&$variables) {
  extract($variables);

  $buttons = array();
  foreach ($links as $link) {
    $button = theme('rate_button', $link['text'], $link['href'], 'rate-btn');
    $buttons[] = $button;
  }
  $variables['buttons'] = $buttons;

  $info = array();
  if ($mode == RATE_CLOSED) {
    $info[] = t('Voting is closed.');
  }
  if ($mode != RATE_COMPACT && $mode != RATE_COMPACT_DISABLED) {
    if ($results['user_vote']) {
      $info[] = t('You voted \'@option\'.', array('@option' => t($results['user_vote'])));
    }
  }
  $variables['info'] = implode(' ', $info);
}

This is the code of theme_rate_button().

function theme_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 . '" rel="nofollow" href="' . htmlentities($href) . '">' .
      check_plain($text) .
      '</a>';
  }
}

The reason I am asking is that I have installed the rate module, and created the emotion vote (only one), which is used in two content types ("story", and "company"). In the "story" content type, the vote button should show two buttons labelled "best" and "bad," but in the "company" content type I want to set the buttons to "up" and "down."

The local variable $links used in rate_preprocess_rate_widget() contain the data for two buttons, of which I want to change the label basing on the content type of the node where those buttons appear.

1
  • many thanks! kiamlaluno, you make me question clearly
    – enjoylife
    Apr 21, 2011 at 9:14

2 Answers 2

3

You will need to implement mytheme_preprocess_rate_widget() and write your own version.

Notice how $variables['buttons'] is set halfway down the function in the original question. Since preprocess functions for a particular theme ALL get run, you can't (that I know of) stop the rate module running its preprocess function, but by re-assigning $variables['buttons'] in your own mytheme_preprocess_rate_widget() function, you will be able to control the output there. I would suggest something like:

function mymodule_preprocess_rate_widget(&$vars) {
  extract($vars);

  $buttons = array();
  foreach ($links as $link) {
    $button = theme('rate_button', 'my custom text here', $link['href'], 'rate-btn');
    $buttons[] = $button;
  }
  $vars['buttons'] = $buttons;
}
5
  • thank you,but my $link['text'] have two values. how to alter the code?
    – enjoylife
    Apr 20, 2011 at 9:01
  • i put your code in the template.php, but there is no effect.
    – enjoylife
    Apr 20, 2011 at 14:22
  • If you want to pass two values to theme_rate_button, you would need to alter the implementation of theme_rate_button. Luckily you can override this too since it's a theme function. Probably better to ask a separate question for that. Apr 20, 2011 at 14:42
  • Like any change to the theme system, once you add the code I suggested above, you will need to clear the theme registry (clear the cache if in doubt) to get this to start working. Apr 20, 2011 at 14:43
  • 2
    @enjoylife - I think you'll probably need to learn a bit about php functions and global variables before doing this, this type of thing tends to be difficult for people who are in the beginning stage of learning php. You'll probably want to find someone who knows php to do this for you.
    – James
    Apr 20, 2011 at 19:25
1

enjoylife, in the other thread on this topic you asked me to show you how to use hook_nodeapi in order to create a global variable containing the node type. I should add: this assumes you're displaying a "node page" - or even more specifically, a page with just one node - since if you are loading more than one node, there's no way of telling "which" node you are getting without a bit more programming. You say you already have a knowledge of php for Drupal, so I'll assume some of this basic knowledge - if you don't, you could, e.g., find a tutorial on how to create a basic Drupal module - see http://drupal.org/node/206753. Creation of basic Drupal modules is a very common task - intermediate Drupal web builders commonly create a specialized module for each site they build. I'd also recommend reading a tutorial on using hook_nodeapi (see http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_nodeapi/6 for api) - this is a "cornerstone" of many simple Drupal modules. Learning about hook_nodeapi is also good, since it is a good introduction to how Drupal's hook mechanism operates - it may sound a bit counter-intuitive at first, but it is easy enough to understand, and it's pretty much the basis of all Drupal modules you'll ever write.

First you create & install a module called mymodule. Then, inside mymodule.module:

function mymodule_nodeapi($node, $op) { 
  global $nodeType;
  //if next line is uncommented, this will always be set to first node loaded (or other $op)
  //if(isset($nodeType)) return;
  $nodeType = $node->type;
}

Inside your voting theme function or preprocess function (as Chris Cohen has provided in his answer):

global $nodeType;
// use $nodeType after it's globalized to modify the text

I hope this helps. If it doesn't make much sense, it probably will once you've read more about how Drupal hooks work.

2
  • James,many many thanks. you made me know a lot. but i test about what you told me. it couldn't work. this is my code function mymodule_nodeapi(&$node, $op) { global $nodeType; $nodeType = $node->type; } in template.php. function mytheme_preprocess_page(&$vars){ global $nodeType; if($nodeType=='page'){ $vars['title'] = 'test'; } } i have cleared the cache, why when i viewed a page, its title not test.
    – enjoylife
    Apr 22, 2011 at 3:12
  • try getting Drupal for Firebug, and then dumping $nodeType at various points. (or just print $nodeType; - but sooner or later, you'll want Drupal for Firebug)
    – James
    Apr 22, 2011 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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