You are here

trait EntityReferenceTestTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/EntityReference/EntityReferenceTestTrait.php \Drupal\field\Tests\EntityReference\EntityReferenceTestTrait

Provides common functionality for the EntityReference test classes.

Hierarchy

16 files declare their use of EntityReferenceTestTrait
DefaultViewsTest.php in core/modules/views/src/Tests/DefaultViewsTest.php
Contains \Drupal\views\Tests\DefaultViewsTest.
EntityQueryRelationshipTest.php in core/modules/system/src/Tests/Entity/EntityQueryRelationshipTest.php
Contains \Drupal\system\Tests\Entity\EntityQueryRelationshipTest.
EntityReferenceFieldTest.php in core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php
Contains \Drupal\system\Tests\Entity\EntityReferenceFieldTest.
EntityReferenceRdfaTest.php in core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php
Contains \Drupal\rdf\Tests\Field\EntityReferenceRdfaTest.
EntityReferenceRelationshipTest.php in core/modules/field/src/Tests/EntityReference/Views/EntityReferenceRelationshipTest.php
Contains \Drupal\field\Tests\EntityReference\Views\EntityReferenceRelationshipTest.

... See full list

File

core/modules/field/src/Tests/EntityReference/EntityReferenceTestTrait.php, line 16
Contains \Drupal\field\Tests\EntityReference\EntityReferenceTestTrait.

Namespace

Drupal\field\Tests\EntityReference
View source
trait EntityReferenceTestTrait {

  /**
   * Creates a field of an entity reference field storage on the specified bundle.
   *
   * @param string $entity_type
   *   The type of entity the field will be attached to.
   * @param string $bundle
   *   The bundle name of the entity the field will be attached to.
   * @param string $field_name
   *   The name of the field; if it already exists, a new instance of the existing
   *   field will be created.
   * @param string $field_label
   *   The label of the field.
   * @param string $target_entity_type
   *   The type of the referenced entity.
   * @param string $selection_handler
   *   The selection handler used by this field.
   * @param array $selection_handler_settings
   *   An array of settings supported by the selection handler specified above.
   *   (e.g. 'target_bundles', 'sort', 'auto_create', etc).
   * @param int $cardinality
   *   The cardinality of the field.
   *
   * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
   */
  protected function createEntityReferenceField($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1) {

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

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceTestTrait::createEntityReferenceField protected function Creates a field of an entity reference field storage on the specified bundle.