diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index d3b5c41..0f95e21 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -465,6 +465,15 @@ public function validate() { } /** + * {@inheritdoc} + */ + public function applyDefaultValue($notify = TRUE) { + foreach ($this->getProperties() as $property) { + $property->applyDefaultValue(FALSE); + } + } + + /** * Implements \Drupal\Core\TypedData\ComplexDataInterface::onChange(). */ public function onChange($property_name) { diff --git a/core/lib/Drupal/Core/Entity/EntityBCDecorator.php b/core/lib/Drupal/Core/Entity/EntityBCDecorator.php index 9697648..00c4654 100644 --- a/core/lib/Drupal/Core/Entity/EntityBCDecorator.php +++ b/core/lib/Drupal/Core/Entity/EntityBCDecorator.php @@ -523,4 +523,11 @@ public function onChange($property_name) { public function isTranslatable() { return $this->decorated->isTranslatable(); } + + /** + * Forwards the call to the decorated entity. + */ + public function applyDefaultValue($notify = TRUE) { + return $this->decorated->applyDefaultValue($notify); + } } diff --git a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php index 2c3feef..eb94b20 100644 --- a/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Entity/Field/FieldItemBase.php @@ -91,6 +91,7 @@ public function set($property_name, $value, $notify = TRUE) { // value that needs to be updated. if (isset($this->properties[$property_name])) { $this->properties[$property_name]->setValue($value, FALSE); + unset($this->values[$property_name]); } // Allow setting plain values for not-defined properties also. else { diff --git a/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php b/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php index 5b78a05..f568aec 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php @@ -87,15 +87,19 @@ public function __isset($property_name) { * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get(). */ public function setValue($values, $notify = TRUE) { - // Treat the values as property value of the entity property, if no array is - // given. + // Treat the values as value of the entity property, if no array is + // given as this handles entity IDs and objects. if (isset($values) && !is_array($values)) { - $values = array('entity' => $values); + // Directly update the property instead of invoking the parent, so that + // the entity property can take care of updating the ID property. + $this->properties['entity']->setValue($values, $notify); } - // Make sure that the 'entity' property gets set as 'target_id'. - if (isset($values['target_id']) && !isset($values['entity'])) { - $values['entity'] = $values['target_id']; + else { + // Make sure that the 'entity' property gets set as 'target_id'. + if (isset($values['target_id']) && !isset($values['entity'])) { + $values['entity'] = $values['target_id']; + } + parent::setValue($values, $notify); } - parent::setValue($values, $notify); } } diff --git a/core/lib/Drupal/Core/Entity/Field/Type/Field.php b/core/lib/Drupal/Core/Entity/Field/Type/Field.php index f3a8866..3f89f97 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/Field.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/Field.php @@ -22,8 +22,7 @@ * directly to the first item. * * Supported settings (below the definition's 'settings' key) are: - * - default_value: (optional) If set, the default value to apply to the first - * item of the field. + * - default_value: (optional) If set, the default value to apply to the field. * * @see \Drupal\Core\Entity\Field\FieldInterface */ @@ -207,16 +206,15 @@ public function defaultAccess($operation = 'view', User $account = NULL) { } /** - * Overrides \Drupal\Core\TypedData\TypedDataInterface::applyDefaultValue(). + * {@inheritdoc} */ - public function applyDefaultValue() { + public function applyDefaultValue($notify = TRUE) { if (isset($this->definition['settings']['default_value'])) { - $value = $this->definition['settings']['default_value']; - // Create one field item with the value provided. - $this->setValue(array(0 => $value)); + $this->setValue($this->definition['settings']['default_value'], $notify); } else { - $this->setValue(NULL); + // Create one field item and apply defaults. + $this->offsetGet(0)->applyDefaultValue(FALSE); } return $this; } diff --git a/core/lib/Drupal/Core/Entity/Field/Type/LanguageField.php b/core/lib/Drupal/Core/Entity/Field/Type/LanguageField.php deleted file mode 100644 index 485995f..0000000 --- a/core/lib/Drupal/Core/Entity/Field/Type/LanguageField.php +++ /dev/null @@ -1,31 +0,0 @@ - LANGUAGE_NOT_SPECIFIED); - if (isset($this->definition['settings']['default_value'])) { - $value = $this->definition['settings']['default_value']; - } - $this->setValue(array(0 => $value)); - return $this; - } -} diff --git a/core/lib/Drupal/Core/Entity/Field/Type/LanguageItem.php b/core/lib/Drupal/Core/Entity/Field/Type/LanguageItem.php index eb02069..c861f2d 100644 --- a/core/lib/Drupal/Core/Entity/Field/Type/LanguageItem.php +++ b/core/lib/Drupal/Core/Entity/Field/Type/LanguageItem.php @@ -51,23 +51,28 @@ public function getPropertyDefinitions() { */ public function setValue($values, $notify = TRUE) { // Treat the values as property value of the language property, if no array - // is given. + // is given as this handles language codes and objects. if (isset($values) && !is_array($values)) { - $values = array('language' => $values); + // Directly update the property instead of invoking the parent, so that + // the language property can take care of updating the language code + // property. + $this->properties['language']->setValue($values, $notify); } - // Make sure that the 'language' property gets set as 'value'. - if (isset($values['value']) && !isset($values['language'])) { - $values['language'] = $values['value']; + else { + // Make sure that the 'language' property gets set as 'value'. + if (isset($values['value']) && !isset($values['language'])) { + $values['language'] = $values['value']; + } + parent::setValue($values, $notify); } - parent::setValue($values, $notify); } /** - * Overrides \Drupal\Core\TypedData\TypedDataInterface::applyDefaultValue(). + * {@inheritdoc} */ - public function applyDefaultValue() { - // Default to no default value. - $this->setValue(NULL); + public function applyDefaultValue($notify = TRUE) { + // Default to LANGUAGE_NOT_SPECIFIED. + $this->setValue(array('value' => LANGUAGE_NOT_SPECIFIED), $notify); return $this; } } diff --git a/core/lib/Drupal/Core/Entity/Field/Type/UUIDField.php b/core/lib/Drupal/Core/Entity/Field/Type/UUIDField.php deleted file mode 100644 index 0bc447a..0000000 --- a/core/lib/Drupal/Core/Entity/Field/Type/UUIDField.php +++ /dev/null @@ -1,28 +0,0 @@ -setValue(array(0 => array('value' => $uuid->generate()))); - return $this; - } -} diff --git a/core/lib/Drupal/Core/Entity/Field/Type/UuidItem.php b/core/lib/Drupal/Core/Entity/Field/Type/UuidItem.php new file mode 100644 index 0000000..2384c5f --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Field/Type/UuidItem.php @@ -0,0 +1,28 @@ +setValue(array('value' => $uuid->generate()), $notify); + return $this; + } +} diff --git a/core/lib/Drupal/Core/TypedData/ItemList.php b/core/lib/Drupal/Core/TypedData/ItemList.php index 7f9afdf..88af366 100644 --- a/core/lib/Drupal/Core/TypedData/ItemList.php +++ b/core/lib/Drupal/Core/TypedData/ItemList.php @@ -133,14 +133,7 @@ public function offsetGet($offset) { * @return \Drupal\Core\TypedData\TypedDataInterface */ protected function createItem($offset = 0, $value = NULL) { - $item = typed_data()->getPropertyInstance($this, $offset); - if (isset($value)) { - $item->setValue($value); - } - else { - $item->applyDefaultValue(); - } - return $item; + return typed_data()->getPropertyInstance($this, $offset, $value); } /** diff --git a/core/lib/Drupal/Core/TypedData/Type/Map.php b/core/lib/Drupal/Core/TypedData/Type/Map.php index da20e96..4bb802d 100644 --- a/core/lib/Drupal/Core/TypedData/Type/Map.php +++ b/core/lib/Drupal/Core/TypedData/Type/Map.php @@ -230,4 +230,14 @@ public function onChange($property_name) { $this->parent->onChange($this->name); } } + + /** + * {@inheritdoc} + */ + public function applyDefaultValue($notify = TRUE) { + // Apply the default value of all properties. + foreach ($this->getProperties() as $property) { + $property->applyDefaultValue(FALSE); + } + } } diff --git a/core/lib/Drupal/Core/TypedData/TypedData.php b/core/lib/Drupal/Core/TypedData/TypedData.php index 11538fd..073fe17 100644 --- a/core/lib/Drupal/Core/TypedData/TypedData.php +++ b/core/lib/Drupal/Core/TypedData/TypedData.php @@ -114,9 +114,9 @@ public function validate() { /** * Implements \Drupal\Core\TypedData\TypedDataInterface::applyDefaultValue(). */ - public function applyDefaultValue() { + public function applyDefaultValue($notify = TRUE) { // Default to no default value. - $this->setValue(NULL); + $this->setValue(NULL, $notify); return $this; } diff --git a/core/lib/Drupal/Core/TypedData/TypedDataInterface.php b/core/lib/Drupal/Core/TypedData/TypedDataInterface.php index 19febbd..96229b6 100644 --- a/core/lib/Drupal/Core/TypedData/TypedDataInterface.php +++ b/core/lib/Drupal/Core/TypedData/TypedDataInterface.php @@ -81,10 +81,15 @@ public function validate(); /** * Applies the default value. * + * @param bool $notify + * (optional) Whether to notify the parent object of the change. Defaults to + * TRUE. If a property is updated from a parent object, set it to FALSE to + * avoid being notified again. + * * @return \Drupal\Core\TypedData\TypedDataInterface * Returns itself to allow for chaining. */ - public function applyDefaultValue(); + public function applyDefaultValue($notify = TRUE); /** * Returns the name of a property or item. diff --git a/core/modules/file/lib/Drupal/file/Type/FileItem.php b/core/modules/file/lib/Drupal/file/Type/FileItem.php index 0b2c74b..4a35e9c 100644 --- a/core/modules/file/lib/Drupal/file/Type/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Type/FileItem.php @@ -61,15 +61,19 @@ public function getPropertyDefinitions() { * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get(). */ public function setValue($values, $notify = TRUE) { - // Treat the values as property value of the entity property, if no array is - // given. + // Treat the values as value of the entity property, if no array is + // given as this handles entity IDs and objects. if (isset($values) && !is_array($values)) { - $values = array('entity' => $values); + // Directly update the property instead of invoking the parent, so that + // the entity property can take care of updating the ID property. + $this->properties['entity']->setValue($values, $notify); } - // Make sure that the 'entity' property gets set as 'fid'. - if (isset($values['fid']) && !isset($values['entity'])) { - $values['entity'] = $values['fid']; + else { + // Make sure that the 'entity' property gets set as 'target_id'. + if (isset($values['fid']) && !isset($values['entity'])) { + $values['entity'] = $values['fid']; + } + parent::setValue($values, $notify); } - parent::setValue($values, $notify); } } diff --git a/core/modules/image/lib/Drupal/image/Type/ImageItem.php b/core/modules/image/lib/Drupal/image/Type/ImageItem.php index f6cedf2..868b4fb 100644 --- a/core/modules/image/lib/Drupal/image/Type/ImageItem.php +++ b/core/modules/image/lib/Drupal/image/Type/ImageItem.php @@ -68,15 +68,19 @@ public function getPropertyDefinitions() { * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get(). */ public function setValue($values, $notify = TRUE) { - // Treat the values as property value of the entity property, if no array is - // given. + // Treat the values as value of the entity property, if no array is + // given as this handles entity IDs and objects. if (isset($values) && !is_array($values)) { - $values = array('entity' => $values); + // Directly update the property instead of invoking the parent, so that + // the entity property can take care of updating the ID property. + $this->properties['entity']->setValue($values, $notify); } - // Make sure that the 'entity' property gets set as 'fid'. - if (isset($values['fid']) && !isset($values['entity'])) { - $values['entity'] = $values['fid']; + else { + // Make sure that the 'entity' property gets set as 'target_id'. + if (isset($values['fid']) && !isset($values['entity'])) { + $values['entity'] = $values['fid']; + } + parent::setValue($values, $notify); } - parent::setValue($values, $notify); } } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index b040863..e22c227 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -2361,7 +2361,7 @@ function system_data_type_info() { 'label' => t('Language field item'), 'description' => t('An entity field referencing a language.'), 'class' => '\Drupal\Core\Entity\Field\Type\LanguageItem', - 'list class' => '\Drupal\Core\Entity\Field\Type\LanguageField', + 'list class' => '\Drupal\Core\Entity\Field\Type\Field', ), 'entity_reference_field' => array( 'label' => t('Entity reference field item'), @@ -2378,8 +2378,8 @@ function system_data_type_info() { 'uuid_field' => array( 'label' => t('UUID field item'), 'description' => t('An entity field containing a UUID.'), - 'class' => '\Drupal\Core\Entity\Field\Type\StringItem', - 'list class' => '\Drupal\Core\Entity\Field\Type\UUIDField', + 'class' => '\Drupal\Core\Entity\Field\Type\UuidItem', + 'list class' => '\Drupal\Core\Entity\Field\Type\Field', ), ); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php index 3aad4a7..af50b25 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php @@ -116,16 +116,6 @@ class Term extends EntityNG implements TermInterface { public $parent; /** - * Default values for the term. - * - * @var array - */ - protected $values = array( - 'langcode' => array(LANGUAGE_DEFAULT => array(0 => array('value' => LANGUAGE_NOT_SPECIFIED))), - 'weight' => array(LANGUAGE_DEFAULT => array(0 => array('value' => 0))), - ); - - /** * Implements Drupal\Core\Entity\EntityInterface::id(). */ public function id() { diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php index bea7976..58f6f55 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php @@ -158,11 +158,14 @@ public function baseFieldDefinitions() { 'label' => t('Weight'), 'description' => t('The weight of this term in relation to other terms.'), 'type' => 'integer_field', + 'settings' => array('default_value' => 0), ); $properties['parent'] = array( 'label' => t('Term Parents'), 'description' => t('The parents of this term.'), 'type' => 'integer_field', + // Save new terms with no parents by default. + 'settings' => array('default_value' => 0), 'computed' => TRUE, ); return $properties; diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php index b558d97..94bb022 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php @@ -53,15 +53,19 @@ public function getPropertyDefinitions() { * Overrides \Drupal\Core\Entity\Field\FieldItemBase::get(). */ public function setValue($values, $notify = TRUE) { - // Treat the values as property value of the entity property, if no array is - // given. + // Treat the values as value of the entity property, if no array is + // given as this handles entity IDs and objects. if (isset($values) && !is_array($values)) { - $values = array('entity' => $values); + // Directly update the property instead of invoking the parent, so that + // the entity property can take care of updating the ID property. + $this->properties['entity']->setValue($values, $notify); } - // Make sure that the 'entity' property gets set as 'tid'. - if (isset($values['tid']) && !isset($values['entity'])) { - $values['entity'] = $values['tid']; + else { + // Make sure that the 'entity' property gets set as 'target_id'. + if (isset($values['tid']) && !isset($values['entity'])) { + $values['entity'] = $values['tid']; + } + parent::setValue($values, $notify); } - parent::setValue($values, $notify); } } diff --git a/core/modules/text/lib/Drupal/text/Type/TextItem.php b/core/modules/text/lib/Drupal/text/Type/TextItem.php index fecfaac..848f5ec 100644 --- a/core/modules/text/lib/Drupal/text/Type/TextItem.php +++ b/core/modules/text/lib/Drupal/text/Type/TextItem.php @@ -52,12 +52,12 @@ public function getPropertyDefinitions() { } /** - * Overrides \Drupal\Core\TypedData\TypedDataInterface::applyDefaultValue(). + * {@inheritdoc} */ - public function applyDefaultValue() { + public function applyDefaultValue($notify = TRUE) { // Default to a simple check_plain(). // @todo: Add in the filter default format here. - $this->setValue(array('format' => NULL)); + $this->setValue(array('format' => NULL), $notify); return $this; }