119 lines
3.9 KiB
PHP
119 lines
3.9 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @file
|
|
* Contains votingapi_widgets.module..
|
|
*/
|
|
|
|
use Drupal\Core\Routing\RouteMatchInterface;
|
|
use Drupal\field\Entity\FieldStorageConfig;
|
|
use Drupal\Core\Entity\EntityTypeInterface;
|
|
use Drupal\Core\Field\BaseFieldDefinition;
|
|
use Drupal\Core\Form\FormStateInterface;
|
|
use Drupal\field\Entity\FieldConfig;
|
|
|
|
/**
|
|
* Implements hook_help().
|
|
*/
|
|
function votingapi_widgets_help($route_name, RouteMatchInterface $route_match) {
|
|
switch ($route_name) {
|
|
// Main module help for the votingapi_widgets module.
|
|
case 'help.page.votingapi_widgets':
|
|
$output = '';
|
|
$output .= '<h3>' . t('About') . '</h3>';
|
|
$output .= '<p>' . t('Voting API Widgets') . '</p>';
|
|
return $output;
|
|
|
|
default:
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_base_field_info().
|
|
*/
|
|
function votingapi_widgets_entity_base_field_info(EntityTypeInterface $entity_type) {
|
|
// Add the field_name as a base field.
|
|
if ($entity_type->id() != 'vote') {
|
|
return;
|
|
}
|
|
$fields = [];
|
|
|
|
$fields['field_name'] = BaseFieldDefinition::create('string')
|
|
->setLabel(t('Field name'))
|
|
->setName('field_name')
|
|
->setRevisionable(FALSE)
|
|
->setRequired(FALSE)
|
|
->setDescription(t('Holds the field name.'))
|
|
->setPropertyConstraints('value', ['Length' => ['max' => FieldStorageConfig::NAME_MAX_LENGTH]]);
|
|
|
|
return $fields;
|
|
}
|
|
|
|
/**
|
|
* Implements hook_form_FORM_ID_alter().
|
|
*/
|
|
function votingapi_widgets_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state) {
|
|
if ($form_state->getFormObject()->getEntity()->bundle() == 'voting_api_field') {
|
|
// We only support posting one comment at the time so it doesn't make sense
|
|
// to let the site builder choose anything else.
|
|
$form['cardinality_container']['cardinality']['#default_value'] = 1;
|
|
$form['cardinality_container']['#access'] = FALSE;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_entity_type_build().
|
|
*/
|
|
function votingapi_widgets_entity_type_build(array &$entity_types) {
|
|
$plugins = \Drupal::service('plugin.manager.voting_api_widget.processor')->getDefinitions();
|
|
foreach ($plugins as $plugin_id => $definition) {
|
|
$entity_types['vote']->setFormClass('votingapi_' . $plugin_id, 'Drupal\votingapi_widgets\Form\BaseRatingForm');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme_suggestions_alter().
|
|
*/
|
|
function votingapi_widgets_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
|
|
if ($hook == 'votingapi_widgets_summary') {
|
|
$entity = $variables['vote'];
|
|
$content = \Drupal::service('entity_type.manager')->getStorage($entity->getVotedEntityType())->load($entity->getVotedEntityId());
|
|
$fieldSettings = FieldConfig::loadByName($content->getEntityTypeId(), $content->bundle(), $variables['field_name']);
|
|
$plugin = $fieldSettings->getSetting('vote_plugin');
|
|
|
|
$suggestions[] = $hook . '__' . $plugin;
|
|
$suggestions[] = $hook . '__' . $plugin . '__' . $entity->field_name->value;
|
|
$suggestions[] = $hook . '__' . $plugin . '__' . $content->getEntityTypeId();
|
|
$suggestions[] = $hook . '__' . $plugin . '__' . $content->getEntityTypeId() . '__' . $content->bundle() . '__' . $entity->field_name->value;
|
|
$suggestions[] = $hook . '__' . $plugin . '__' . $content->getEntityTypeId() . '__' . $content->bundle();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Implements hook_theme().
|
|
*/
|
|
function votingapi_widgets_theme() {
|
|
return [
|
|
'votingapi_widgets_summary' => [
|
|
'variables' => [
|
|
'results' => [],
|
|
'vote' => NULL,
|
|
'field_name' => NULL,
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Implements hook_preprocess_HOOK() for the voting_widgets_summary template.
|
|
*/
|
|
function votingapi_widgets_preprocess_votingapi_widgets_summary(array &$variables) {
|
|
// If all votes have been deleted the vote_field_count variable is zero
|
|
// instead of a default empty array.
|
|
// Here we set vote_field_count to 0 if the results array is empty so it can
|
|
// be more easily checked in the Twig template.
|
|
if (empty($variables['results'])) {
|
|
$variables['results']['vote_field_count'] = 0;
|
|
}
|
|
}
|