The simplest way to re-generate the path for an entity is to set the pathauto setting to the constant "PathautoState::CREATE" and then save the entity.
For example, to do this with a node.
use Drupal\node\Entity\Node
use Drupal\pathauto\PathautoState;
\Drupal::entityTypeManager()->getStorage('node');
$node = Node::load(123);
$node->path->pathauto = PathautoState::CREATE;
$node->save();
The page will regenerate the path from scratch, which will create a new path.
This works for any entity that has path auto settings.
Alternatively, you can use the "pathauto.generator" service to do the same. The method "updateEntityAlias()" is commonly used by the module to update paths.
To add a path to an entity run the following.
\Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'update');
If the entity already has a valid path then this will not be altered as path auto has a few checks to make sure that doesn't happen.
\Drupal::service('pathauto.generator')->updateEntityAlias($entity, 'update', ['force' => true]);
This will force the path to be updated.
Add new comment