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