function AliasStorage::save

Overrides AliasStorageInterface::save

File

core/lib/Drupal/Core/Path/AliasStorage.php, line 75

Class

AliasStorage
Provides a class for CRUD operations on path aliases.

Namespace

Drupal\Core\Path

Code

public function save($source, $alias, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid = NULL) {
    if ($source[0] !== '/') {
        throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $source));
    }
    if ($alias[0] !== '/') {
        throw new \InvalidArgumentException(sprintf('Alias path %s has to start with a slash.', $alias));
    }
    if ($pid) {
        
        /** @var \Drupal\path_alias\PathAliasInterface $path_alias */
        $path_alias = $this->getPathAliasEntityStorage()
            ->load($pid);
        $original_values = [
            'source' => $path_alias->getPath(),
            'alias' => $path_alias->getAlias(),
            'langcode' => $path_alias->get('langcode')->value,
        ];
        $path_alias->setPath($source);
        $path_alias->setAlias($alias);
        $path_alias->set('langcode', $langcode);
    }
    else {
        $path_alias = $this->getPathAliasEntityStorage()
            ->create([
            'path' => $source,
            'alias' => $alias,
            'langcode' => $langcode,
        ]);
    }
    $path_alias->save();
    $path_alias_values = [
        'pid' => $path_alias->id(),
        'source' => $path_alias->getPath(),
        'alias' => $path_alias->getAlias(),
        'langcode' => $path_alias->get('langcode')->value,
    ];
    if (isset($original_values)) {
        $path_alias_values['original'] = $original_values;
    }
    return $path_alias_values;
}

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