phpsymfonydrupaldrupal-8

Drupal 8 Passing global variables from settings.php to twig and/or js files


i really wondering how to pass my variable globaly (page level) so it can be used anywhere.

What i have done:

on my group vars > dev.yml

link: "www.anylink.com"

on my group vars > prod.yml

link: "www.anylink-prod.com"

on my settings.php (j2)

$settings["custom_link"]={{link}};

on my template.theme

function theme_preprocess_page(&$variables) {
  $variables[theme_link] = Settings::get('custom_link');
}

on my twig {{ theme_link }}

but it really doesn't print any string from my prod/dev.yml.. i was wondering what is wrong?

my main aim doing this is,

i want to have a links that print depends on what environment i am on. hopefully anybody can light me up on this problem, THank you !


Solution

    1. For global twig varibales:

    The OOP way without using HOOKs:

    create a RequestListener placed in (MY_MODULE/src/Event/Listener):

    <?php
    
    namespace Drupal\<MY_MODULE>\Event\Listener;
    
    use Drupal\Core\Site\Settings;
    use Drupal\Core\Template\TwigEnvironment;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\HttpKernel\Event\GetResponseEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    
    /**
     * Class MyRequestListener
     *
     */
    class MyRequestListener implements EventSubscriberInterface {
    
      /**
       * @var \Drupal\Core\Template\TwigEnvironment
       */
      protected $twigEnvironment;
    
      /**
       * @var \Drupal\Core\Site\Settings
       */
      protected $settings;
    
    
      /**
       * FdeRequestListener constructor.
       */
      public function __construct(TwigEnvironment $twigEnvironment,
                                  Settings $settings) {
    
        $this->twigEnvironment = $twigEnvironment;
        $this->settings        = $settings;
      }
    
      /**
       * @return mixed
       */
      public static function getSubscribedEvents() {
        $events[KernelEvents::REQUEST][] = ['onRequest'];
        return $events;
      }
    
      /**
       * @param GetResponseEvent $e
       */
      public function onRequest(GetResponseEvent $e) {
        //here you can add everything which is then globally accessible in twig templates like {{ custom_link }}
        $this->twigEnvironment->addGlobal('custom_link', $this->settings->get('custom_link'));
      }
    
    }
    

    and you have to register it as a service in your MY_MODULE.service.yml like:

    my_requestlistener:
      class: Drupal\<MY_MODULE>\Event\Listener\MyRequestListener
      arguments:
        - @twig
        - @settings
    
    1. for global JS settings:

    create a hook in your MY_MODULE.module file:

    <?php
    /**
     * Implements hook_js_settings_alter().
     * - adds "custom_link" to the drupalSettings
     */
    function my_module_js_settings_alter(array &$settings,
        \Drupal\Core\Asset\AttachedAssetsInterface $assets
    ) {
          /* @var $settings Drupal\Core\Site\Settings */
          $globalSettings = \Drupal::service('settings');
    
          //if you want to push all settings into drupalSettings JS object
          $settings['all_settings'] = $globalSettings->getAll();
          //if you want to push only a single value
          $settings['custom_link'] = $globalSettings->get('custom_link')
    
     }