You are here

class FlagEvents in Message Subscribe 8

React to flag and unflag events.

Hierarchy

  • class \Drupal\message_subscribe_email\EventSubscriber\FlagEvents implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of FlagEvents

1 string reference to 'FlagEvents'
message_subscribe_email.services.yml in message_subscribe_email/message_subscribe_email.services.yml
message_subscribe_email/message_subscribe_email.services.yml
1 service uses FlagEvents
message_subscribe_email.flag_subscriber in message_subscribe_email/message_subscribe_email.services.yml
\Drupal\message_subscribe_email\EventSubscriber\FlagEvents

File

message_subscribe_email/src/EventSubscriber/FlagEvents.php, line 17

Namespace

Drupal\message_subscribe_email\EventSubscriber
View source
class FlagEvents implements EventSubscriberInterface {

  /**
   * Config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The flag service.
   *
   * @var \Drupal\flag\FlagServiceInterface
   */
  protected $flagService;

  /**
   * Construct the flag event subscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\flag\FlagServiceInterface $flag_service
   *   The flag service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, FlagServiceInterface $flag_service) {
    $this->configFactory = $config_factory;
    $this->flagService = $flag_service;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[Flag::ENTITY_FLAGGED] = [
      'onFlag',
      50,
    ];
    $events[Flag::ENTITY_UNFLAGGED] = [
      'onUnflag',
      50,
    ];
    return $events;
  }

  /**
   * React to entity flagging.
   *
   * @param \Drupal\flag\Event\FlaggingEvent $event
   *   The flagging event.
   */
  public function onFlag(FlaggingEvent $event) {
    $this
      ->triggerEmailFlag($event
      ->getFlagging(), 'flag');
  }

  /**
   * React to entity unflagging.
   *
   * @param \Drupal\flag\Event\UnflaggingEvent $event
   *   The flagging event.
   */
  public function onUnflag(UnflaggingEvent $event) {

    // Unflagging can happen in bulk, so loop through all flaggings.
    foreach ($event
      ->getFlaggings() as $flagging) {
      $this
        ->triggerEmailFlag($flagging, 'unflag');
    }
  }

  /**
   * Flag or unflag the corresponding `email_*` flag for `subscribe_*` flags.
   *
   * @param \Drupal\flag\FlaggingInterface $flagging
   *   The flagging object.
   * @param string $action
   *   The action. Either 'flag' or 'unflag'.
   *
   * @throws \Drupal\message_subscribe\Exception\MessageSubscribeException
   *   If there isn't a corresponding `email_` flag for the given `subscribe_`
   *   flag.
   */
  protected function triggerEmailFlag(FlaggingInterface $flagging, $action) {
    if (strpos($flagging
      ->getFlagId(), $this->configFactory
      ->get('message_subscribe.settings')
      ->get('flag_prefix') . '_') === 0) {

      // The flag is a subscription flag.
      if ($flagging
        ->getOwner()->message_subscribe_email->value || $action == 'unflag') {

        // User wants to use email for the subscription, or the subscription is
        // being removed.
        $prefix = $this->configFactory
          ->get('message_subscribe.settings')
          ->get('flag_prefix');
        $email_flag_name = $this->configFactory
          ->get('message_subscribe_email.settings')
          ->get('flag_prefix') . '_' . str_replace($prefix . '_', '', $flagging
          ->getFlagId());
        $flag = $this->flagService
          ->getFlagById($email_flag_name);
        if (!$flag) {
          throw new MessageSubscribeException('There is no corresponding email flag (' . $email_flag_name . ') for the ' . $flagging
            ->getFlagId() . ' flag.');
        }
        if ($action === 'flag') {
          $this->flagService
            ->flag($flag, $flagging
            ->getFlaggable(), $flagging
            ->getOwner());
        }
        elseif ($this->flagService
          ->getFlagging($flag, $flagging
          ->getFlaggable(), $flagging
          ->getOwner())) {
          $this->flagService
            ->unflag($flag, $flagging
            ->getFlaggable(), $flagging
            ->getOwner());
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagEvents::$configFactory protected property Config factory service.
FlagEvents::$flagService protected property The flag service.
FlagEvents::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
FlagEvents::onFlag public function React to entity flagging.
FlagEvents::onUnflag public function React to entity unflagging.
FlagEvents::triggerEmailFlag protected function Flag or unflag the corresponding `email_*` flag for `subscribe_*` flags.
FlagEvents::__construct public function Construct the flag event subscriber.