You are here

class ExtlinkSettingsSaveEventSubscriber in External Links 8

Clears JS and asset libraries in response to changes in extlink settings.

Hierarchy

Expanded class hierarchy of ExtlinkSettingsSaveEventSubscriber

1 string reference to 'ExtlinkSettingsSaveEventSubscriber'
extlink.services.yml in ./extlink.services.yml
extlink.services.yml
1 service uses ExtlinkSettingsSaveEventSubscriber
extlink.setting_save.subscriber in ./extlink.services.yml
Drupal\extlink\EventSubscriber\ExtlinkSettingsSaveEventSubscriber

File

src/EventSubscriber/ExtlinkSettingsSaveEventSubscriber.php, line 14

Namespace

Drupal\extlink\EventSubscriber
View source
class ExtlinkSettingsSaveEventSubscriber implements EventSubscriberInterface {

  /**
   * The CSS/JS asset library discovery service.
   *
   * @var \Drupal\Core\Asset\LibraryDiscoveryInterface
   */
  protected $libraryDiscovery;

  /**
   * The JS asset optimizer service.
   *
   * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
   */
  protected $jsOptimizer;

  /**
   * ExtlinkSettingsSaveEventSubscriber constructor.
   *
   * @param \Drupal\Core\Asset\LibraryDiscoveryInterface $library_discovery
   *   The CSS/JS asset library discovery service.
   * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_optimizer
   *   The JS asset optimizer service.
   */
  public function __construct(LibraryDiscoveryInterface $library_discovery, AssetCollectionOptimizerInterface $js_optimizer) {
    $this->libraryDiscovery = $library_discovery;
    $this->jsOptimizer = $js_optimizer;
  }

  /**
   * Acts on changes to extlink.settings to flush JS library and assets.
   *
   * @param \Drupal\Core\Config\ConfigCrudEvent $event
   *   The configuration event.
   */
  public function onConfigSave(ConfigCrudEvent $event) {
    $config = $event
      ->getConfig();
    if ($config
      ->getName() === 'extlink.settings') {
      $flush_js_files = $config
        ->get('extlink_use_external_js_file');
      if ($event
        ->isChanged('extlink_use_external_js_file')) {

        // When using external JS file is enabled or disabled, need to flush the
        // library discovery cache to update the dependencies of drupal.extlink
        // library.
        $this->libraryDiscovery
          ->clearCachedDefinitions();
        $flush_js_files = TRUE;
      }
      if ($flush_js_files) {

        // Flush the optimized JS files if using an external JS file when the
        // settings are saved. Also flush the optimized JS files when disabling
        // or enabling using the external JS files.
        $this->jsOptimizer
          ->deleteAll();
        _drupal_flush_css_js();
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ConfigEvents::SAVE => 'onConfigSave',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExtlinkSettingsSaveEventSubscriber::$jsOptimizer protected property The JS asset optimizer service.
ExtlinkSettingsSaveEventSubscriber::$libraryDiscovery protected property The CSS/JS asset library discovery service.
ExtlinkSettingsSaveEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ExtlinkSettingsSaveEventSubscriber::onConfigSave public function Acts on changes to extlink.settings to flush JS library and assets.
ExtlinkSettingsSaveEventSubscriber::__construct public function ExtlinkSettingsSaveEventSubscriber constructor.