function EntityPathAliasCreate::doExecute

Creates entity path alias.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity that should get an alias.

string $alias: The alias to be created.

File

src/Plugin/RulesAction/EntityPathAliasCreate.php, line 75

Class

EntityPathAliasCreate
Provides a generic 'Create entity path alias' action.

Namespace

Drupal\rules\Plugin\RulesAction

Code

protected function doExecute(EntityInterface $entity, $alias) {
    // We need to save the entity before we can get its internal path.
    if ($entity->isNew()) {
        $entity->save();
    }
    $path = '/' . $entity->toUrl()
        ->getInternalPath();
    $langcode = $entity->language()
        ->getId();
    $path_alias = $this->aliasStorage
        ->create([
        'path' => $path,
        'alias' => $alias,
        'langcode' => $langcode,
    ]);
    $path_alias->save();
}