You are here

public function AliasStorageHelper::save in Pathauto 8

Private function for Pathauto to create an alias.

Parameters

array $path: An associative array containing the following keys:

  • source: The internal system path.
  • alias: The URL alias.
  • pid: (optional) Unique path alias identifier.
  • language: (optional) The language of the alias.

array|bool|null $existing_alias: (optional) An associative array of the existing path alias.

string $op: An optional string with the operation being performed.

Return value

array|bool The saved path or NULL if the path was not saved.

Overrides AliasStorageHelperInterface::save

File

src/AliasStorageHelper.php, line 98

Class

AliasStorageHelper
Provides helper methods for accessing alias storage.

Namespace

Drupal\pathauto

Code

public function save(array $path, $existing_alias = NULL, $op = NULL) {
  $config = $this->configFactory
    ->get('pathauto.settings');

  // Set up all the variables needed to simplify the code below.
  $source = $path['source'];
  $alias = $path['alias'];
  $langcode = $path['language'];
  if ($existing_alias) {

    /** @var \Drupal\path_alias\PathAliasInterface $existing_alias */
    $existing_alias = $this->entityTypeManager
      ->getStorage('path_alias')
      ->load($existing_alias['pid']);
  }

  // Alert users if they are trying to create an alias that is the same as the
  // internal system path.
  if ($source == $alias) {
    $this->messenger
      ->addMessage($this
      ->t('Ignoring alias %alias because it is the same as the internal path.', [
      '%alias' => $alias,
    ]));
    return NULL;
  }

  // Update the existing alias if there is one and the configuration is set to
  // replace it.
  if ($existing_alias && $config
    ->get('update_action') == PathautoGeneratorInterface::UPDATE_ACTION_DELETE) {

    // Skip replacing the current alias with an identical alias.
    if ($existing_alias
      ->getAlias() == $alias) {
      return NULL;
    }
    $old_alias = $existing_alias
      ->getAlias();
    $existing_alias
      ->setAlias($alias)
      ->save();
    $this->messenger
      ->addMessage($this
      ->t('Created new alias %alias for %source, replacing %old_alias.', [
      '%alias' => $alias,
      '%source' => $source,
      '%old_alias' => $old_alias,
    ]));
    $return = $existing_alias;
  }
  else {

    // Otherwise, create a new alias.
    $path_alias = $this->entityTypeManager
      ->getStorage('path_alias')
      ->create([
      'path' => $source,
      'alias' => $alias,
      'langcode' => $langcode,
    ]);
    $path_alias
      ->save();
    $this->messenger
      ->addMessage($this
      ->t('Created new alias %alias for %source.', [
      '%alias' => $path_alias
        ->getAlias(),
      '%source' => $path_alias
        ->getPath(),
    ]));
    $return = $path_alias;
  }
  return [
    'source' => $return
      ->getPath(),
    'alias' => $return
      ->getAlias(),
    'pid' => $return
      ->id(),
    'langcode' => $return
      ->language()
      ->getId(),
  ];
}