<?php

/**
 * @file
 * Contains unique_field.module.
 */

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements hook_help().
 */
function unique_field_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.unique_field':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t("The Unique Field module allows administrators to require that content supplied for specified fields is unique. For example, you may require that each node has a unique title or a different author. For configuration options, please see the <em>Unique Field restrictions</em> section of a content type's administration page.") . '</p>';
      return $output;
  }
}

/**
 * Hook_form_BASE_FORM_ID_alter().
 */
function unique_field_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo('node');
  $bundle_id = [];
  foreach ($bundles as $key => $value) {
    $bundle_id['node_' . $key . '_form'] = $key;
    $bundle_id['node_' . $key . '_add_form'] = $key;
    $bundle_id['node_' . $key . '_edit_form'] = $key;
  }
  // Call form validate function for nodes.
  if (in_array($form_id, array_keys($bundle_id))) {
    $form['#validate'][] = 'unique_field_form_validate';
    $form['#bundle'] = $bundle_id[$form_id];
    $form['unique_field_override'] = [
      '#type' => 'hidden',
      '#default_value' => '0',
    ];
  }

  $taxonomy_vocabularies = \Drupal::service('entity_type.bundle.info')->getBundleInfo('taxonomy_term');
  $taxonomy_term_form_id = [];
  foreach ($taxonomy_vocabularies as $key => $value) {
    $taxonomy_term_form_id['taxonomy_term_' . $key . '_form'] = $key;
  }
  // Call form validate function for taxonomy terms.
  if (in_array($form_id, array_keys($taxonomy_term_form_id))) {
    $form['#validate'][] = 'unique_field_taxonomy_term_form_validate';
    $form['#bundle'] = $taxonomy_term_form_id[$form_id];
    $form['unique_field_override'] = [
      '#type' => 'hidden',
      '#default_value' => '0',
    ];
  }

  // Call form validate function for user registration.
  if ($form_id == 'user_register_form' || $form_id == 'user_form') {
    $form['#validate'][] = 'unique_field_user_form_validate';
    $form['#bundle'] = 'user';
    $form['unique_field_override'] = [
      '#type' => 'hidden',
      '#default_value' => '0',
    ];
  }

  // Get the current user.
  $user = \Drupal::currentUser();

  // Load default values.
  $config = \Drupal::configFactory()->get('unique_field.settings');
  // Add unique field settings form.
  if (($form_id == 'node_type_add_form' || $form_id == 'node_type_edit_form') && isset($form['type']) && $user->hasPermission('unique_field_perm_admin')) {
    // Load fields for content type.
    $type = $form_state->getFormObject()->getEntity();
    $ntype = $type->id();
    $fieldopts = [];

    if ((\Drupal::moduleHandler()->moduleExists('language')) && !empty($ntype) && \Drupal::languageManager()->getCurrentLanguage()->getId()) {
      $fieldopts['language'] = t('Language');
    }

    if (!empty($ntype)) {
      $entity_type_id = 'node';
      $bundle = $ntype;
      foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle) as $field_name => $field_definition) {
        if (!empty($field_definition->getTargetBundle())) {
          $fieldopts[$field_name] = $field_definition->getLabel() . '(' . $field_name . ')';
        }
      }
    }

    foreach ($fieldopts as $key => $value) {
      if ($key === "status" || $key === "promote") {
        unset($fieldopts[$key]);
      }
    }

    // Build the unique field form.
    $form['unique_field'] = [
      '#title' => t('Unique Field restrictions'),
      '#type' => 'details',
      '#group' => 'additional_settings',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 1,
    ];
    $form['unique_field']['unique_field_fields'] = [
      '#type' => 'checkboxes',
      '#title' => t('Choose the fields that should be unique'),
      '#options' => $fieldopts,
      '#default_value' => !empty($config->get('unique_field_settings.' . $ntype . '.fields')) ? $config->get('unique_field_settings.' . $ntype . '.fields') : [],
      '#description' => t('After designating that certain fields should be unique, users will not be able to submit the content form to create a new node or update an existing one if it contains values in the designated fields that duplicate others.'),
    ];

    $form['unique_field']['unique_field_scope'] = [
      '#type' => 'radios',
      '#title' => t('Choose the scope for the unique values'),
      '#options' => [
        'type' => t('Content type'),
        'language' => t('Language'),
        'all' => t('All nodes'),
        'node' => t('Single node only'),
      ],
      '#default_value' => !empty($config->get('unique_field_settings.' . $ntype . '.scope')) ? $config->get('unique_field_settings.' . $ntype . '.scope') : 'type',
      '#description' => t('Choose whether the values in the specified fields must be unique among nodes of this content type, among nodes of the same language, among all nodes, or only among the fields of the present node.'),
    ];

    $form['unique_field']['unique_field_comp'] = [
      '#type' => 'radios',
      '#title' => t('Choose whether values must be unique individually or in combination'),
      '#options' => [
        'each' => t('Each of the specified fields must have a unique value'),
        'all' => t('The combination of values from the specified fields must be unique'),
      ],
      '#default_value' => !empty($config->get('unique_field_settings.' . $ntype . '.comp')) ? $config->get('unique_field_settings.' . $ntype . '.comp') : 'each',
      '#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a node, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
    ];

    // Add validation function in submit handler.
    $form['actions']['submit']['#submit'][] = 'unique_field_form_node_type_form_submit';
  }
  // Unique field for taxonomy terms.
  if ($form_id == 'taxonomy_vocabulary_form' && $user->hasPermission('unique_field_perm_admin')) {
    $type = $form_state->getFormObject()->getEntity();
    $taxonomy_vocabulary_name = $type->id();
    $fieldopts = [];
    $fieldopts['name'] = t('Term Name');
    $fieldopts['description'] = t('Term Description');

    if (!empty($taxonomy_vocabulary_name)) {
      $entity_type_id = 'taxonomy_term';
      foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $taxonomy_vocabulary_name) as $field_name => $field_definition) {
        if (!empty($field_definition->getTargetBundle())) {
          $fieldopts[$field_name] = $field_definition->getLabel() . '(' . $field_name . ')';
        }
      }
    }

    // Build the unique field form.
    $form['unique_field_taxonomy'] = [
      '#title' => t('Unique Field restrictions'),
      '#type' => 'details',
      '#group' => 'additional_settings',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 1,
    ];
    $form['unique_field_taxonomy']['unique_field_taxonomy_fields'] = [
      '#type' => 'checkboxes',
      '#title' => t('Choose the fields that should be unique'),
      '#options' => $fieldopts,
      '#default_value' => !empty($config->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.fields')) ? $config->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.fields') : [],
      '#description' => t('After designating that certain fields should be unique, users will not be able to submit the content form to create a new term or update an existing one if it contains values in the designated fields that duplicate others.'),
    ];
    $form['unique_field_taxonomy']['unique_field_taxonomy_scope'] = [
      '#type' => 'radios',
      '#title' => t('Choose the scope for the unique values'),
      '#options' => [
        'vocabulary' => t('Vocabulary'),
        'language' => t('Language'),
        'all' => t('All Vocabularies'),
        'term' => t('Single Term only'),
      ],
      '#default_value' => !empty($config->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.scope')) ? $config->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.scope') : 'vocabulary',
      '#description' => t('Choose whether the values in the specified fields must be unique among terms of this Vocabulary, among terms of the same language, among all terms, or only among the fields of the present term.'),
    ];
    $form['unique_field_taxonomy']['unique_field_taxonomy_comp'] = [
      '#type' => 'radios',
      '#title' => t('Choose whether values must be unique individually or in combination'),
      '#options' => [
        'each' => t('Each of the specified fields must have a unique value'),
        'all' => t('The combination of values from the specified fields must be unique'),
      ],
      '#default_value' => !empty($config->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.comp')) ? $config->get('unique_field_taxonomy.' . $taxonomy_vocabulary_name . '.comp') : 'each',
      '#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a term, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
    ];

    // Add validation function in submit handler.
    $form['actions']['submit']['#submit'][] = 'unique_field_form_taxonomy_term_form_submit';

  }

  // Unique field form for User fields.
  if ($form_id == 'user_admin_settings') {
    $fieldoptions = [];
    $config = \Drupal::configFactory()->get('unique_field.settings');
    foreach (\Drupal::service('entity_field.manager')->getFieldDefinitions('user', 'user') as $field_name => $field_definition) {
      if (!empty($field_definition->getTargetBundle())) {
        $fieldoptions[$field_name] = $field_definition->getLabel() . '(' . $field_name . ')';
      }
    }

    // Build the unique field form.
    $form['unique_field_user'] = [
      '#title' => t('Unique Field restrictions'),
      '#type' => 'details',
      '#group' => 'additional_settings',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 1,
    ];
    $form['unique_field_user']['unique_field_user_fields'] = [
      '#type' => 'checkboxes',
      '#title' => t('Choose the fields that should be unique'),
      '#options' => $fieldoptions,
      '#default_value' => !empty($config->get('unique_field_user.fields')) ? $config->get('unique_field_user.fields') : [],
      '#description' => t('After designating that certain fields should be unique, users will not be able to submit the User form to create a new user or update an existing one if it contains values in the designated fields that duplicate others.'),
    ];
    $form['unique_field_user']['unique_field_user_comp'] = [
      '#type' => 'radios',
      '#title' => t('Choose whether values must be unique individually or in combination'),
      '#options' => [
        'each' => t('Each of the specified fields must have a unique value'),
        'all' => t('The combination of values from the specified fields must be unique'),
      ],
      '#default_value' => !empty($config->get('unique_field_user.comp')) ? $config->get('unique_field_user.comp') : 'each',
      '#description' => t('For example, if you have fields for the parts of a street address (street number and name, city, and zip code) on a node, and want to allow only one node per complete address, but not only one node per city or per zip code, then you would want to choose that the fields must be unique in combination.'),
    ];

    // Add validation function in submit handler.
    $form['#submit'][] = 'unique_field_form_user_type_form_submit';
  }
}

