Question:

Change name to menu link dinamically

Greyson: 5 days ago

I have a menu:

Parent-1
 - Child-1-1
 - Child-1-2
 - Child-1-3
 - another_child
Parent-2
 - Child-2-1
 - Child-2-2
 - another_child

I needed change the name of Parent-2 item (Parent-2 text is unique), I did it with hook_link_alter() hook:

/**
 * Implements hook_link_alter().
 */
function MY_MODULE_link_alter(&$variables) {
  if ($variables['text'] == 'Parent-2') {
     $variables['text'] = t('New name');
  }
}

I need too to change the names of all child of Parent-2 dynamically. I can't do it with hook_link_alter() hook because in my example there is two elements(another_child) with the same name. Those changes must not be saved in database.

How can I do this?

Answer:
Emilia: 5 days ago

I can do it implementing hook_preprocess_HOOK (https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21theme.api.php/function/hook_preprocess_HOOK/8.4.x) hook, specifically hook_preprocess_superfish:

/**
 * Implements hook_preprocess_HOOK().
 */
function MODULE_NAME_preprocess_superfish(&$variables) {
  if ($variables['id'] == 'superfish-main-menu') {
    $menu = ($variables['element']['#tree']);
    // ...
  }
}