You are here

public function ExposedGroups::updateGroupsValidate in Views exposed groups 3.0.x

Validate the groups text area.

Parameters

array &$element: The form element array.

\Drupal\Core\Form\FormStateInterface $formState: The form state.

File

src/Plugin/views/exposed_form/ExposedGroups.php, line 264

Class

ExposedGroups
Provides an views exposed form plugin that groups filters.

Namespace

Drupal\views_exposed_groups\Plugin\views\exposed_form

Code

public function updateGroupsValidate(array &$element, FormStateInterface $formState) {
  $groups_value = $formState
    ->getValue([
    'manage_groups',
    'groups',
  ]);
  $groups = explode("\n", $groups_value);
  $groups = array_map('trim', $groups);
  if (!in_array('_none', $groups)) {
    $formState
      ->setError($element, $this
      ->t('The "_none" group is required.'));
  }
  foreach ($groups as $group_label) {
    $groups_with_label = array_filter($groups, function ($group) use ($group_label) {
      return $group === $group_label;
    });
    if (count($groups_with_label) > 1) {
      $formState
        ->setError($element, $this
        ->t('Duplicate group "@name" found.', [
        '@name' => $group_label,
      ]));
    }
  }
}