This is some code from my theme_menu_link in Drupal 7.

function mytheme_menu_link($variables) {
  $element = $variables['element'];
  if(arg(0) === 'x') {
    if($element['#href'] === '<front>') {
      $element['#href'] = 'x';
    } else {
      $element['#href'] = 'x/' . $element['#href'];

At this point, I just need to do the same thing in Drupal 8: If the visitor arrived at the page with an arg(0) of 'x', then prepend all of the menu links with 'x' keeping them in the "x" experience of the site.

I appreciate any help you can offer. Please provide detail if possible.

  • If you want to keep the visitors in the "x" experience on the whole site then you need probably a path processor, see the code example in this topic drupal.stackexchange.com/questions/230171/… – 4k4 Apr 14 '18 at 8:35
  • In Drupal 8, you can use $arg0 = explode('/', \Drupal::service('path.current')->getPath())[1]; instead of arg(0). – kenorb Dec 7 '18 at 11:47

Browse other questions tagged or ask your own question.