2017-10-11 13 views
0

こんにちはすべてDrupal 8 EventSubscriberクラスはdrupalSettingsをDrupal 8 JavaScript設定に添付することになっていました。しかし、残念ながら、この\ Drupal :: service( 'renderer') - > renderRoot($ js_data); drupalのjavascript設定を添付していません。Drupal 8 #attachedはeventsubscriber内で動作しません

namespace Drupal\ejectorseat\EventSubscriber; 

use Symfony\Component\EventDispatcher\EventSubscriberInterface; 
use Symfony\Component\HttpKernel\Event\GetResponseEvent; 
use Symfony\Component\HttpKernel\KernelEvents; 
use Drupal\Core\Url; 

class EjectorseatSubscriber implements EventSubscriberInterface { 

    public function customEjector(GetResponseEvent $event) { 
     $account = \Drupal::currentUser(); 
     $ejectorseat_interval = \Drupal::config('ejectorseat.settings')->get('ejectorseat_interval'); 
     if(empty($ejectorseat_interval)){ 
     $ejectorseat_interval = 60; 
     } 
     $ejectorseat_background = \Drupal::config('ejectorseat.settings')->get('ejectorseat_background'); 
     if(empty($ejectorseat_background)){ 
     $ejectorseat_background = 0; 
     } 

     if ($account->id() > 0 && (int) $ejectorseat_interval) { 
      $js_data = array(); 
      $js_data['#attached']['drupalSettings']['ejectorSeat']['interval'] = $ejectorseat_interval; 
      $js_data['#attached']['drupalSettings']['ejectorSeat']['url'] = Url::fromRoute('ejectorseat/check'); 
      $js_data['#attached']['drupalSettings']['ejectorSeat']['ignoreFocus'] = $ejectorseat_background ? TRUE : FALSE; 
      \Drupal::service('renderer')->renderRoot($js_data); 
     } 
    } 

    public static function getSubscribedEvents() { 
    $events[KernelEvents::REQUEST][] = array('customEjector'); 
    return $events; 
    } 
} 

答えて

0

私は同じ問題に直面していました。私はOOPソリューションを見つけられませんでした。 あなたのケースでは、ロジックフォームcustomEjectorメソッドをhook_js_settings_alterというejectorseat.moduleファイルに入れてください。このようにして、drupalSettingsオブジェクトですべての変数を使用できることを確認できます。ただ、デモ用

例えば:

<?php 
/** 
* Implements hook_js_settings_alter(). 
* 
*/ 
function ejectorseat_js_settings_alter(array &$settings, 
    \Drupal\Core\Asset\AttachedAssetsInterface $assets 
) { 
    ... 

     $settings['all_settings'] = $globalSettings->getAll(); 
     //if you want to push only a single value 
     $settings['custom_link'] = $globalSettings->get('custom_link') 
     $settings['ejectorSeat']['interval'] = $ejectorseat_interval; 
     $settings['ejectorSeat']['url'] = Url::fromRoute('ejectorseat/check'); 
     $settings['ejectorSeat']['ejectorSeat']['ignoreFocus'] = $ejectorseat_background ? TRUE : FALSE; 

} 
関連する問題