You are here

public function MessageActionForm::submitForm in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Form/MessageActionForm.php \Drupal\rng\Form\MessageActionForm::submitForm()
  2. 3.x src/Form/MessageActionForm.php \Drupal\rng\Form\MessageActionForm::submitForm()

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

src/Form/MessageActionForm.php, line 118

Class

MessageActionForm
Creates a rule with a rng_courier_message action.

Namespace

Drupal\rng\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $this->actionPlugin
    ->submitConfigurationForm($form, $form_state);
  if (!($template_collection = $this->actionPlugin
    ->getTemplateCollection())) {
    drupal_set_message(t('Unable to create templates.', 'error'));
    return;
  }
  $event = $form_state
    ->get('event');
  $context = $this->entityManager
    ->getStorage('courier_context')
    ->load('rng_registration_' . $event
    ->getEntityTypeId());
  if (!$context) {
    throw new \Exception(sprintf('No context available for %s', $event
      ->getEntityTypeId()));
  }
  $template_collection
    ->setContext($context);
  $template_collection
    ->setOwner($event);
  $template_collection
    ->save();
  drupal_set_message(t('Templates created.'));
  $action = RuleComponent::create([])
    ->setPluginId($this->actionPlugin
    ->getPluginId())
    ->setConfiguration($this->actionPlugin
    ->getConfiguration())
    ->setType('action');
  $trigger_id = $form_state
    ->getValue('trigger');
  $rule = Rule::create([
    'event' => array(
      'entity' => $event,
    ),
    'trigger_id' => $trigger_id,
  ]);
  $rule
    ->save();
  $action
    ->setRule($rule)
    ->save();
  if ($trigger_id == 'rng:custom:date') {
    $rule_component = RuleComponent::create()
      ->setRule($rule)
      ->setType('condition')
      ->setPluginId('rng_rule_scheduler');
    $rule_component
      ->save();

    // Save the ID into config.
    $rule_component
      ->setConfiguration([
      'rng_rule_component' => $rule_component
        ->id(),
    ]);
    $rule_component
      ->save();
  }
  $entity_type = $event
    ->getEntityTypeId();
  $form_state
    ->setRedirectUrl(Url::fromRoute('rng.event.' . $entity_type . '.messages', [
    $entity_type => $event
      ->id(),
  ]));
}