function PathFieldItemList::computeValue

Same name and namespace in other branches
  1. 8.9.x core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php \Drupal\path\Plugin\Field\FieldType\PathFieldItemList::computeValue()
  2. 10 core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php \Drupal\path\Plugin\Field\FieldType\PathFieldItemList::computeValue()
  3. 11.x core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php \Drupal\path\Plugin\Field\FieldType\PathFieldItemList::computeValue()

Overrides ComputedItemListTrait::computeValue

File

core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php, line 20

Class

PathFieldItemList
Represents a configurable entity path field.

Namespace

Drupal\path\Plugin\Field\FieldType

Code

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()) {
        
        /** @var \Drupal\path_alias\AliasRepositoryInterface $path_alias_repository */
        $path_alias_repository = \Drupal::service('path_alias.repository');
        if ($path_alias = $path_alias_repository->lookupBySystemPath('/' . $entity->toUrl()
            ->getInternalPath(), $this->getLangcode())) {
            $value = [
                'alias' => $path_alias['alias'],
                'pid' => $path_alias['id'],
                'langcode' => $path_alias['langcode'],
            ];
        }
    }
    $this->list[0] = $this->createItem(0, $value);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.