I'm currently importing data from a database to create nodes based on them. My node save code looks like this
$node = \Drupal\node\Entity\Node::create([
'type' => 'issue',
'title' => $title,
'body' => $body,
'field_issue_category' => $term,
'field_issue_type' => 'publication',
'field_pub_id' => [
'target_id' => $publication_id,
],
'field_issue_group' => $group_id,
'field_date' => $date,
]);
$node->save();
I also want to create a url alias based on the title of this node, which I create like this,
$nid = $node->id();
$title = preg_replace("/[\s_]/", "-", $title);
$path = \Drupal::service('path.alias_storage')->save('/node/'.$nid, '/mypath/'.$title);
This successfully create a url alias that shows up in the URL aliases list in the URL alias config. However it doesn't show up in the URL alias field on the node edit page.
This creates a problem. One, the user can't see what the actual alias is. Also I have a block plugin that puts every node of that type in a list on a page. It also displays the url to that node. I'd like the url displayed to be the alias like '/mypath/title' but Drupal only seems to know about the '/node/nid' path.
Is there a proper way to programmatically set a nodes url alias so that it still appears in the url alias field?