Skip to content
Snippets Groups Projects

Issue #2914527 by mlncn: Fix check for combination of values must be unique

1 file
+ 18
5
Compare changes
  • Side-by-side
  • Inline
+ 18
5
@@ -290,8 +290,6 @@ function unique_field_form_validate(array &$form, FormStateInterface $form_state
@@ -290,8 +290,6 @@ function unique_field_form_validate(array &$form, FormStateInterface $form_state
$query = \Drupal::database()->select('node_field_data', 'n');
$query = \Drupal::database()->select('node_field_data', 'n');
$query->fields('n', ['nid']);
$query->fields('n', ['nid']);
}
}
$query = \Drupal::database()->select('node_field_data', 'n');
$query->fields('n', ['nid']);
if ($field_name == 'title') {
if ($field_name == 'title') {
$query->condition('n.title', $title);
$query->condition('n.title', $title);
}
}
@@ -327,8 +325,8 @@ function unique_field_form_validate(array &$form, FormStateInterface $form_state
@@ -327,8 +325,8 @@ function unique_field_form_validate(array &$form, FormStateInterface $form_state
}
}
$col_name = $field_type == 'entity_reference' ? $field_name . '_target_id' : $field_name . $suffix;
$col_name = $field_type == 'entity_reference' ? $field_name . '_target_id' : $field_name . $suffix;
$query->join($table_name, 'f', 'n.nid = f.entity_id');
$query->join($table_name, $field_name, 'n.nid = ' . $field_name . '.entity_id');
$query->condition('f.' . $col_name, $field_values, 'IN');
$query->condition($col_name, $field_values, 'IN');
if ($scope == 'language') {
if ($scope == 'language') {
$query->condition('f.langcode', $langcode);
$query->condition('f.langcode', $langcode);
@@ -340,9 +338,24 @@ function unique_field_form_validate(array &$form, FormStateInterface $form_state
@@ -340,9 +338,24 @@ function unique_field_form_validate(array &$form, FormStateInterface $form_state
if ($node) {
if ($node) {
$query->condition('n.nid', $node->id(), '<>');
$query->condition('n.nid', $node->id(), '<>');
}
}
 
if ($comp_type == 'each') {
 
$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();
 
}
 
}
 
}
 
if ($comp_type == 'all') {
$res = $query->execute()->fetchField();
$res = $query->execute()->fetchField();
if ($res) {
if ($res) {
$form_state->setErrorByName($field_name, t('The field @field_label has to be unique.', ['@field_label' => $field_label]));
$form_state->setErrorByName($field_name, t('The combination of fields including @field_label has to be unique.', ['@field_label' => $field_label]));
$user = \Drupal::currentUser();
$user = \Drupal::currentUser();
if ($user->hasPermission('unique_field_perm_bypass')) {
if ($user->hasPermission('unique_field_perm_bypass')) {
$form_id = str_replace('_', '-', $form['#id']);
$form_id = str_replace('_', '-', $form['#id']);
Loading