function rate_views_pre_render in Rate 8.2
Implements hook_views_pre_render().
File
- ./
rate.module, line 443 - Hook implementation code for the Rate module.
Code
function rate_views_pre_render(ViewExecutable $view) {
// Used for the blocks, which show the results of rate widget votings.
// Currently used for nodes.
if (in_array($view
->id(), [
'rate_widgets_results',
])) {
if (in_array($view->current_display, [
'node_summary_block',
])) {
$widget_id = $view->args[2];
$widget = \Drupal::service('entity_type.manager')
->getStorage('rate_widget')
->load($widget_id);
$options = isset($widget) ? $widget
->get('options') : [];
$template = isset($widget) ? $widget
->get('template') : '';
$labels = [];
// Add missing options if not all options were used to vote.
if (isset($view->total_rows) && $view->total_rows < count($options) && $view->total_rows > 0) {
$options = $template == 'fivestar' ? array_reverse($options) : $options;
foreach ($options as $key => $option) {
$new_row = new $view->result[0]();
$new_row->_entity = $view->result[0]->_entity;
// Set the variables.
$new_row->index = $key;
$new_row->votingapi_vote_value = $options[$key]['value'];
$new_row->votingapi_vote_value_1 = 0;
$new_row->node_field_data_votingapi_vote_nid = $view->result[0]->node_field_data_votingapi_vote_nid;
$temp[$key] = $new_row;
}
foreach ($view->result as $key => $value) {
$vote_value = $value->votingapi_vote_value;
$temp_key = array_search($vote_value, array_column($temp, 'votingapi_vote_value'));
if (isset($temp_key)) {
$temp[$temp_key] = $value;
}
}
$view->result = $temp;
}
foreach ($view->result as $row_num => $row_data) {
// Remove the empty rows.
$vote_value = $row_data->_entity
->getValue();
if ($vote_value == 0) {
unset($view->result[$row_num]);
$view->total_rows = $view->total_rows - 1;
}
else {
$vote_value = $row_data->votingapi_vote_value;
$option_key = array_search($vote_value, array_column($options, 'value'));
$row_data->votingapi_vote_value = (string) $options[$option_key]['label'];
}
}
}
}
}