You are here

protected function EntityReferenceTestTrait::createEntityReferenceField in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Traits/EntityReferenceTestTrait.php \Drupal\Tests\field\Traits\EntityReferenceTestTrait::createEntityReferenceField()

Creates a field of an entity reference field storage on the specified bundle.

Parameters

string $entity_type: The type of entity the field will be attached to.

string $bundle: The bundle name of the entity the field will be attached to.

string $field_name: The name of the field; if it already exists, a new instance of the existing field will be created.

string $field_label: The label of the field.

string $target_entity_type: The type of the referenced entity.

string $selection_handler: The selection handler used by this field.

array $selection_handler_settings: An array of settings supported by the selection handler specified above. (e.g. 'target_bundles', 'sort', 'auto_create', etc).

int $cardinality: The cardinality of the field.

See also

\Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()

60 calls to EntityReferenceTestTrait::createEntityReferenceField()
CommonCollectionFilterAccessTestPatternsTrait::doTestCollectionFilterAccessBasedOnPermissions in core/modules/jsonapi/tests/src/Traits/CommonCollectionFilterAccessTestPatternsTrait.php
Implements ::testCollectionFilterAccess() for pure permission-based access.
ContentEntityTest::setUp in core/modules/migrate_drupal/tests/src/Kernel/Plugin/migrate/source/ContentEntityTest.php
ContentModerationTest::setUp in core/modules/media_library/tests/src/FunctionalJavascript/ContentModerationTest.php
DefaultViewsTest::setUp in core/modules/views/tests/src/Functional/DefaultViewsTest.php
DisplayEntityReferenceTest::setUp in core/modules/views/tests/src/Functional/Plugin/DisplayEntityReferenceTest.php

... See full list

File

core/modules/field/tests/src/Traits/EntityReferenceTestTrait.php, line 37

Class

EntityReferenceTestTrait
Provides common functionality for the EntityReference test classes.

Namespace

Drupal\Tests\field\Traits

Code

protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = [], $cardinality = 1) {

  // Look for or add the specified field to the requested entity bundle.
  if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
    FieldStorageConfig::create([
      'field_name' => $field_name,
      'type' => 'entity_reference',
      'entity_type' => $entity_type,
      'cardinality' => $cardinality,
      'settings' => [
        'target_type' => $target_entity_type,
      ],
    ])
      ->save();
  }
  if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
    FieldConfig::create([
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $field_label,
      'settings' => [
        'handler' => $selection_handler,
        'handler_settings' => $selection_handler_settings,
      ],
    ])
      ->save();
  }
}