You are here

public static function Dropdown::validateAllowedValues in Open Social 8.9

Same name and namespace in other branches
  1. 8 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  2. 8.2 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  3. 8.3 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  4. 8.4 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  5. 8.5 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  6. 8.6 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  7. 8.7 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  8. 8.8 modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  9. 10.3.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  10. 10.0.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  11. 10.1.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()
  12. 10.2.x modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php \Drupal\dropdown\Plugin\Field\FieldType\Dropdown::validateAllowedValues()

Element_validate callback for options field allowed values.

Parameters

array $element: An associative array containing the properties and children of the generic form element.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form for the form this element belongs to.

See also

\Drupal\Core\Render\Element\FormElement::processPattern()

File

modules/custom/dropdown/src/Plugin/Field/FieldType/Dropdown.php, line 97

Class

Dropdown
Plugin implementation of the 'dropdown' field type.

Namespace

Drupal\dropdown\Plugin\Field\FieldType

Code

public static function validateAllowedValues(array $element, FormStateInterface $form_state) {
  $values = static::extractAllowedValues($element['#value'], $element['#field_has_data']);
  if (!is_array($values)) {
    $form_state
      ->setError($element, t('Allowed values list: invalid input.'));
  }
  else {

    // Check that keys are valid for the field type.
    foreach ($values as $key => $value) {
      if ($error = static::validateAllowedValue($key)) {
        $form_state
          ->setError($element, $error);
        break;
      }
    }

    // Prevent removing values currently in use.
    if ($element['#field_has_data']) {
      $lost_keys = array_keys(array_diff_key($element['#allowed_values'], $values));
      if (_options_values_in_use($element['#entity_type'], $element['#field_name'], $lost_keys)) {
        $form_state
          ->setError($element, t('Allowed values list: some values are being removed while currently in use.'));
      }
    }
    $form_state
      ->setValueForElement($element, $values);
  }
}