user739350 user739350 - 1 month ago 14x
Twig Question

Drupal 8 how to pass a value to menu twig template

I am new to Drupal, I want to pass domain URL to use in

template. How can I do this. Actually I have a menu of 5 domains all pointing to the same Drupal instance. Based on the domain name I want to make the menu active.



Extending of Artreaktors answer since the OP isn't too familiar with Drupal.

Create a module with any name you want. If you need help with this take a look at Drupal Console, it will help you a bunch.

Inside the folder of your module you will find a module_name.module file.

Here you will add the code given by Artreaktor:

function mymodule_preprocess_menu(&$vars) {
  $domain = \Drupal::service('domain.negotiator')->getActiveDomain();
  $vars['domain'] = $domain->getUrl();

mymodule here stands for the name of your module. Don't forget to enable your module and clearing the cache!