/**
 * Validate the node for unique field conditions on submission.
 */
function unique_field_form_validate(array &$form, FormStateInterface $form_state) {
  if (\Drupal::request()->isXmlHttpRequest()) {
    return;
  }

  // If override permission set show warring message and skip validation.
  if ($form_state->getValues()['unique_field_override'] == 1) {
    return;
  }
  // Load default  configuration for validation.
  $config = \Drupal::configFactory()->get('unique_field.settings');

  $fields = $config->get('unique_field_settings.' . $form['#bundle'] . '.fields');
  if (empty($fields)) {
    return;
  }
  $scope = $config->get('unique_field_settings.' . $form['#bundle'] . '.scope');
  $comp_type = $config->get('unique_field_settings.' . $form['#bundle'] . '.comp');

  $title = $form_state->getValues()['title'][0]['value'];
  $langcode = $form_state->getValues()['langcode'][0]['value'];
  $node = \Drupal::routeMatch()->getParameter('node');

  if ($comp_type == 'all') {
    $query = \Drupal::database()->select('node_field_data', 'n');
    $query->fields('n', ['nid']);
  }
  foreach ($fields as $field_name) {

    // Skips validations for the fields which are not presented in the form.
    // For example, the fields that are hidden in the "Manage form display".
    if (!array_key_exists($field_name, $form_state->getValues())) {
      continue;
    }

    if ($comp_type == 'each') {
      $query = \Drupal::database()->select('node_field_data', 'n');
      $query->fields('n', ['nid']);
    }
    $query = \Drupal::database()->select('node_field_data', 'n');
    $query->fields('n', ['nid']);
    if ($field_name == 'title') {
      $query->condition('n.title', $title);
    }
    else {
      $field_definition = FieldStorageConfig::loadByName('node', $field_name);
      $field_type = $field_definition->get('type');
      $field_config = FieldConfig::loadByName('node', $form['#bundle'], $field_name);
      $values = $form_state->getValues()[$field_name];
      $field_values = [];
      $field_label = $field_config->getLabel();
      foreach ($values as $value) {
        if ($field_type == 'link') {
          $field_values[] = $value['uri'];
        }
        else {
          $field_values[] = ($field_type == 'entity_reference') ? $value['target_id'] : $value['value'];
        }
      }
      if ($scope == 'node') {
        $unique_values = array_unique($field_values);
        if (count($field_values) != count($unique_values)) {
          $form_state->setErrorByName($field_name, t('The field @field_label has to be unique.', ['@field_label' => $field_label]));
          $form_state->setRebuild();
          continue;
        }
      }
      $table_name = 'node__' . $field_name;
      if ($field_type == 'link') {
        $suffix = '_uri';
      }
      else {
        $suffix = '_value';
      }

      $col_name = $field_type == 'entity_reference' ? $field_name . '_target_id' : $field_name . $suffix;
      $query->join($table_name, 'f', 'n.nid = f.entity_id');
      $query->condition('f.' . $col_name, $field_values, 'IN');

      if ($scope == 'language') {
        $query->condition('f.langcode', $langcode);
      }
    }
    if ($scope == 'type') {
      $query->condition('n.type', $form['#bundle']);
    }
    if ($node) {
      $query->condition('n.nid', $node->id(), '<>');
    }
    $res = $query->execute()->fetchField();
    if ($res) {
      $form_state->setErrorByName($field_name, t('The field @field_label has to be unique.', ['@field_label' => $field_label]));
      $user = \Drupal::currentUser();
      if ($user->hasPermission('unique_field_perm_bypass')) {
        $form_id = str_replace('_', '-', $form['#id']);
        $msg = t('Your form has unique field validation errors. Click <a href="#" onclick=":onclick">here</a> to bypass this check and resubmit.', [':onclick' => "jQuery('form#" . $form_id . " input[name=\'unique_field_override\']').val(1);jQuery('form#" . $form_id . "').submit();return false;"]);
        \Drupal::messenger()->addWarning($msg);
      }
      $form_state->setRebuild();
    }
  }
}

