Advertising sustains the DA. Ads are hidden for members. Join today

Reacting to changes

Last updated on
25 October 2017

Sometimes it is necessary to modify the site behavior when a feature is enabled or disabled. For instance, could be necessary to notify to a specific user or enabled a 3rd module.

That's the reason why Feature Toggle module provides Event API integration.

To create an event subscriber to react to Feature Toggle events, you can follow this great tutorial: https://www.chapterthree.com/blog/how-to-register-event-subscriber-drupal8

Here is a small example of EventSubscriber class implementation:

/**
 * Event Subscriber FeatureEventSubscriber.
 */
class FeatureEventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    // Here is a generic event to react to changes in any feature.
    $events[FeatureUpdateEvents::UPDATE][] = ['genericResponse'];
    // Here is an event to react only to changes in 'my_feature' feature.
    $events[FeatureUpdateEvents::UPDATE . '.my_feature'][] = ['myFeatureResponse'];
    return $events;
  }

  /**
   * Code that should be triggered on any feature change. 
   */
  public function genericResponse(FilterResponseEvent $event) {
    drupal_set_message(t('Feature @feature updated. New status: @status, [
      '@feature' => $event->feature()->label(),
      '@status' => $event->status(),
    ]));
  }

  /**
   * Code that should be triggered on 'my_feature' change. 
   */
  public function myFeatureResponse(FilterResponseEvent $event) {
    drupal_set_message(t('My Feature has been updated. New status: @status, [
      '@status' => $event->status(),
    ]));
  }

}

Help improve this page

Page status: No known problems

You can: