By AstaC on
OK - this is propably a really, really simple question. But not for me. I use Bookreviewmodule. In this you can rate books. The code for that is like this:
$form['book_infos']['rating'] = array(
'#type' => 'select',
'#title' => t('Rating'),
'#default_value' => $node->rating,
'#options' => (array('<' . t('none') . '>') + drupal_map_assoc(range(1, 10))),
'#description' => t('Score of the book on a 1 to 10 scale.'),
);*/
I would like to have words instead of numbers. How do I change the code. For example I want it to
be
- Rate-
Bad
Average
Good
Very good
Instead av a range 1-10.
Anyone who can tell me how to write that in this code?
Comments
it's not that difficult, but it's not easy either
We need to stay compatible with all other code, so we only change this line:
'#options' => (array('<' . t('none') . '>') + drupal_map_assoc(range(1, 10))),Drupal_map_assoc is able to do two things;
1. make an array associative by itself
2. make an array associative by callback function
As used here; drupal_map_assoc transforms the array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) which for PHP is array( 0=>1, 1=>2, ... 9=>10) to array(1=>1, 2=>2, ..., 10=>10)
When you change the line to:
'#options' => (array('<' . t('none') . '>') + drupal_map_assoc(range(1, 10), 'my_callback_function')),(where my_callback_function is a correct function name in either a separate module or the module you are editing)
and the function my_callback_function does:
the resulting array would be: array(0 => '', 1 => 'Very bad, 2=> etc etc
(The reason i list 1 through 10 here is because the original code expects it, but it's not obligatory, see my note further down)
This is, however, not the best way to alter existing code.
The Drupal way is to create a module with a form alter function to change the form value $form['book_infos']['rating']['#options'].
this would look someting like:
-- File: book_review_alter.info --
(the name bookreview in the dependencies line must be identical to the modulename of the module you are altering the form for)
--File: book_review_alter.module--
Note: Remember, this just alters the form, but not the presentation of the rating in, for example, a node view. You'll need to dig a bit to find where the ratings are 'read' for viewing it on screen and make similar adjustments.
If you rewrite EVERY spot the rating is used to use your names, it doesn't matter how much or little ratings you define.
I don't know the book review module, so I can't tell you on how many points you need to hook. The idea is hopefully clear to help you a few steps in the right direction.
Remon.
Thank you
This was a bit to think about. Now I'll start trying it out :)
It works so far!
Really nice. If I may bother you once more for the output. This is the line for the output. Do you have an idea about this to? That about a new module is propably more than I can manage.
I guess it is something with if else, or should it be a function here to? This was really learning.
Display of textual ratings
First of all, allow me to apologise for the delayed reaction.
Where did you find this piece of code?
If it's inside a theme_ function, you can alter it by creating a theme override function in your template.php (theme folder).
If not, you probably need to rewrite the $node->rating variable.
Most probably you'll need to implement the hook_nodeapi() hook.
now, the
line alters the rating to our textual rating.
BUT!
1. this shouldn't be done on node edit pages (the form expects a number, not a text)
2. this should be done AFTER the bookreview adds it to the $node (we need to increase the weight of the module)
solution to 1:
solution to 2:
as I have not seen an interface for this, you'll have to change the database itself.
I would suggest this piece of code be in the book_review_alter.install file inside the book_review_alter_install function, see the drupal api for hook_install() implementation. This work, however, only upon first activation of the module.
notes:
a. 'bookreview' should be the name of the module we're altering
b. this query hasn't been tested by me :P, done by heart, but should work. The trick here is to join a table on itself. Alternatively you could do 2 queries; read the weight of the original module, then write the new weight of the alter module. I was taught to minimise database access so 1 query is better than 2 :P. Naming: st = system target, ss = system source.
c. Never tried this, but you could create an update function. Don't know how, just rip it from a module that does have update functions in its .install file :P
Remon.