forked from erik-seifert/votingapi_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
votingapi_widgets.module
105 lines (93 loc) · 3.43 KB
/
votingapi_widgets.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?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 = array();
$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', array('Length' => array('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,
],
],
];
}