You are here

public static function BarcodeBase::validateTypes in Barcode 8

#element_validate callback for options field allowed values.

Parameters

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

$form_state: The current state of the form for the form this element belongs to.

See also

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

File

src/Plugin/Field/FieldType/BarcodeBase.php, line 164

Class

BarcodeBase

Namespace

Drupal\barcode\Plugin\Field\FieldType

Code

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

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

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