There are several things I notice here:
An undefined index error shouldn't result just from this code - you're not referencing the array index; just setting it. Does the actual code have a more complex expression which references $links['node.add.article']
?
This is not quite correct for this hook (and the linked issue is for 7.x, so it can't be compared). The hook documentation says that the menu item must be an associative array - it should be altered with $links['node.add.page']['title'] = '...';
The array key is not guaranteed to be fixed:
Since the machine name may be arbitrary, you should never write code that assumes it is identical to the route name.
Therefore you should probably iterate and identify the menu link by the route name instead.
- From what I can tell, core doesn't (and never did) define menu links for the
node.add.{type}
routes. The links on the /node/add
page are dynamically generated, and don't actually show up in any menu.
So it doesn't seem to be possible to alter the links on the /node/add
page by altering the menu. As you can see from the function below, these links are actually generated in a template preprocessor:
function template_preprocess_node_add_list(&$variables) {
$variables['types'] = [];
if (!empty($variables['content'])) {
foreach ($variables['content'] as $type) {
$variables['types'][$type->id()] = [
'type' => $type->id(),
'add_link' => \Drupal::l($type->label(), new Url('node.add', ['node_type' => $type->id()])),
'description' => [
'#markup' => $type->getDescription(),
],
];
}
}
}
The only way to modify the link text would be to replace the links in another preprocess function.
Edit: This other answer actually explains how to do that in detail: How to modify the link titles shown in /node/add page?