/**
 * Validate the taxonomy term for unique field conditions on submission.
 */
function unique_field_taxonomy_term_form_validate(array &$form, FormStateInterface $form_state) {
  if (\Drupal::request()->isXmlHttpRequest()) {
    return;
  }

  // If override permission set show warring message and skip validation.
  if ($form_state->getValues()['unique_field_override'] == 1) {
    return;
  }

  $config = \Drupal::configFactory()->get('unique_field.settings');
  $fields = $config->get('unique_field_taxonomy.' . $form['#bundle'] . '.fields');
  if (empty($fields)) {
    return;
  }
  $scope = $config->get('unique_field_taxonomy.' . $form['#bundle'] . '.scope');
  $comp_type = $config->get('unique_field_taxonomy.' . $form['#bundle'] . '.comp');

  $name = $form_state->getValues()['name'][0]['value'];
  $langcode = $form_state->getValues()['langcode'][0]['value'];
  $description = $form_state->getValues()['description'][0]['value'];

  foreach ($fields as $field_name) {

    // Skips validations for the fields which are not presented in the form.
    // For example, the fields that are hidden in the "Manage form display".
    if (!array_key_exists($field_name, $form_state->getValues())) {
      continue;
    }

    if ($comp_type == 'each') {
      $query = \Drupal::database()->select('taxonomy_term_field_data', 't');
      $query->fields('t', ['tid']);
    }
    $query = \Drupal::database()->select('taxonomy_term_field_data', 't');
    $query->fields('t', ['tid']);
    if ($field_name == 'name') {
      $query->condition('t.name', $name);
    }
    elseif ($field_name == 'description') {
      $query->condition('t.description__value', $description);
    }
    else {
      $field_type = FieldStorageConfig::loadByName('taxonomy_term', $field_name)->get('type');
      $values = $form_state->getValues()[$field_name];
      $field_values = [];
      foreach ($values as $value) {
        $field_values[] = ($field_type == 'entity_reference') ? $value['target_id'] : $value['value'];
      }
      if ($scope == 'term') {
        $unique_values = array_unique($field_values);
        if (count($field_values) != count($unique_values)) {
          $form_state->setErrorByName($field_name, t('The field @field_name has to be unique.', ['@field_name' => $field_name]));
          $form_state->setRebuild();
          continue;
        }
      }
      $table_name = 'taxonomy_term__' . $field_name;
      $col_name = $field_type == 'entity_reference' ? $field_name . '_target_id' : $field_name . '_value';

      $query->join($table_name, 'f', 't.tid = f.entity_id');
      $query->condition('f.' . $col_name, $field_values, 'IN');

      if ($scope == 'language') {
        $query->condition('f.langcode', $langcode);
      }
    }

    if ($scope == 'vocabulary') {
      $query->condition('t.vid', $form['#bundle']);
    }
    if ($form_state->getValues()['tid']) {
      $query->condition('t.tid', $form_state->getValues()['tid'], '<>');
    }
    $res = $query->execute()->fetchField();
    if ($res) {
      $form_state->setErrorByName($field_name, t('The field @field_name has to be unique.', ['@field_name' => $field_name]));
      $user = \Drupal::currentUser();
      if ($user->hasPermission('unique_field_perm_bypass')) {
        $form_id = str_replace('_', '-', $form['#id']);
        $msg = t('Your form has unique field validation errors. Click <a href="#" onclick=":onclick">here</a> to bypass this check and resubmit.', [':onclick' => "jQuery('form#" . $form_id . " input[name=\'unique_field_override\']').val(1);jQuery('form#" . $form_id . "').submit();return false;"]);
        \Drupal::messenger()->addWarning($msg);
      }
      $form_state->setRebuild();
    }
  }
}

