You are here

public function SettingsForm::validateForm in Entity Share Cron 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Form/SettingsForm.php \Drupal\entity_share_cron\Form\SettingsForm::validateForm()
  2. 8 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 209

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');
  if (is_array($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;
            $element =& $form['remotes'][$remote_id]['channels'][$channel_id];

            // Checks if an import config had been selected.
            if (empty($channel_config['import_config'])) {
              $form_state
                ->setError($element, $this
                ->t('No import config selected for channel %channel of remote %remote.', [
                '%channel' => $element['enabled']['#title'],
                '%remote' => $form['remotes'][$remote_id]['enabled']['#title'],
              ]));
            }

            // Checks if at least one operation is enabled.
            if (!array_filter($channel_config['operations'])) {
              $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'],
          ]));
        }
      }
    }
  }
}