function hook_rate_widget_options_alter in Rate 8.2
Alter the options of a rate widget.
Override the options (value, label, class) of a rate widget. The options are used to generate the inputs for the rate widget. This hook is called before the rate form is generated.
Parameters
array $options: An array of rate widget options.
string $entity_type: The voted entity type.
string $entity_bundle: The voted entity bundle.
int $entity_id: The voted entity id.
string $rate_widget: The rate widget being used for voting.
Drupal\rate\Entity\RateWidget $settings: The rate widget config entity and settings.
int $user_id: The user id casting the vote.
1 invocation of hook_rate_widget_options_alter()
- RateWidgetBase::getForm in src/
Plugin/ RateWidgetBase.php - Gets the widget form as configured for given parameters.
File
- ./
rate.api.php, line 63 - Provides hook documentation for the Rate module.
Code
function hook_rate_widget_options_alter(array &$options, $entity_type, $entity_bundle, $entity_id, $rate_widget, $settings, $user_id) {
// Trigger only on articles and specific widgets.
if ($rate_widget == 'test_rate_widget' && $entity_bundle == 'article') {
foreach ($options as $key => $option) {
// Increase the value for each option by 10.
$options[$key]['value'] = $option['value'] * 10;
// Change the label of each option.
$options[$key]['label'] = $option['label'] . '-test';
// Add a class.
$options[$key]['class'] = $option['class'] . ' test-class';
}
}
}