5 * Defines selection, check box and radio button widgets for text and numeric fields.
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;
15 * Implements hook_help().
17 function options_help($route_name, RouteMatchInterface $route_match) {
18 switch ($route_name) {
19 case 'help.page.options':
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>';
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>';
37 * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.
39 function options_field_storage_config_update(FieldStorageConfigInterface $field_storage) {
40 drupal_static_reset('options_allowed_values');
44 * Implements hook_ENTITY_TYPE_delete() for 'field_storage_config'.
46 function options_field_storage_config_delete(FieldStorageConfigInterface $field_storage) {
47 drupal_static_reset('options_allowed_values');
51 * Returns the array of allowed values for a list field.
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.
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.
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.
70 * @see callback_allowed_values_function()
72 function options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL) {
73 $allowed_values = &drupal_static(__FUNCTION__, []);
75 $cache_keys = [$definition->getTargetEntityTypeId(), $definition->getName()];
77 $cache_keys[] = 'entity';
79 $cache_id = implode(':', $cache_keys);
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.
87 if (!empty($function)) {
88 $values = $function($definition, $entity, $cacheable);
91 $values = $definition->getSetting('allowed_values');
95 $allowed_values[$cache_id] = $values;
102 return $allowed_values[$cache_id];
106 * Implements hook_field_storage_config_update_forbid().
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()]));
121 * Checks if a list of values are being used in actual field values.
123 function _options_values_in_use($entity_type, $field_name, $values) {
125 $factory = \Drupal::service('entity.query');
126 $result = $factory->get($entity_type)
127 ->condition($field_name . '.value', $values, 'IN')