Problem/Motivation

The constraint can only be added on non-ID fields, and doesn't take into account if the unique field being checked is a (non-incremementing) ID itself.

Steps to reproduce

Given the following field definition:

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    $fields['id'] = BaseFieldDefinition::create('string')
      ->setLabel(t('My unique String ID'))
      ->setRequired(TRUE)
      ->setReadOnly(TRUE)
      ->setDescription(t('The non-numeric string ID.'))
      ->addConstraint('UniqueField');
   // ...
}

The validation will never pass.

Due to the following line: https://git.drupalcode.org/project/drupal/-/blob/96aa5ca5/core/lib/Drupa...

Where it checks if the ID does not equal to itself.

Proposed resolution

There should be special logic in place to handle when the field is an ID itself (or a special validation constraint introduced specifically for ID fields).

Using something like:

    $entity_id = $field_name === $id_key ? $entity->getOriginalId() : $entity->id();
    // Using isset() instead of !empty() as 0 and '0' are valid ID values for
    // entity types using string IDs.
    if (isset($entity_id)) {
      $query->condition($id_key, $entity_id, '<>');
    }

Remaining tasks

  • Provide working patch with test coverage.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

N/A

Comments

codebymikey created an issue. See original summary.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.