diff -u b/core/modules/aggregator/src/Entity/Feed.php b/core/modules/aggregator/src/Entity/Feed.php --- b/core/modules/aggregator/src/Entity/Feed.php +++ b/core/modules/aggregator/src/Entity/Feed.php @@ -10,7 +10,7 @@ use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\FieldDefinition; -use Drupal\options\Plugin\Field\FieldType\ListItemBase; +use Drupal\Core\Field\FieldItemBase; use Symfony\Component\DependencyInjection\Container; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\aggregator\FeedInterface; @@ -168,7 +168,7 @@ ->setDescription(t('The length of time between feed updates. Requires a correctly configured cron maintenance task.', array('@cron' => url('admin/reports/status')))) ->setSetting('unsigned', TRUE) ->setRequired(TRUE) - ->setSetting('allowed_values', ListItemBase::structureAllowedValues($period)) + ->setSetting('allowed_values', FieldItemBase::structureAllowedValues($period)) ->setDisplayOptions('form', array( 'type' => 'options_select', 'weight' => -2, diff -u b/core/modules/options/options.module b/core/modules/options/options.module --- b/core/modules/options/options.module +++ b/core/modules/options/options.module @@ -101,9 +101,10 @@ // Forbid any update that removes allowed values with actual data. $allowed_values = $field->getSetting('allowed_values'); $prior_allowed_values = $prior_field->getSetting('allowed_values'); + $type_definition = \Drupal::typedDataManager()->getDefinition('field_item:' . $field->getType()); $lost_keys = array_diff( - array_keys(ListItemBase::simplifyAllowedValues($prior_allowed_values)), - array_keys(ListItemBase::simplifyAllowedValues($allowed_values)) + array_keys($type_definition['class']::simplifyAllowedValues($prior_allowed_values)), + array_keys($type_definition['class']::simplifyAllowedValues($allowed_values)) ); if (_options_values_in_use($field->entity_type, $field->getName(), $lost_keys)) { throw new FieldStorageDefinitionUpdateForbiddenException(t('A list field (@field_name) with existing data cannot have its keys changed.', array('@field_name' => $field->getName()))); diff -u b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php --- b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php +++ b/core/modules/options/src/Plugin/Field/FieldFormatter/OptionsDefaultFormatter.php @@ -7,9 +7,12 @@ namespace Drupal\options\Plugin\Field\FieldFormatter; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Field\FieldItemListInterface; -use Drupal\options\Plugin\Field\FieldType\ListItemBase; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\TypedData\TypedDataManager; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation of the 'list_default' formatter. @@ -25,7 +28,48 @@ * } * ) */ -class OptionsDefaultFormatter extends FormatterBase { +class OptionsDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface { + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('typed_data_manager') + ); + } + + /** + * Constructs a new OptionsDefaultFormatter. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Third party settings. + * @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager + * The typed data manager. + */ + public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, TypedDataManager $typed_data_manager) { + parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings); + $this->typedDataManager = $typed_data_manager; + } /** * {@inheritdoc} @@ -34,7 +78,8 @@ $elements = array(); $entity = $items->getEntity(); - $allowed_values = ListItemBase::simplifyAllowedValues(options_allowed_values($this->fieldDefinition, $entity)); + $type_definition = $this->typedDataManager->getDefinition('field_item:' . $this->fieldDefinition->getType()); + $allowed_values = $type_definition['class']::simplifyAllowedValues(options_allowed_values($this->fieldDefinition, $entity)); foreach ($items as $delta => $item) { if (isset($allowed_values[$item->value])) { diff -u b/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php b/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php --- b/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListBooleanItem.php @@ -62,7 +62,7 @@ * {@inheritdoc} */ public function getSettableOptions(AccountInterface $account = NULL) { - return ListItemBase::simplifyAllowedValues(options_allowed_values($this->getFieldDefinition(), $this->getEntity())); + return static::simplifyAllowedValues(options_allowed_values($this->getFieldDefinition(), $this->getEntity())); } /** diff -u b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php --- b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListFloatItem.php @@ -89,2 +89,20 @@ + /** + * {@inheritdoc} + */ + public static function simplifyAllowedValues(array $structured_values) { + $values = array(); + foreach ($structured_values as $item) { + if (is_array($item['label'])) { + $item['label'] = static::simplifyAllowedValues($item['label']); + } + // Cast the value to a float first so that .5 and 0.5 are the same value + // and then cast to a string so that values like 0.5 can be used as array + // keys. + // @see http://php.net/manual/en/language.types.array.php + $values[(string) (float) $item['value']] = $item['label']; + } + return $values; + } + } diff -u b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php --- b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php +++ b/core/modules/options/src/Plugin/Field/FieldType/ListItemBase.php @@ -208,55 +208,6 @@ } /** - * Simplify allowed values to a key-value array from the structured array. - * - * @param array $structured_values - * Array of items with a 'value' and 'label' key each for the allowed - * values. - * - * @return array - * Allowed values were the array key is the 'value' value, the value is - * the 'label' value. - */ - public static function simplifyAllowedValues(array $structured_values) { - $values = array(); - foreach ($structured_values as $item) { - if (is_array($item['label'])) { - $item['label'] = static::simplifyAllowedValues($item['label']); - } - // Cast the value to a string so that values like 0 and 0.5 are not - // treated as equal. - $values[(string) $item['value']] = $item['label']; - } - return $values; - } - - /** - * Creates a structured array of allowed values from a key-value array. - * - * @param array $values - * Allowed values were the array key is the 'value' value, the value is - * the 'label' value. - * - * @return array - * Array of items with a 'value' and 'label' key each for the allowed - * values. - */ - public static function structureAllowedValues(array $values) { - $structured_values = array(); - foreach ($values as $value => $label) { - if (is_array($label)) { - $label = static::structureAllowedValues($label); - } - $structured_values[] = array( - 'value' => $value, - 'label' => $label, - ); - } - return $structured_values; - } - - /** * Checks whether a candidate allowed value is valid. * * @param string $option diff -u b/core/modules/options/src/Tests/OptionsFieldUITest.php b/core/modules/options/src/Tests/OptionsFieldUITest.php --- b/core/modules/options/src/Tests/OptionsFieldUITest.php +++ b/core/modules/options/src/Tests/OptionsFieldUITest.php @@ -165,14 +165,14 @@ $this->assertAllowedValuesInput("Zero\nOne", 'invalid input', 'Unkeyed lists are rejected once the field has data.'); // Check that values can be added but values in use cannot be removed. - $string = "0|Zero\n0.5|Point five\n2|Two"; + $string = "0|Zero\n.5|Point five\n2|Two"; $array = array( array('value' => (float) 0, 'label' => 'Zero'), array('value' => (float) 0.5, 'label' => 'Point five'), array('value' => (float) 2, 'label' => 'Two'), ); $this->assertAllowedValuesInput($string, $array, 'Values can be added.'); - $string = "0|Zero\n0.5|Point five"; + $string = "0|Zero\n.5|Point five"; $array = array( array('value' => (float) 0, 'label' => 'Zero'), array('value' => (float) 0.5, 'label' => 'Point five'), diff -u b/core/modules/options/tests/options_test.module b/core/modules/options/tests/options_test.module --- b/core/modules/options/tests/options_test.module +++ b/core/modules/options/tests/options_test.module @@ -7,7 +7,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\options\Plugin\Field\FieldType\ListItemBase; +use Drupal\Core\Field\FieldItemBase; /** * Allowed values callback. @@ -23,7 +23,7 @@ ), ); - return ListItemBase::structureAllowedValues($values); + return FieldItemBase::structureAllowedValues($values); } /** @@ -40,3 +40,3 @@ // We need the values of the entity as keys. - return ListItemBase::structureAllowedValues(array_combine($values, $values)); + return FieldItemBase::structureAllowedValues(array_combine($values, $values)); } only in patch2: unchanged: --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -255,4 +255,34 @@ public function instanceSettingsForm(array $form, array &$form_state) { return array(); } + /** + * {@inheritdoc} + */ + public static function simplifyAllowedValues(array $structured_values) { + $values = array(); + foreach ($structured_values as $item) { + if (is_array($item['label'])) { + $item['label'] = static::simplifyAllowedValues($item['label']); + } + $values[$item['value']] = $item['label']; + } + return $values; + } + + /** + * {@inheritdoc} + */ + public static function structureAllowedValues(array $values) { + $structured_values = array(); + foreach ($values as $value => $label) { + if (is_array($label)) { + $label = static::structureAllowedValues($label); + } + $structured_values[] = array( + 'value' => $value, + 'label' => $label, + ); + } + return $structured_values; + } } only in patch2: unchanged: --- a/core/lib/Drupal/Core/Field/FieldItemInterface.php +++ b/core/lib/Drupal/Core/Field/FieldItemInterface.php @@ -271,4 +271,30 @@ public function settingsForm(array &$form, array &$form_state, $has_data); */ public function instanceSettingsForm(array $form, array &$form_state); + /** + * Simplify allowed values to a key-value array from the structured array. + * + * @param array $structured_values + * Array of items with a 'value' and 'label' key each for the allowed + * values. + * + * @return array + * Allowed values were the array key is the 'value' value, the value is + * the 'label' value. + */ + public static function simplifyAllowedValues(array $structured_values); + + /** + * Creates a structured array of allowed values from a key-value array. + * + * @param array $values + * Allowed values were the array key is the 'value' value, the value is + * the 'label' value. + * + * @return array + * Array of items with a 'value' and 'label' key each for the allowed + * values. + */ + public static function structureAllowedValues(array $values); + }