/**
 * Validate the user fields for unique field conditions on submission.
 */
function unique_field_user_form_validate(array &$form, FormStateInterface $form_state) {
  if (\Drupal::request()->isXmlHttpRequest()) {
    return;
  }

  // If override permission set show warring message and skip validation.
  if ($form_state->getValues()['unique_field_override'] == 1) {
    return;
  }

  // Load default  configuration for validation.
  $config = \Drupal::configFactory()->get('unique_field.settings');

  $fields = $config->get('unique_field_user.fields');
  if (empty($fields)) {
    return;
  }

  // Load the current user being edited.
  $uid = $form_state->getFormObject()->getEntity()->id();
  $comp_type = $config->get('unique_field_user.comp');

  if ($comp_type == 'all') {
    $query = \Drupal::database()->select('users_field_data', 'u');
    $query->fields('n', ['nid']);
  }
  foreach ($fields as $field_name) {

    // Skips validations for the fields which are not presented in the form.
    // For example, the fields that are hidden in the "Manage form display".
    if (!array_key_exists($field_name, $form_state->getValues())) {
      continue;
    }

    if ($comp_type == 'each') {
      $query = \Drupal::database()->select('users_field_data', 'u');
      $query->fields('u', ['uid']);
    }
    $query = \Drupal::database()->select('users_field_data', 'u');
    $query->fields('u', ['uid']);

    $field_type = FieldStorageConfig::loadByName('user', $field_name)->get('type');
    $values = $form_state->getValues()[$field_name];
    $field_values = [];
    foreach ($values as $value) {
      $field_values[] = ($field_type == 'entity_reference') ? $value['target_id'] : $value['value'];
    }

    $table_name = 'user__' . $field_name;
    $col_name = $field_type == 'entity_reference' ? $field_name . '_target_id' : $field_name . '_value';

    $query->join($table_name, 'f', 'u.uid = f.entity_id');
    $query->condition('f.' . $col_name, $field_values, 'IN');
    if ($uid) {
      // Exclude the current user from the uniqueness check.
      $query->condition('u.uid', $uid, '<>');
    }

    $res = $query->execute()->fetchField();
    if ($res) {
      $form_state->setErrorByName($field_name, t('The field @field_name has to be unique.', ['@field_name' => $field_name]));
      $user = \Drupal::currentUser();
      if ($user->hasPermission('unique_field_perm_bypass')) {
        $form_id = str_replace('_', '-', $form['#id']);
        $msg = t('Your form has unique field validation errors. Click <a href="#" onclick=":onclick">here</a> to bypass this check and resubmit.', [':onclick' => "jQuery('form#" . $form_id . " input[name=\'unique_field_override\']').val(1);jQuery('form#" . $form_id . "').submit();return false;"]);
        \Drupal::messenger()->addWarning($msg);
      }
      $form_state->setRebuild();
    }
  }
}

