2009-02-05 03:42:58 +00:00
<?php
/**
* @file
* Defines selection, check box and radio button widgets for text and numeric fields.
*/
2009-02-10 03:16:15 +00:00
2019-04-16 05:38:27 +00:00
use Drupal\Core\Url;
2014-12-14 13:15:33 +00:00
use Drupal\Core\Entity\FieldableEntityInterface;
2014-07-18 18:56:27 +00:00
use Drupal\Core\Entity\Exception\FieldStorageDefinitionUpdateForbiddenException;
2014-12-14 13:15:33 +00:00
use Drupal\Core\Field\FieldStorageDefinitionInterface;
2014-06-30 03:33:08 +00:00
use Drupal\Core\Routing\RouteMatchInterface;
2014-07-18 18:56:27 +00:00
use Drupal\field\FieldStorageConfigInterface;
2012-07-18 09:44:36 +00:00
2009-12-13 02:04:14 +00:00
/**
* Implements hook_help().
*/
2014-06-30 03:33:08 +00:00
function options_help($route_name, RouteMatchInterface $route_match) {
2014-05-07 02:04:53 +00:00
switch ($route_name) {
case 'help.page.options':
2009-12-13 02:04:14 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2019-04-16 05:38:27 +00:00
$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>';
2014-02-13 10:31:02 +00:00
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing and displaying list fields') . '</dt>';
2019-04-16 05:38:27 +00:00
$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>';
2014-02-13 10:31:02 +00:00
$output .= '<dt>' . t('Defining option keys and labels') . '</dt>';
$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>';
$output .= '<dt>' . t('Choosing list field type') . '</dt>';
$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>';
$output .= '</dl>';
2009-12-13 02:04:14 +00:00
return $output;
}
}
2012-07-18 09:44:36 +00:00
/**
2014-07-18 18:56:27 +00:00
* Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.
2012-07-18 09:44:36 +00:00
*/
2014-07-18 18:56:27 +00:00
function options_field_storage_config_update(FieldStorageConfigInterface $field_storage) {
2012-07-18 09:44:36 +00:00
drupal_static_reset('options_allowed_values');
}
2013-06-16 08:27:11 +00:00
/**
2014-07-18 18:56:27 +00:00
* Implements hook_ENTITY_TYPE_delete() for 'field_storage_config'.
2013-06-16 08:27:11 +00:00
*/
2014-07-18 18:56:27 +00:00
function options_field_storage_config_delete(FieldStorageConfigInterface $field_storage) {
2013-06-16 08:27:11 +00:00
drupal_static_reset('options_allowed_values');
}
2012-07-18 09:44:36 +00:00
/**
* Returns the array of allowed values for a list field.
*
* The strings are not safe for output. Keys and values of the array should be
2020-02-12 09:52:52 +00:00
* sanitized through \Drupal\Core\Field\FieldFilteredMarkup before being
* displayed.
2012-07-18 09:44:36 +00:00
*
2014-12-14 13:15:33 +00:00
* @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition
* The field storage definition.
2016-08-18 09:38:31 +00:00
* @param \Drupal\Core\Entity\FieldableEntityInterface|null $entity
2014-12-14 13:15:33 +00:00
* (optional) The specific entity when this function is called from the
* context of a specific field on a specific entity. This allows custom
2016-04-16 12:17:32 +00:00
* 'allowed_values_function' callbacks to either restrict the values or
2014-12-14 13:15:33 +00:00
* customize the labels for particular bundles and entities. NULL when
* there is not a specific entity available, such as for Views filters.
2012-07-18 09:44:36 +00:00
*
2014-12-14 13:15:33 +00:00
* @return array
2012-07-18 09:44:36 +00:00
* The array of allowed values. Keys of the array are the raw stored values
* (number or text), values of the array are the display labels.
2014-12-14 13:15:33 +00:00
*
* @see callback_allowed_values_function()
2012-07-18 09:44:36 +00:00
*/
2014-12-14 13:15:33 +00:00
function options_allowed_values(FieldStorageDefinitionInterface $definition, FieldableEntityInterface $entity = NULL) {
2017-03-04 01:20:24 +00:00
$allowed_values = &drupal_static(__FUNCTION__, []);
2012-07-18 09:44:36 +00:00
2017-03-04 01:20:24 +00:00
$cache_keys = [$definition->getTargetEntityTypeId(), $definition->getName()];
2014-12-14 13:15:33 +00:00
if ($entity) {
$cache_keys[] = 'entity';
}
$cache_id = implode(':', $cache_keys);
2013-06-16 08:27:11 +00:00
if (!isset($allowed_values[$cache_id])) {
2014-12-14 13:15:33 +00:00
$function = $definition->getSetting('allowed_values_function');
2012-07-18 09:44:36 +00:00
// If $cacheable is FALSE, then the allowed values are not statically
// cached. See options_test_dynamic_values_callback() for an example of
// generating dynamic and uncached values.
$cacheable = TRUE;
if (!empty($function)) {
2014-12-14 13:15:33 +00:00
$values = $function($definition, $entity, $cacheable);
2012-07-18 09:44:36 +00:00
}
else {
2014-12-14 13:15:33 +00:00
$values = $definition->getSetting('allowed_values');
2012-07-18 09:44:36 +00:00
}
if ($cacheable) {
2013-06-16 08:27:11 +00:00
$allowed_values[$cache_id] = $values;
2012-07-18 09:44:36 +00:00
}
else {
return $values;
}
}
2013-06-16 08:27:11 +00:00
return $allowed_values[$cache_id];
2012-07-18 09:44:36 +00:00
}
/**
2014-07-18 18:56:27 +00:00
* Implements hook_field_storage_config_update_forbid().
2012-07-18 09:44:36 +00:00
*/
2014-07-18 18:56:27 +00:00
function options_field_storage_config_update_forbid(FieldStorageConfigInterface $field_storage, FieldStorageConfigInterface $prior_field_storage) {
2015-02-10 13:39:31 +00:00
if ($field_storage->getTypeProvider() == 'options' && $field_storage->hasData()) {
2012-07-18 09:44:36 +00:00
// Forbid any update that removes allowed values with actual data.
2014-07-18 18:56:27 +00:00
$allowed_values = $field_storage->getSetting('allowed_values');
$prior_allowed_values = $prior_field_storage->getSetting('allowed_values');
2014-10-14 13:09:47 +00:00
$lost_keys = array_keys(array_diff_key($prior_allowed_values, $allowed_values));
2015-02-10 13:39:31 +00:00
if (_options_values_in_use($field_storage->getTargetEntityTypeId(), $field_storage->getName(), $lost_keys)) {
Issue #2055851 by andypost, jungle, dawehner, Mac_Weber, sja112, borisson_, fietserwin, init90, Gábor Hojtsy, xjm, effulgentsia, tim.plunkett: Remove translation of exception messages
2020-05-19 00:21:21 +00:00
throw new FieldStorageDefinitionUpdateForbiddenException("A list field '{$field_storage->getName()}' with existing data cannot have its keys changed.");
2012-07-18 09:44:36 +00:00
}
}
}
/**
* Checks if a list of values are being used in actual field values.
*/
2014-01-13 18:06:09 +00:00
function _options_values_in_use($entity_type, $field_name, $values) {
2012-07-18 09:44:36 +00:00
if ($values) {
2018-05-17 12:18:31 +00:00
$result = \Drupal::entityQuery($entity_type)
2015-02-03 09:42:28 +00:00
->condition($field_name . '.value', $values, 'IN')
2013-09-01 06:20:08 +00:00
->count()
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($result) {
return TRUE;
2012-10-30 10:41:42 +00:00
}
2012-07-18 09:44:36 +00:00
}
return FALSE;
}