Get path alias of node on presave I was trying to get the path alias using for nodes using the following code.

\Drupal::service('path.alias_manager')-&gt;getAliasByPath('/node/' . $nid);


It works for nodes that are already created, but it breaks when a new node is being added. I get the following error.


  Uncaught PHP Exception Symfony\Component\Routing\Exception\InvalidParameterException: "Parameter "node" for route "entity.node.canonical" must match "\d+" ("" given) to generate a corresponding URL.


Is there an alternative way to get the path alias for nodes that have not been saved yet?
Aliases are alternative paths to the node. They require the canonical path, so that there is a path to be an alternative of. The canonical path requires the Node ID (Entity ID) as node paths are /node/[NODE ID]. The Node ID doesn't exist until the node has been saved, as it comes from the database upon save. So the path does not exist until the node has been saved, meaning there is nothing to alias.

So the answer is that no, it's impossible to create an alias before a node has been saved, as the canonical path does not and cannot exist until the node has been saved.