You are here

public function CshsOptionsFromHelper::validateSettingsForm in Client-side Hierarchical Select 8

Same name and namespace in other branches
  1. 8.3 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::validateSettingsForm()
  2. 8.2 src/CshsOptionsFromHelper.php \Drupal\cshs\CshsOptionsFromHelper::validateSettingsForm()

Validates the settings form.

Parameters

array $element: The element's form structure.

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

File

src/CshsOptionsFromHelper.php, line 220

Class

CshsOptionsFromHelper
Defines a class for getting options for a cshs form element from vocabulary.

Namespace

Drupal\cshs

Code

public function validateSettingsForm(array &$element, FormStateInterface $form_state) : void {
  $settings = $form_state
    ->getValue($element['#parents']);
  $options = $element['parent']['#options'];
  foreach ($options as $id => $label) {

    // This always removes at least the first item, which is what we
    // want. If a user selects nothing we remove the `- Please select -`
    // and count only the number of nesting levels. In another case,
    // we remove everything before and including the selected item and
    // count the rest.
    unset($options[$id]);

    // Leave the rest of the list after the selected option.
    if ((string) $id === $settings['parent']) {
      break;
    }
  }
  if ($settings['hierarchy_depth'] > ($max_hierarchy_depth = \count($options))) {
    $form_state
      ->setError($element['hierarchy_depth'], $this
      ->t('The hierarchy depth cannot be @actual because the selection list has @levels levels.', [
      '@actual' => $settings['hierarchy_depth'],
      '@levels' => $max_hierarchy_depth,
    ]));
  }
  elseif ($settings['required_depth'] > $settings['hierarchy_depth']) {
    $form_state
      ->setError($element['required_depth'], $this
      ->t('The required depth cannot be greater than the hierarchy depth.'));
  }
}