function rate_entity_view in Rate 8.2
Same name and namespace in other branches
- 8 rate.module \rate_entity_view()
Implements hook_entity_view().
File
- ./
rate.module, line 91 - Hook implementation code for the Rate module.
Code
function rate_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Rate widgets definitions.
$display_components = $display
->getComponents();
$widgets = \Drupal::service('entity_type.manager')
->getStorage('rate_widget');
$rate_widget_base_service = \Drupal::service('rate.vote_widget_base');
$entity_type = $entity
->getEntityTypeId();
$entity_bundle = $entity
->bundle();
$entity_id = $entity
->id();
foreach ($display_components as $component => $component_settings) {
// Check if we have a comment or node related widget.
if (substr($component, 0, 5) == 'rate_') {
if (substr($component, 0, 13) == 'rate_comment_') {
// This is for comments.
$widget_name = substr($component, 13);
}
elseif (substr($component, 0, 5) == 'rate_') {
// This is for nodes.
$widget_name = substr($component, 5);
}
else {
return;
}
// Generate the voting form for each widget.
$generate_widget = FALSE;
$widget = $widgets
->load($widget_name);
// Leftover rate extra field - core issue in #2903746 and #2903745.
if (!$widget) {
// Delete the leftover rate extra field.
$display
->removeComponent($component)
->save();
}
else {
$entities_enabled = $widget
->get('entity_types');
$comments_enabled = $widget
->get('comment_types');
}
// Generate the form only if the widget is enabled in the config entity.
if (isset($entities_enabled) && count($entities_enabled) > 0 && $entity_type != 'comment') {
if (in_array($entity_type . '.' . $entity_bundle, $entities_enabled)) {
$generate_widget = TRUE;
}
}
if (isset($comments_enabled) && count($comments_enabled) > 0 && $entity_type == 'comment') {
if (in_array($entity
->getCommentedEntityTypeId() . '.' . $entity
->getCommentedEntity()
->bundle(), $comments_enabled)) {
$generate_widget = TRUE;
}
}
if ($widget && $generate_widget === TRUE) {
$widget_template = $widget
->get('template');
$value_type = $widget
->get('value_type') ? $widget
->get('value_type') : 'percent';
$settings = $widget;
$vote_type = $widget_template == 'fivestar' ? $widget_template : 'updown';
$form = $rate_widget_base_service
->getForm($entity_type, $entity_bundle, $entity_id, $vote_type, $value_type, $widget_name, $settings);
$form_container = [
'rating' => [
'#theme' => 'container',
'#attributes' => [
'class' => [
'rate-widget',
$widget_template,
],
],
'#children' => [
'form' => $form,
],
],
'#attached' => [
'library' => [
'rate/unvote-helper',
'rate/w-' . $widget_template,
],
],
];
if (isset($build[$component])) {
array_unshift($build[$component], $form_container);
}
else {
$build[$component][] = $form_container;
}
$build['#cache']['tags'][] = 'vote:' . $entity
->bundle() . ':' . $entity
->id();
// Create a date field in each linked content entity (node).
// This field allows voting until the date set by the user.
// "Use deadline" must be checked in the widget voting settings.
$voting_settings = $widget
->get('voting');
$use_deadline = isset($voting_settings['use_deadline']) ? $voting_settings['use_deadline'] : 0;
if ($entity_type == 'node' && $use_deadline == 1) {
$field_name = 'field_rate_vote_deadline';
// Create the field to hold the date, if it does not exist yet.
if (empty($rate_date_storage = FieldStorageConfig::loadByName($entity_type, $field_name))) {
$rate_date_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [
'module' => [
'datetime',
$entity_type,
],
],
'entity_type' => $entity_type,
'type' => 'datetime',
'settings' => [
'datetime_type' => 'date',
],
'module' => 'datetime',
'locked' => FALSE,
'cardinality' => 1,
'translatable' => TRUE,
'persist_with_no_fields' => TRUE,
'custom_storage' => FALSE,
])
->save();
}
// Add the field to the entity, which has the rate widget enabled.
if (empty($rate_date_field = FieldConfig::loadByName($entity_type, $entity_bundle, $field_name))) {
$rate_date_field = FieldConfig::create([
'field_name' => $field_name,
'field_type' => 'datetime',
'langcode' => 'en',
'status' => TRUE,
'dependencies' => [
'config' => [
'field.storage.' . $entity_type . '.' . $field_name,
$entity_type . '.type.' . $entity_bundle,
],
'module' => [
'datetime',
],
],
'entity_type' => $entity_type,
'bundle' => $entity_bundle,
'label' => 'Rate vote deadline',
'description' => '',
'required' => FALSE,
'translatable' => FALSE,
'default_value' => [
'default_date_type' => 'now',
'default_date' => 'now',
],
])
->save();
}
// Add the rate date field to the form mode of the entity in question.
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Assign widget settings for the default form mode.
$display_repository
->getFormDisplay($entity_type, $entity_bundle)
->setComponent($field_name, [
'weight' => 300,
])
->save();
}
}
}
}
}