You are here

public function Subscriptions::submitForm in Message Subscribe 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

message_subscribe_ui/src/Plugin/Block/Subscriptions.php, line 241

Class

Subscriptions
An entity subscriptions block.

Namespace

Drupal\message_subscribe_ui\Plugin\Block

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  foreach ($form_state
    ->getValue('subscriptions') as $entity_type => $entities) {
    foreach ($entities as $entity_id => $subscribe) {

      /** @var \Drupal\flag\FlagInterface[] $flags */
      $flags = $form['subscriptions'][$entity_type][$entity_id]['#flags'];
      $entity = $form['subscriptions'][$entity_type][$entity_id]['#entity'];
      foreach ($flags as $flag) {
        try {
          if ($subscribe) {
            $this->flagService
              ->flag($flag, $entity, $this->currentUser);
          }
          else {
            $this->flagService
              ->unflag($flag, $entity, $this->currentUser);
          }
        } catch (\LogicException $e) {

          // User was either already unsubscribed, or subscribed.
        }
      }
    }
  }
}