2017-10-19 170 views
0

我真的想知道如何传递我的变量globaly(页面级),以便它可以在任何地方使用。Drupal 8将settings.php中的全局变量传递给twig和/或js文件

我做了什么:

在我的组瓦尔> dev.yml

link: "www.anylink.com" 

在我的组瓦尔> 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'); 
} 

{{theme_link}}

,但它确实不打印从我刺的/ dev任何字符串。 yml .. 我想知道什么是错的?

我的主要目的,这样做是

我想有打印取决于我是什么样的环境上的链接。 希望任何人都可以在这个问题上点亮我,谢谢你!

回答

0
  1. 对于全球树枝varibales:

的面向对象的方式,而不使用挂钩:

创建放在(MY_MODULE/src目录/事件/监听器)一个RequestListener:

<?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 
  • 全球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') 
    
    } 
    
    +0

    @Rainer嗨,我觉得这是你的代码是我在找什么.. 这只是我想问是否通过“MY_MODULE”这意味着我需要创建一个模块并首先安装它?或者我可以只使用任何自定义名称而无需安装它? –

    +0

    嗨马丁, 是的,你需要一个自定义模块为我写的所有代码。但这很容易。只需使用drupal控制台来生成一个框架模块。 MY_MODULE是机器友好的名称,你会给它。看看这个https://www.drupal.org/docs/8/creating-custom-modules –