You are here

protected function PathFieldItemList::computeValue in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/path/src/Plugin/Field/FieldType/PathFieldItemList.php \Drupal\path\Plugin\Field\FieldType\PathFieldItemList::computeValue()

Computes the values for an item list.

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);
}