public function Path::save

Saves a path alias to the database.

Parameters

string $source: The internal system path.

string $alias: The URL alias.

string $langcode: The language code of the alias.

int $pid: Unique path alias identifier.

Return value

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

  • source: The internal system path.
  • alias: The URL alias.
  • pid: Unique path alias identifier.
  • langcode: The language code of the alias.

File

drupal/core/lib/Drupal/Core/Path/Path.php, line 67
Contains Drupal\Core\Path\Path.

Class

Path
Defines a class for CRUD operations on path aliases.

Namespace

Drupal\Core\Path

Code

public function save($source, $alias, $langcode = Language::LANGCODE_NOT_SPECIFIED, $pid = NULL) {
  $fields = array(
    'source' => $source,
    'alias' => $alias,
    'langcode' => $langcode,
  );

  // Insert or update the alias.
  if (empty($pid)) {
    $query = $this->connection
      ->insert('url_alias')
      ->fields($fields);
    $pid = $query
      ->execute();
    $fields['pid'] = $pid;

    // @todo: Find a correct place to invoke hook_path_insert().
    $hook = 'path_insert';
  }
  else {
    $fields['pid'] = $pid;
    $query = $this->connection
      ->update('url_alias')
      ->fields($fields)
      ->condition('pid', $pid);
    $pid = $query
      ->execute();

    // @todo: figure out where we can invoke hook_path_update()
    $hook = 'path_update';
  }
  if ($pid) {

    // @todo Switch to using an event for this instead of a hook.
    module_invoke_all($hook, $fields);
    $this->alias_manager
      ->cacheClear();
    return $fields;
  }
  return FALSE;
}