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