diff --git a/core/modules/path/src/PathAliasComputed.php b/core/modules/path/src/PathAliasComputed.php new file mode 100644 index 0000000000..838981465d --- /dev/null +++ b/core/modules/path/src/PathAliasComputed.php @@ -0,0 +1,47 @@ +alias !== NULL) { + return $this->alias; + } + + /** @var \Drupal\Core\Entity\EntityInterface $entity */ + if (($field_item = $this->getParent()) && $entity = $field_item->getEntity()) { + $langcode = $langcode ?: $entity->language()->getId(); + $alias = $this->getAliasStorage()->lookupPathAlias('/' . $entity->toUrl()->getInternalPath(), $langcode); + // Mimic the field structure that would be returned if this field + // contained any items. + $this->alias = $alias; + } + return $this->alias; + } + + /** + * The path alias storage. + * + * @return \Drupal\Core\Path\AliasStorageInterface + */ + protected function getAliasStorage() { + return \Drupal::service('path.alias_storage'); + } + +} diff --git a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php index 239588fd51..5b5f2cec36 100644 --- a/core/modules/path/src/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/src/Plugin/Field/FieldType/PathItem.php @@ -7,6 +7,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\TypedData\DataDefinition; +use Drupal\path\PathAliasComputed; /** * Defines the 'path' entity field type. @@ -27,6 +28,8 @@ class PathItem extends FieldItemBase { */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['alias'] = DataDefinition::create('string') + ->setComputed(TRUE) + ->setClass(PathAliasComputed::class) ->setLabel(t('Path alias')); $properties['pid'] = DataDefinition::create('integer') ->setLabel(t('Path id'));