diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php index a98e718..1a5fbe0 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php @@ -5,12 +5,42 @@ 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() { + // Default the langcode to the current language if this is a new entity or + // there is no alias for an existent entity. + // @todo Set the langcode to not specified for untranslatable fields + // in https://www.drupal.org/node/2689459. + $value = ['langcode' => $this->getLangcode()]; + + $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; + } + } + + $this->list[0] = $this->createItem(0, $value); + } + /** * {@inheritdoc} */ @@ -34,42 +64,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 0f0a77b..65cb14b 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -24,20 +24,6 @@ class PathItem extends FieldItemBase { /** - * Whether the alias has been loaded from the alias storage service yet. - * - * @var bool - */ - protected $isLoaded = FALSE; - - /** - * Whether the alias is currently being set. - * - * @var bool - */ - protected $isLoading = FALSE; - - /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { @@ -53,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 []; } @@ -68,73 +46,17 @@ 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 === '') && ($this->langcode === NULL || $this->langcode === ''); } /** * {@inheritdoc} */ public function preSave() { - $this->alias = trim($this->alias); - } - - /** - * {@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 setValue($values, $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::setValue($values, $notify); + if ($this->alias !== NULL) { + $this->alias = trim($this->alias); + } } /** @@ -178,42 +100,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() { - // Make sure to avoid a infinite loop if setValue() has be called from this - // block which calls ensureLoaded(). - if (!$this->isLoaded && !$this->isLoading) { - $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->isLoading = TRUE; - $this->setValue($alias); - $this->isLoading = FALSE; - } - 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; - } - } - }