You are here

public function SettingsForm::validateForm in Entity Share Cron 8

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\entity_share_cron\Form\SettingsForm::validateForm()
  2. 3.0.x src/Form/SettingsForm.php \Drupal\entity_share_cron\Form\SettingsForm::validateForm()

Form validation 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 FormBase::validateForm

File

src/Form/SettingsForm.php, line 170

Class

SettingsForm
Module settings form.

Namespace

Drupal\entity_share_cron\Form

Code

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

  // Checks if every enabled remote has at least one channel enabled.
  $remotes = $form_state
    ->getValue('remotes');
  foreach ($remotes as $remote_id => $remote_config) {
    if (!empty($remote_config['enabled'])) {

      // Searches for an enabled channel.
      $channel_enabled = FALSE;
      $channels = isset($remote_config['channels']) ? $remote_config['channels'] : [];
      foreach ($channels as $channel_id => $channel_config) {
        if (!empty($channel_config['enabled'])) {
          $channel_enabled = TRUE;

          // Checks if at least one operation is enabled.
          if (!array_filter($channel_config['operations'])) {
            $element =& $form['remotes'][$remote_id]['channels'][$channel_id];
            $form_state
              ->setError($element, $this
              ->t('No operations enabled for channel %channel of remote %remote.', [
              '%channel' => $element['enabled']['#title'],
              '%remote' => $form['remotes'][$remote_id]['enabled']['#title'],
            ]));
          }
        }
      }

      // Shows an error if no enabled channel could be found.
      if (!$channel_enabled) {
        $element =& $form['remotes'][$remote_id];
        $form_state
          ->setError($element, $this
          ->t('No channels enabled for remote %remote.', [
          '%remote' => $element['enabled']['#title'],
        ]));
      }
    }
  }
}