Drupal investigation

options.module 6.5KB

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