Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Defines selection, check box and radio button widgets for text and numeric fields.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@18
|
8 use Drupal\Core\Url;
|
Chris@0
|
9 use Drupal\Core\Entity\FieldableEntityInterface;
|
Chris@0
|
10 use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
|
Chris@0
|
11 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@0
|
12 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
13 use Drupal\field\FieldStorageConfigInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Implements hook_help().
|
Chris@0
|
17 */
|
Chris@0
|
18 function options_help($route_name, RouteMatchInterface $route_match) {
|
Chris@0
|
19 switch ($route_name) {
|
Chris@0
|
20 case 'help.page.options':
|
Chris@0
|
21 $output = '';
|
Chris@0
|
22 $output .= '<h3>' . t('About') . '</h3>';
|
Chris@18
|
23 $output .= '<p>' . t('The Options module allows you to create fields where data values are selected from a fixed list of options. Usually these items are entered through a select list, checkboxes, or radio buttons. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":options_do">online documentation for the Options module</a>.', [':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#', ':options_do' => 'https://www.drupal.org/documentation/modules/options']) . '</p>';
|
Chris@0
|
24 $output .= '<h3>' . t('Uses') . '</h3>';
|
Chris@0
|
25 $output .= '<dl>';
|
Chris@0
|
26 $output .= '<dt>' . t('Managing and displaying list fields') . '</dt>';
|
Chris@18
|
27 $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the list fields can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
|
Chris@0
|
28 $output .= '<dt>' . t('Defining option keys and labels') . '</dt>';
|
Chris@0
|
29 $output .= '<dd>' . t('When you define the list options you can define a key and a label for each option in the list. The label will be shown to the users while the key gets stored in the database.') . '</dd>';
|
Chris@0
|
30 $output .= '<dt>' . t('Choosing list field type') . '</dt>';
|
Chris@0
|
31 $output .= '<dd>' . t('There are three types of list fields, which store different types of data: <em>float</em>, <em>integer</em> or, <em>text</em>. The <em>float</em> type allows storing approximate decimal values. The <em>integer</em> type allows storing whole numbers, such as years (for example, 2012) or values (for example, 1, 2, 5, 305). The <em>text</em> list field type allows storing text values. No matter which type of list field you choose, you can define whatever labels you wish for data entry.') . '</dd>';
|
Chris@0
|
32 $output .= '</dl>';
|
Chris@0
|
33 return $output;
|
Chris@0
|
34 }
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.
|
Chris@0
|
39 */
|
Chris@0
|
40 function options_field_storage_config_update(FieldStorageConfigInterface $field_storage) {
|
Chris@0
|
41 drupal_static_reset('options_allowed_values');
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Implements hook_ENTITY_TYPE_delete() for 'field_storage_config'.
|
Chris@0
|
46 */
|
Chris@0
|
47 function options_field_storage_config_delete(FieldStorageConfigInterface $field_storage) {
|
Chris@0
|
48 drupal_static_reset('options_allowed_values');
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Returns the array of allowed values for a list field.
|
Chris@0
|
53 *
|
Chris@0
|
54 * The strings are not safe for output. Keys and values of the array should be
|
Chris@0
|
55 * sanitized through \Drupal\Core\Field\AllowedTagsXssTrait::fieldFilterXss()
|
Chris@0
|
56 * before being displayed.
|
Chris@0
|
57 *
|
Chris@0
|
58 * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
|
Chris@0
|
59 * The field storage definition.
|
Chris@0
|
60 * @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
|
Chris@0
|
61 * (optional) The specific entity when this function is called from the
|
Chris@0
|
62 * context of a specific field on a specific entity. This allows custom
|
Chris@0
|
63 * 'allowed_values_function' callbacks to either restrict the values or
|
Chris@0
|
64 * customize the labels for particular bundles and entities. NULL when
|
Chris@0
|
65 * there is not a specific entity available, such as for Views filters.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @return array
|
Chris@0
|
68 * The array of allowed values. Keys of the array are the raw stored values
|
Chris@0
|
69 * (number or text), values of the array are the display labels.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @see callback_allowed_values_function()
|
Chris@0
|
72 */
|
Chris@0
|
73 function options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL) {
|
Chris@0
|
74 $allowed_values = &drupal_static(__FUNCTION__, []);
|
Chris@0
|
75
|
Chris@0
|
76 $cache_keys = [$definition->getTargetEntityTypeId(), $definition->getName()];
|
Chris@0
|
77 if ($entity) {
|
Chris@0
|
78 $cache_keys[] = 'entity';
|
Chris@0
|
79 }
|
Chris@0
|
80 $cache_id = implode(':', $cache_keys);
|
Chris@0
|
81
|
Chris@0
|
82 if (!isset($allowed_values[$cache_id])) {
|
Chris@0
|
83 $function = $definition->getSetting('allowed_values_function');
|
Chris@0
|
84 // If $cacheable is FALSE, then the allowed values are not statically
|
Chris@0
|
85 // cached. See options_test_dynamic_values_callback() for an example of
|
Chris@0
|
86 // generating dynamic and uncached values.
|
Chris@0
|
87 $cacheable = TRUE;
|
Chris@0
|
88 if (!empty($function)) {
|
Chris@0
|
89 $values = $function($definition, $entity, $cacheable);
|
Chris@0
|
90 }
|
Chris@0
|
91 else {
|
Chris@0
|
92 $values = $definition->getSetting('allowed_values');
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 if ($cacheable) {
|
Chris@0
|
96 $allowed_values[$cache_id] = $values;
|
Chris@0
|
97 }
|
Chris@0
|
98 else {
|
Chris@0
|
99 return $values;
|
Chris@0
|
100 }
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 return $allowed_values[$cache_id];
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 /**
|
Chris@0
|
107 * Implements hook_field_storage_config_update_forbid().
|
Chris@0
|
108 */
|
Chris@0
|
109 function options_field_storage_config_update_forbid(FieldStorageConfigInterface $field_storage, FieldStorageConfigInterface $prior_field_storage) {
|
Chris@0
|
110 if ($field_storage->getTypeProvider() == 'options' && $field_storage->hasData()) {
|
Chris@0
|
111 // Forbid any update that removes allowed values with actual data.
|
Chris@0
|
112 $allowed_values = $field_storage->getSetting('allowed_values');
|
Chris@0
|
113 $prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
|
Chris@0
|
114 $lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
|
Chris@0
|
115 if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
|
Chris@0
|
116 throw new FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', ['@field_name' => $field_storage->getName()]));
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * Checks if a list of values are being used in actual field values.
|
Chris@0
|
123 */
|
Chris@0
|
124 function _options_values_in_use($entity_type, $field_name, $values) {
|
Chris@0
|
125 if ($values) {
|
Chris@17
|
126 $result = \Drupal::entityQuery($entity_type)
|
Chris@0
|
127 ->condition($field_name . '.value', $values, 'IN')
|
Chris@0
|
128 ->count()
|
Chris@0
|
129 ->accessCheck(FALSE)
|
Chris@0
|
130 ->range(0, 1)
|
Chris@0
|
131 ->execute();
|
Chris@0
|
132 if ($result) {
|
Chris@0
|
133 return TRUE;
|
Chris@0
|
134 }
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 return FALSE;
|
Chris@0
|
138 }
|