diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 239588f..4effaa0 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -23,6 +23,13 @@ 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) { @@ -30,12 +37,22 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel ->setLabel(t('Path alias')); $properties['pid'] = DataDefinition::create('integer') ->setLabel(t('Path id')); + $properties['langcode'] = DataDefinition::create('string') + ->setLabel(t('Language Code')); return $properties; } /** * {@inheritdoc} */ + public function __get($name) { + $this->ensureLoaded(); + return parent::__get($name); + } + + /** + * {@inheritdoc} + */ public static function schema(FieldStorageDefinitionInterface $field_definition) { return array(); } @@ -43,6 +60,14 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) /** * {@inheritdoc} */ + public function getValue() { + $this->ensureLoaded(); + return parent::getValue(); + } + + /** + * {@inheritdoc} + */ public function preSave() { $this->alias = trim($this->alias); } @@ -88,4 +113,21 @@ public static function mainPropertyName() { return 'alias'; } + /** + * Ensures the alias properties are loaded if available. + */ + protected function ensureLoaded() { + $entity = $this->getEntity(); + if (!$this->isLoaded && !$entity->isNew()) { + $alias = \Drupal::service('path.alias_storage')->load([ + 'source' => '/' . $entity->toUrl()->getInternalPath(), + 'langcode' => $this->getLangcode(), + ]); + if ($alias) { + $this->setValue($alias); + } + $this->isLoaded = TRUE; + } + } + } diff --git a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php index 366db88..eb6a0c7 100644 --- a/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php +++ b/core/modules/path/src/Plugin/Field/FieldWidget/PathWidget.php @@ -5,7 +5,6 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Language\LanguageInterface; use Symfony\Component\Validator\ConstraintViolationInterface; /** @@ -28,14 +27,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $entity = $items->getEntity(); $path = array(); if (!$entity->isNew()) { - $conditions = array('source' => '/' . $entity->urlInfo()->getInternalPath()); - if ($items->getLangcode() != LanguageInterface::LANGCODE_NOT_SPECIFIED) { - $conditions['langcode'] = $items->getLangcode(); - } - $path = \Drupal::service('path.alias_storage')->load($conditions); - if ($path === FALSE) { - $path = array(); - } + $path = $items->first()->getValue(); } $path += array( 'pid' => NULL,