/**
 * Submit handler for the form.
 */
function unique_field_form_node_type_form_submit(&$form, FormStateInterface $form_state) {
  $ntype = $form_state->getFormObject()->getEntity();

  $unique_field_field_items = $form_state->getValue('unique_field_fields');
  if (!empty($unique_field_field_items)) {
    foreach (array_keys($unique_field_field_items, '0') as $key) {
      unset($unique_field_field_items[$key]);
    }
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_settings.' . $ntype->id() . '.fields', $unique_field_field_items)
      ->save();
  }
  $unique_field_scope_items = $form_state->getValue('unique_field_scope');
  if (!empty($unique_field_scope_items)) {
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_settings.' . $ntype->id() . '.scope', $unique_field_scope_items)
      ->save();
  }
  $unique_field_comp_items = $form_state->getValue('unique_field_comp');
  if (!empty($unique_field_comp_items)) {
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_settings.' . $ntype->id() . '.comp', $unique_field_comp_items)
      ->save();
  }
  $unique_field_show_matches = $form_state->getValue('unique_field_show_matches');
  if (!empty($unique_field_show_matches)) {
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_settings.' . $ntype->id() . '.show_matches', $unique_field_show_matches)
      ->save();
  }
  if ($form_state->getValue(['unique_field', 'unique_field_scope']) === 'node') {
    if ($form_state->getValue(['unique_field', 'unique_field_comp']) === 'all') {
      \Drupal::messenger()->addError(t('The scope of a single node requires that each field must be unique.'));
      $form_state->setRebuild();
    }
  }
}

