diff --git a/core/lib/Drupal/Core/Field/FieldItemBase.php b/core/lib/Drupal/Core/Field/FieldItemBase.php index 84d73ec..139463e 100644 --- a/core/lib/Drupal/Core/Field/FieldItemBase.php +++ b/core/lib/Drupal/Core/Field/FieldItemBase.php @@ -101,6 +101,43 @@ protected function getSetting($setting_name) { /** * {@inheritdoc} */ + public function getValue($include_computed = FALSE) { + $value = parent::getValue(); + + // Include computed properties if the condition of + // ::includeComputedProperties() is fulfilled. + if ($this->includeComputedProperties($include_computed)) { + // Iterate only over computed properties and include them, as the parent + // method is not including them. + foreach ($this->properties as $name => $property) { + $definition = $property->getDataDefinition(); + if ($definition->isComputed()) { + $value[$name] = $property->getValue(); + } + } + } + return $value; + } + + /** + * Checks if included properties have to be included. + * + * Helper method for ::getValue() to check if included properties have to be + * included. + * + * @param bool $include_computed + * Whether to include computed properties as provided to ::getValue(). + * + * @return bool + * TRUE if computed properties have to be included. FALSE otherwise. + */ + protected function includeComputedProperties($include_computed) { + return $include_computed && !$this->isEmpty(); + } + + /** + * {@inheritdoc} + */ public function setValue($values, $notify = TRUE) { // Treat the values as property value of the first property, if no array is // given. diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php index b156dff..c39e92a 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php @@ -208,15 +208,10 @@ public function setValue($values, $notify = TRUE) { /** * {@inheritdoc} */ - public function getValue($include_computed = FALSE) { - $values = parent::getValue(); - + protected function includeComputedProperties($include_computed) { // If there is an unsaved entity, return it as part of the field item values // to ensure idempotency of getValue() / setValue(). - if ($this->hasNewEntity() || ($include_computed && !$this->isEmpty())) { - $values['entity'] = $this->entity; - } - return $values; + return $this->hasNewEntity() ?: parent::includeComputedProperties($include_computed); } /**