You are here

function _unique_field_ajax_process in Unique field ajax 2.x

Same name and namespace in other branches
  1. 8 unique_field_ajax.module \_unique_field_ajax_process()

Attach ajax to unique field.

Parameters

array $element: Element array.

\Drupal\Core\Form\FormStateInterface $form_state: FormState.

array $form: Form array.

Return value

array Element array.

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityMalformedException

1 string reference to '_unique_field_ajax_process'
unique_field_ajax_field_widget_form_alter in ./unique_field_ajax.module
Attaching data to unique fields.

File

./unique_field_ajax.module, line 262
Unique value for cck fields check module.

Code

function _unique_field_ajax_process(array $element, FormStateInterface &$form_state, array &$form) : array {
  foreach (Element::children($element) as $property) {
    if (isset($element[$property]) && isset($element[$property]['#unique_field_ajax_settings'])) {
      $field_name = $element[$property]['#unique_field_ajax_settings']['field_name'];
      $unique_field_ajax_settings = $element[$property]['#unique_field_ajax_settings'];
      if ($unique_field_ajax_settings) {
        $field_label = $unique_field_ajax_settings['field_label'];
        $wrapper = 'unique-' . $field_name;
        $form['#attached']['library'][] = 'unique_field_ajax/unique_event';
        $settings = [
          'id' => '#' . $wrapper . ' input',
        ];
        $form['#attached']['drupalSettings']['unique_field_ajax'][] = $settings;
        $element[$property]['#ajax'] = [
          'callback' => '_unique_field_ajax',
          'event' => 'finishedinput',
          'wrapper' => $wrapper,
          'progress' => [
            'type' => 'throbber',
            'message' => t('Verifying @field_label...', [
              '@field_label' => $field_label,
            ]),
          ],
        ];
        $element[$property]['#prefix'] = '<div id="' . $wrapper . '">';
        $element[$property]['#suffix'] = '</div>';
        $value = $form_state
          ->getValue($field_name);
        $value = !empty($value) ? $value[0][$property] : NULL;
        if (!isset($value)) {
          return $element;
        }
        $entity = $form_state
          ->getFormObject()
          ->getEntity();
        $entity_type = $entity
          ->getEntityTypeId();
        $lang_code = !empty($form_state
          ->getValues()['langcode'][0][$property]) ? $form_state
          ->getValues()['langcode'][0][$property] : '0';
        $valid = unique_field_ajax_is_unique($entity_type, $lang_code, $field_name, $value, $entity
          ->bundle(), $element[$property]['#unique_field_ajax_settings']['per_lang'], $entity);
        $message = unique_field_ajax_custom_message($entity, $element, $valid, $property, TRUE);
        if ($valid !== TRUE) {
          $element[$property]['#attributes']['class'][] = 'error';
          $element[$property]['#attributes']['aria-invalid'] = 'true';
          $element[$property]['#suffix'] = '<div class="error">' . $message . '</div>' . $element[$property]['#suffix'];
        }
      }
    }
  }
  return $element;
}