diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php index a98e718..8488c7e 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php @@ -5,12 +5,43 @@ use Drupal\Core\Access\AccessResult; use Drupal\Core\Field\FieldItemList; use Drupal\Core\Session\AccountInterface; +use Drupal\Core\TypedData\ComputedItemListTrait; /** * Represents a configurable entity path field. */ class PathFieldItemList extends FieldItemList { + use ComputedItemListTrait; + + /** + * {@inheritdoc} + */ + protected function computeValue() { + $entity = $this->getEntity(); + if (!$entity->isNew()) { + // @todo Support loading languge neutral aliases in + // https://www.drupal.org/node/2511968. + $alias = \Drupal::service('path.alias_storage')->load([ + 'source' => '/' . $entity->toUrl()->getInternalPath(), + 'langcode' => $this->getLangcode(), + ]); + + if ($alias) { + $value = $alias; + } + else { + // If there is no existing alias, default the langcode to the current + // language. + // @todo Set the langcode to not specified for untranslatable fields + // in https://www.drupal.org/node/2689459. + $value = ['langcode' => $this->getLangcode()]; + } + + $this->list[0] = $this->createItem(0, $value); + } + } + /** * {@inheritdoc} */ @@ -34,42 +65,4 @@ public function delete() { \Drupal::service('path.alias_storage')->delete($conditions); } - /** - * {@inheritdoc} - */ - public function getValue($include_computed = FALSE) { - $this->ensureLoaded(); - return parent::getValue($include_computed); - } - - /** - * {@inheritdoc} - */ - public function isEmpty() { - $this->ensureLoaded(); - return parent::isEmpty(); - } - - /** - * {@inheritdoc} - */ - public function getIterator() { - $this->ensureLoaded(); - return parent::getIterator(); - } - - /** - * Automatically create the first item for computed fields. - * - * This ensures that ::getValue() and ::isEmpty() calls will behave like a - * non-computed field. - * - * @todo: Move this to the base class in https://www.drupal.org/node/2392845. - */ - protected function ensureLoaded() { - if (!isset($this->list[0]) && $this->definition->isComputed()) { - $this->list[0] = $this->createItem(0); - } - } - } diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 9b5b67d..ea21dac 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -24,13 +24,6 @@ class PathItem extends FieldItemBase { /** - * Whether the alias has been loaded from the alias storage service yet. - * - * @var bool - */ - protected $isLoaded = FALSE; - - /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { @@ -46,14 +39,6 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel /** * {@inheritdoc} */ - public function __get($name) { - $this->ensureLoaded(); - return parent::__get($name); - } - - /** - * {@inheritdoc} - */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return []; } @@ -61,25 +46,8 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) /** * {@inheritdoc} */ - public function getValue() { - $this->ensureLoaded(); - return parent::getValue(); - } - - /** - * {@inheritdoc} - */ public function isEmpty() { - $this->ensureLoaded(); - return parent::isEmpty(); - } - - /** - * {@inheritdoc} - */ - public function getIterator() { - $this->ensureLoaded(); - return parent::getIterator(); + return ($this->alias === NULL || $this->alias === '') && ($this->pid === NULL || $this->pid === ''); } /** @@ -92,36 +60,6 @@ public function preSave() { /** * {@inheritdoc} */ - public function __set($name, $value) { - // Also ensure that existing values are loaded when setting a value, this - // ensures that it is possible to set a new value immediately after loading - // an entity. - $this->ensureLoaded(); - parent::__set($name, $value); - } - - /** - * {@inheritdoc} - */ - public function set($property_name, $value, $notify = TRUE) { - // Also ensure that existing values are loaded when setting a value, this - // ensures that it is possible to set a new value immediately after loading - // an entity. - $this->ensureLoaded(); - return parent::set($property_name, $value, $notify); - } - - /** - * {@inheritdoc} - */ - public function get($property_name) { - $this->ensureLoaded(); - return parent::get($property_name); - } - - /** - * {@inheritdoc} - */ public function postSave($update) { if (!$update) { if ($this->alias) { @@ -160,38 +98,4 @@ public static function mainPropertyName() { return 'alias'; } - /** - * Ensures the alias properties are loaded if available. - * - * This ensures that the properties will always be loaded and act like - * non-computed fields when calling ::__get() and getValue(). - * - * @todo: Determine if this should be moved to the base class in - * https://www.drupal.org/node/2392845. - */ - protected function ensureLoaded() { - if (!$this->isLoaded) { - $entity = $this->getEntity(); - if (!$entity->isNew()) { - // @todo Support loading languge neutral aliases in - // https://www.drupal.org/node/2511968. - $alias = \Drupal::service('path.alias_storage')->load([ - 'source' => '/' . $entity->toUrl()->getInternalPath(), - 'langcode' => $this->getLangcode(), - ]); - if ($alias) { - $this->setValue($alias); - } - else { - // If there is no existing alias, default the langcode to the current - // language. - // @todo Set the langcode to not specified for untranslatable fields - // in https://www.drupal.org/node/2689459. - $this->langcode = $this->getLangcode(); - } - } - $this->isLoaded = TRUE; - } - } - } diff --git a/core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php b/core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php index d3abef6..ad3f02e 100644 --- a/core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/serialization/src/Normalizer/ContentEntityNormalizer.php @@ -22,6 +22,7 @@ public function normalize($entity, $format = NULL, array $context = []) { $attributes = []; foreach ($entity as $name => $field_items) { + $field_items->filterEmptyItems(); if ($field_items->access('view', $context['account'])) { $attributes[$name] = $this->serializer->normalize($field_items, $format, $context); }