【问题标题】:Drupal 8 Passing global variables from settings.php to twig and/or js filesDrupal 8 将全局变量从 settings.php 传递到 twig 和/或 js 文件
【发布时间】:2017-10-19 05:25:08
【问题描述】:

我真的很想知道如何将我的变量全局传递(页面级别),以便它可以在任何地方使用。

我做了什么:

在我的组 vars > dev.yml

link: "www.anylink.com"

在我的组 vars > prod.yml

link: "www.anylink-prod.com"

关于我的 settings.php (j2)

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

在我的 template.theme 上

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

在我的树枝上 {{ 主题链接 }}

但它确实没有从我的 prod/dev.yml 打印任何字符串.. 我想知道怎么了?

我这样做的主要目的是,

我想要一个链接,打印取决于我所处的环境。 希望有人能帮我解决这个问题,谢谢!

【问题讨论】:

    标签: php symfony drupal drupal-8


    【解决方案1】:
    1. 对于全局 twig 变量:

    不使用HOOKs的OOP方式:

    创建一个RequestListener放在(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'));
      }
    
    }
    

    并且您必须在您的 MY_MODULE.service.yml 中将其注册为服务,例如:

    my_requestlistener:
      class: Drupal\<MY_MODULE>\Event\Listener\MyRequestListener
      arguments:
        - @twig
        - @settings
    
    1. 对于全局 JS 设置:

    在您的 MY_MODULE.module 文件中创建一个挂钩:

    <?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')
    
     }
    

    【讨论】:

    • 嗨@Rainer,我认为这是你的代码是我正在寻找的......只是我想问一下“MY_MODULE”是否意味着我需要创建一个模块并首先安装它?或者我可以使用任何自定义名称而不需要安装它吗?
    • 嗨,马丁,是的,您需要为我编写的所有代码定制一个模块。但这很容易。只需使用 drupal 控制台生成骨架模块。 MY_MODULE 是您将给它的机器友好名称。看看这个drupal.org/docs/8/creating-custom-modules
    【解决方案2】:

    Fot 全局 TWIG 变量 上面的代码对我不起作用,直到我将 event_subscriber 标记添加到服务定义中。因此将以下内容放入 MY_MODULE.services.yml 中:

      my_requestlistener:
        class: Drupal\<MY_MODULE>\Event\Listener\MyRequestListener
        arguments: ['@twig', '@settings']
        tags:
          - { name: event_subscriber }
    

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 2019-04-03
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      相关资源
      最近更新 更多