You are here

public function MongodbPathAliasStorage::save in MongoDB 8

Saves a path alias to the database.

@thrown \InvalidArgumentException Thrown when either the source or alias has not a starting slash.

Parameters

string $source: The internal system path.

string $alias: The URL alias.

string $langcode: (optional) The language code of the alias.

int|null $pid: (optional) Unique path alias identifier.

Return value

array|false FALSE if the path could not be saved or an associative array containing the following keys:

  • source (string): The internal system path with a starting slash.
  • alias (string): The URL alias with a starting slash.
  • pid (int): Unique path alias identifier.
  • langcode (string): The language code of the alias.
  • original: For updates, an array with source, alias and langcode with the previous values.

Overrides AliasStorageInterface::save

File

src/MongodbPathAliasStorage.php, line 60
Contains Drupal\mongodb\Path.

Class

MongodbPathAliasStorage
Provides a class for CRUD operations on path aliases in MongoDB.

Namespace

Drupal\mongodb

Code

public function save($source, $alias, $langcode = Language::LANGCODE_NOT_SPECIFIED, $pid = NULL) {
  $fields = array(
    'source' => $source,
    'alias' => $alias,
    'langcode' => $langcode,
  );
  if ($pid) {
    $hook = 'path_update';
  }
  else {
    $pid = $this->mongo
      ->nextId();
    $hook = 'path_insert';
  }
  $response = $this
    ->mongoCollection()
    ->update([
    '_id' => $pid,
  ], [
    '$set' => $fields,
  ], [
    'upsert' => TRUE,
  ]);
  if (empty($response['err'])) {
    $fields['pid'] = $pid;
    $this->module_handler
      ->invokeAll($hook, $fields);
    return $fields;
  }
  return FALSE;
}