function PathItem::postSave

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

Overrides FieldItemBase::postSave

File

core/modules/path/src/Plugin/Field/FieldType/PathItem.php, line 71

Class

PathItem
Defines the 'path' entity field type.

Namespace

Drupal\path\Plugin\Field\FieldType

Code

public function postSave($update) {
  $path_alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
  $entity = $this->getEntity();
  $alias = $this->get('alias')
    ->getValue();
  $pid = $this->get('pid')
    ->getValue();
  $langcode = $this->get('langcode')
    ->getValue();
  // If an explicit alias langcode is provided, use it. This allows code to
  // preserve an existing alias language or set a specific language,
  // including language-neutral, when creating or updating an alias.
  // Otherwise, fall back to the field/entity langcode.
  $alias_langcode = $langcode ?: $this->getLangcode();
  // If we have an alias, we need to create or update a path alias entity.
  if ($alias) {
    $properties = [
      'path' => '/' . $entity->toUrl()
        ->getInternalPath(),
      'alias' => $alias,
      'langcode' => $alias_langcode,
    ];
    if (!$pid) {
      // Try to load it from storage before creating it. In some cases the
      // path alias could be created before this function runs. For example,
      // \Drupal\workspaces\EntityOperations::entityTranslationInsert will
      // create a translation, and an associated path alias will be created
      // with it.
      $query = $path_alias_storage->getQuery()
        ->accessCheck(FALSE);
      foreach ($properties as $field => $value) {
        $query->condition($field, $value);
      }
      $ids = $query->execute();
      $pid = $ids ? reset($ids) : $pid;
    }
    if (!$pid) {
      $path_alias = $path_alias_storage->create($properties);
      $path_alias->save();
      $this->set('pid', $path_alias->id());
    }
    else {
      $path_alias = $path_alias_storage->load($pid);
      $path_alias_changed = FALSE;
      if ($alias != $path_alias->getAlias()) {
        $path_alias->setAlias($alias);
        $path_alias_changed = TRUE;
      }
      // Persist langcode-only updates when the alias text is unchanged.
      if ($alias_langcode !== $path_alias->language()
        ->getId()) {
        $path_alias->set('langcode', $alias_langcode);
        $path_alias_changed = TRUE;
      }
      if ($path_alias_changed) {
        $path_alias->save();
      }
    }
  }
  elseif ($pid) {
    // Otherwise, delete the old alias if the user erased it.
    $path_alias = $path_alias_storage->load($pid);
    if ($entity->isDefaultRevision()) {
      $path_alias_storage->delete([
        $path_alias,
      ]);
    }
    else {
      $path_alias_storage->deleteRevision($path_alias->getRevisionID());
    }
  }
}

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