/**
 * Submit handler for the form.
 */
function unique_field_form_taxonomy_term_form_submit(&$form, FormStateInterface $form_state) {
  $taxonomy_vocabulary_name = $form_state->getFormObject()->getEntity();

  $unique_field_taxonomy_field_items = $form_state->getValue('unique_field_taxonomy_fields');
  if (!empty($unique_field_taxonomy_field_items)) {
    foreach (array_keys($unique_field_taxonomy_field_items, '0') as $key) {
      unset($unique_field_taxonomy_field_items[$key]);
    }
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_taxonomy.' . $taxonomy_vocabulary_name->id() . '.fields', $unique_field_taxonomy_field_items)
      ->save();
  }
  $unique_field_taxonomy_scope_items = $form_state->getValue('unique_field_taxonomy_scope');
  if (!empty($unique_field_taxonomy_scope_items)) {
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_taxonomy.' . $taxonomy_vocabulary_name->id() . '.scope', $unique_field_taxonomy_scope_items)
      ->save();
  }

  $unique_field_taxonomy_comp_items = $form_state->getValue('unique_field_taxonomy_comp');
  if (!empty($unique_field_taxonomy_comp_items)) {
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_taxonomy.' . $taxonomy_vocabulary_name->id() . '.comp', $unique_field_taxonomy_comp_items)
      ->save();
  }
  if ($form_state->getValue('unique_field_taxonomy_scope') === 'vocabulary') {
    if ($form_state->getValue('unique_field_taxonomy_comp') === 'all') {
      \Drupal::messenger()->addError(t('The scope of a single term requires that each field must be unique.'));
      $form_state->setRebuild();
    }
  }
}

/**
 * Submit handler for the user form.
 */
function unique_field_form_user_type_form_submit(&$form, FormStateInterface $form_state) {

  $unique_field_field_items = $form_state->getValue('unique_field_user_fields');
  if (!empty($unique_field_field_items)) {
    foreach (array_keys($unique_field_field_items, '0') as $key) {
      unset($unique_field_field_items[$key]);
    }
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_user.fields', $unique_field_field_items)
      ->save();
  }
  $unique_field_comp_items = $form_state->getValue('unique_field_user_comp');
  if (!empty($unique_field_comp_items)) {
    \Drupal::configFactory()->getEditable('unique_field.settings')
      ->set('unique_field_user.comp', $unique_field_comp_items)
      ->save();
  }
}
