unique_field_constraint_test.module

Same filename in other branches
  1. 9 core/modules/system/tests/modules/unique_field_constraint_test/unique_field_constraint_test.module
  2. 8.9.x core/modules/system/tests/modules/unique_field_constraint_test/unique_field_constraint_test.module
  3. 11.x core/modules/system/tests/modules/unique_field_constraint_test/unique_field_constraint_test.module

Contains unique_field_constraint_test.module.

File

core/modules/system/tests/modules/unique_field_constraint_test/unique_field_constraint_test.module

View source
<?php


/**
 * @file
 * Contains unique_field_constraint_test.module.
 */
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Database\Query\AlterableInterface;

/**
 * Implements hook_entity_base_field_info_alter().
 */
function unique_field_constraint_test_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
    if ($entity_type->id() === 'entity_test_string_id') {
        
        /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
        $fields['name']->addConstraint('UniqueField');
    }
    if ($entity_type->id() === 'entity_test') {
        
        /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
        $fields['name']->addConstraint('UniqueField');
    }
}

/**
 * Implements hook_query_entity_test_access_alter().
 */
function unique_field_constraint_test_query_entity_test_access_alter(AlterableInterface $query) {
    // Set an impossible condition to filter out all entities.
    
    /** @var \Drupal\Core\Database\Query\Select|\Drupal\Core\Database\Query\AlterableInterface $query */
    $query->condition('entity_test.id', 0);
}

Functions

Title Deprecated Summary
unique_field_constraint_test_entity_base_field_info_alter Implements hook_entity_base_field_info_alter().
unique_field_constraint_test_query_entity_test_access_alter Implements hook_query_entity_test_access_alter().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.