diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php index a59f7d1..74f25b4 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReference.php @@ -64,8 +64,7 @@ public function getTarget() { if (!isset($this->target) && isset($this->id)) { // If we have a valid reference, return the entity object which is typed // data itself. - $constraints = $this->definition->getConstraints(); - $this->target = entity_load($constraints['EntityType'], $this->id); + $this->target = entity_load($this->definition->getConstraint('EntityType'), $this->id); } return $this->target; } diff --git a/core/lib/Drupal/Core/Field/FieldDefinition.php b/core/lib/Drupal/Core/Field/FieldDefinition.php index d33da70..e93006d 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinition.php +++ b/core/lib/Drupal/Core/Field/FieldDefinition.php @@ -22,7 +22,7 @@ class FieldDefinition extends ListDefinition implements FieldDefinitionInterface * @param string $type * The type of the field. * - * @return \Drupal\Core\Field\FieldDefinition + * @return \Drupal\Core\Field\FieldDefinitionInterface * A new field definition object. */ public static function create($type) { @@ -42,7 +42,7 @@ public function getFieldName() { * @param string $name * The field name to set. * - * @return self + * @return static * The object itself for chaining. */ public function setFieldName($name) { @@ -73,7 +73,7 @@ public function getFieldSettings() { * @param array $settings * The value to set. * - * @return self + * @return static * The object itself for chaining. */ public function setFieldSettings(array $settings) { @@ -85,8 +85,7 @@ public function setFieldSettings(array $settings) { * {@inheritdoc} */ public function getFieldSetting($setting_name) { - $settings = $this->getFieldSettings(); - return isset($settings[$setting_name]) ? $settings[$setting_name] : NULL; + return $this->getItemDefinition()->getSetting($setting_name); } /** @@ -97,13 +96,12 @@ public function getFieldSetting($setting_name) { * @param mixed $value * The value to set. * - * @return self + * @return static * The object itself for chaining. */ public function setFieldSetting($setting_name, $value) { - $settings = $this->getFieldSettings(); - $settings[$setting_name] = $value; - return $this->setFieldSettings($settings); + $this->getItemDefinition()->setSetting($setting_name, $value); + return $this; } /** @@ -126,7 +124,7 @@ public function isFieldTranslatable() { * @param bool $translatable * Whether the field is translatable. * - * @return self + * @return static * The object itself for chaining. */ public function setTranslatable($translatable) { @@ -191,7 +189,7 @@ public function isFieldMultiple() { * @param bool $required * Whether the field is required. * - * @return self + * @return static * The object itself for chaining. */ public function setFieldRequired($required) { @@ -211,7 +209,7 @@ public function isFieldQueryable() { * @param bool $queryable * Whether the field is queryable. * - * @return self + * @return static * The object itself for chaining. */ public function setFieldQueryable($queryable) { @@ -227,7 +225,7 @@ public function setFieldQueryable($queryable) { * @param array $constraints * The constraints to set. * - * @return self + * @return static * The object itself for chaining. */ public function setPropertyConstraints($name, array $constraints) { diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index c329ed6..0145489 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -199,7 +199,7 @@ public function getConstraints() { // applying them. if ($property_constraints = $this->definition->getConstraints()) { $constraints[] = \Drupal::typedData()->getValidationConstraintManager() - ->create('ComplexData', $property_constraints); + ->create('ComplexData', $property_constraints['ComplexData']); } return $constraints; } diff --git a/core/lib/Drupal/Core/TypedData/DataDefinition.php b/core/lib/Drupal/Core/TypedData/DataDefinition.php index ed4709e..9fec1a3 100644 --- a/core/lib/Drupal/Core/TypedData/DataDefinition.php +++ b/core/lib/Drupal/Core/TypedData/DataDefinition.php @@ -202,12 +202,7 @@ public function setClass($class) { } /** - * Returns the array of settings, as required by the used class. - * - * See the documentation of the class for supported or required settings. - * - * @return array - * The array of settings. + * {@inheritdoc} */ public function getSettings() { return isset($this->definition['settings']) ? $this->definition['settings'] : array(); @@ -228,6 +223,29 @@ public function setSettings(array $settings) { } /** + * {@inheritdoc} + */ + public function getSetting($setting_name) { + return isset($this->definition['settings'][$setting_name]) ? $this->definition['settings'][$setting_name] : NULL; + } + + /** + * Sets a definition setting. + * + * @param string $setting_name + * The definition setting to set. + * @param mixed $value + * The value to set. + * + * @return static + * The object itself for chaining. + */ + public function setSetting($setting_name, $value) { + $this->definition['settings'][$setting_name] = $value; + return $this; + } + + /** * Returns an array of validation constraints. * * See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. @@ -241,6 +259,13 @@ public function getConstraints() { } /** + * {@inheritdoc} + */ + public function getConstraint($constraint_name) { + return isset($this->definition['constraints'][$constraint_name]) ? $this->definition['constraints'][$constraint_name] : NULL; + } + + /** * Sets the array of validation constraints. * * See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. diff --git a/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php b/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php index eb5dd67..219cd30 100644 --- a/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php +++ b/core/lib/Drupal/Core/TypedData/DataDefinitionInterface.php @@ -102,6 +102,17 @@ public function getClass(); public function getSettings(); /** + * Returns the value of a given setting. + * + * @param string $setting_name + * The setting name. + * + * @return mixed + * The setting value. + */ + public function getSetting($setting_name); + + /** * Returns an array of validation constraints. * * See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. @@ -112,4 +123,17 @@ public function getSettings(); */ public function getConstraints(); + /** + * Returns a validation constraint. + * + * See \Drupal\Core\TypedData\TypedDataManager::getConstraints() for details. + * + * @param string $constraint_name + * The name of the the constraint, i.e. its plugin id. + * + * @return \Symfony\Component\Validator\Constraint + * A validation constraint. + */ + public function getConstraint($constraint_name); + } diff --git a/core/lib/Drupal/Core/TypedData/TypedDataManager.php b/core/lib/Drupal/Core/TypedData/TypedDataManager.php index 1045697..65db76b 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataManager.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataManager.php @@ -188,10 +188,12 @@ public function getInstance(array $options) { */ public function getPropertyInstance(TypedDataInterface $object, $property_name, $value = NULL) { $definition = $object->getRoot()->getDefinition(); - $key = $definition->getDataType(); + // If the definition is a list, we need to look at the data type and the + // settings of its item definition. if ($definition instanceof ListDefinition) { - $key .= ':' . $definition->getItemDefinition()->getDataType(); + $definition = $definition->getItemDefinition(); } + $key = $definition->getDataType(); if ($settings = $definition->getSettings()) { $key .= ':' . implode(',', $settings); } diff --git a/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php b/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php index ad9410d..909e0a3 100644 --- a/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php +++ b/core/modules/edit/lib/Drupal/edit/Form/EditFieldForm.php @@ -243,8 +243,8 @@ protected function simplify(array &$form, array &$form_state) { */ protected function getChangedFieldName(EntityInterface $entity) { foreach ($entity as $field_name => $field) { - $definition = $field->getDefinition(); - if (isset($definition['property_constraints']['value']['EntityChanged'])) { + $constraints = $field->getDefinition()->getConstraints(); + if (isset($constraints['ComplexData']['value']['EntityChanged'])) { return $field_name; } } diff --git a/core/modules/field/lib/Drupal/field/Entity/Field.php b/core/modules/field/lib/Drupal/field/Entity/Field.php index fc52346..0483272 100644 --- a/core/modules/field/lib/Drupal/field/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Entity/Field.php @@ -782,6 +782,18 @@ public function getSettings() { /** * {@inheritdoc} */ + public function getSetting($setting_name) { + // This should actually return the settings for field item list, which are + // not the field settings. However, there is no harm in returning field + // settings here, so we do that to avoid confusion for now. + // @todo: Unify with getFieldSettings() or remove once typed data moved + // to the adapter approach. + return $this->getFieldSetting($setting_name); + } + + /** + * {@inheritdoc} + */ public function getConstraints() { return array(); } @@ -789,6 +801,13 @@ public function getConstraints() { /** * {@inheritdoc} */ + public function getConstraint($constraint_name) { + return NULL; + } + + /** + * {@inheritdoc} + */ public function getItemDefinition() { if (!isset($this->itemDefinition)) { $this->itemDefinition = DataDefinition::create('field_item:' . $this->type) diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index 7e34d3d..3b63fa9 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -677,6 +677,13 @@ public function getSettings() { /** * {@inheritdoc} */ + public function getSetting($setting_name) { + return $this->getFieldSetting($setting_name); + } + + /** + * {@inheritdoc} + */ public function getConstraints() { return array(); } @@ -684,6 +691,13 @@ public function getConstraints() { /** * {@inheritdoc} */ + public function getConstraint($constraint_name) { + return NULL; + } + + /** + * {@inheritdoc} + */ public function getItemDefinition() { if (!isset($this->itemDefinition)) { $this->itemDefinition = DataDefinition::create('field_item:' . $this->field->type) diff --git a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItemBase.php b/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItemBase.php index ef42c2f..a41b77a 100644 --- a/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItemBase.php +++ b/core/modules/text/lib/Drupal/text/Plugin/Field/FieldType/TextItemBase.php @@ -76,7 +76,7 @@ public function getCacheData() { $text_processing = $this->getFieldSetting('text_processing'); if (!$text_processing || filter_format_allowcache($this->get('format')->getValue())) { foreach ($this->getPropertyDefinitions() as $property => $definition) { - if (isset($definition['class']) && ($definition['class'] == '\Drupal\text\TextProcessed')) { + if ($definition->getClass() == '\Drupal\text\TextProcessed') { $data[$property] = $this->get($property)->getValue(); } } @@ -96,8 +96,7 @@ public function onChange($property_name) { // Unset processed properties that are affected by the change. foreach ($this->getPropertyDefinitions() as $property => $definition) { if ($definition->getClass() == '\Drupal\text\TextProcessed') { - $settings = $definition->getSettings(); - if ($property_name == 'format' || (isset($settings['text source']) && $settings['text source'] == $property_name)) { + if ($property_name == 'format' || ($definition->getSetting('text source') == $property_name)) { $this->set($property, NULL, FALSE); } } diff --git a/core/modules/text/lib/Drupal/text/TextProcessed.php b/core/modules/text/lib/Drupal/text/TextProcessed.php index 22a0772..e32087b 100644 --- a/core/modules/text/lib/Drupal/text/TextProcessed.php +++ b/core/modules/text/lib/Drupal/text/TextProcessed.php @@ -32,8 +32,7 @@ class TextProcessed extends TypedData { public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) { parent::__construct($definition, $name, $parent); - $settings = $definition->getSettings(); - if (!isset($settings['text source'])) { + if ($definition->getSetting('text source') === NULL) { throw new \InvalidArgumentException("The definition's 'source' key has to specify the name of the text property to be processed."); } } @@ -47,8 +46,7 @@ public function getValue($langcode = NULL) { } $item = $this->getParent(); - $settings = $this->definition->getSettings(); - $text = $item->{($settings['text source'])}; + $text = $item->{($this->definition->getSetting('text source'))}; // Avoid running check_markup() or check_plain() on empty strings. if (!isset($text) || $text === '') {