You are here

protected function FlagEvents::triggerEmailFlag in Message Subscribe 8

Flag or unflag the corresponding `email_*` flag for `subscribe_*` flags.

Parameters

\Drupal\flag\FlaggingInterface $flagging: The flagging object.

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.

2 calls to FlagEvents::triggerEmailFlag()
FlagEvents::onFlag in message_subscribe_email/src/EventSubscriber/FlagEvents.php
React to entity flagging.
FlagEvents::onUnflag in message_subscribe_email/src/EventSubscriber/FlagEvents.php
React to entity unflagging.

File

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

Class

FlagEvents
React to flag and unflag events.

Namespace

Drupal\message_subscribe_email\EventSubscriber

Code

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());
      }
    }
  }
}