2017-10-19 171 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 –