You are here

public function MessageForm::validateForm in Courier 8

Same name and namespace in other branches
  1. 2.x courier_message_composer/src/Form/MessageForm.php \Drupal\courier_message_composer\Form\MessageForm::validateForm()

@inheritDoc

Overrides FormBase::validateForm

File

courier_message_composer/src/Form/MessageForm.php, line 170

Class

MessageForm
Create a message.

Namespace

Drupal\courier_message_composer\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\courier\ChannelInterface $message */
  $message = $form_state
    ->get('message_entity');
  $form_state
    ->get([
    'form_display',
  ])
    ->validateFormValues($message, $form, $form_state);

  // Identity.
  if ($form_state
    ->getValue('identity')) {
    $identity = NULL;
    list($entity_type, $entity_id) = explode(':', $form_state
      ->getValue('identity'));
    if (!empty($entity_type)) {
      $references = $form_state
        ->getValue('entity');
      if (is_numeric($references[$entity_type])) {
        $entity_id = $references[$entity_type];
        $identity = $this->entityManager
          ->getStorage($entity_type)
          ->load($entity_id);
      }
    }
    if ($identity instanceof EntityInterface) {
      $form_state
        ->setValueForElement($form['identity_information']['identity'], $identity);
      $channel_types = $this->identityChannelManager
        ->getChannelsForIdentity($identity);
      if (!in_array($message
        ->getEntityTypeId(), $channel_types)) {
        $form_state
          ->setError($form['identity_information']['identity'], $this
          ->t('@identity cannot receive @channel.', [
          '@identity' => $identity
            ->label(),
          '@channel' => $message
            ->getEntityType()
            ->getLabel(),
        ]));
      }
    }
    else {
      $form_state
        ->setError($form['identity_information']['identity'], $this
        ->t('Invalid identity.'));
    }
  }
}