You are here

protected function BlockFieldTestTrait::createBlockField in Block field 8

Creates a block field 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 $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. 'plugin_ids').

int $cardinality: The cardinality of the field.

See also

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

1 call to BlockFieldTestTrait::createBlockField()
BlockFieldFormatterTest::setUp in tests/src/Kernel/BlockFieldFormatterTest.php

File

tests/src/Traits/BlockFieldTestTrait.php, line 35

Class

BlockFieldTestTrait
Provides common functionality for the Block Field test classes.

Namespace

Drupal\Tests\block_field\Traits

Code

protected function createBlockField($entity_type, $bundle, $field_name, $field_label, $selection_handler = 'blocks', array $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' => 'block_field',
      'entity_type' => $entity_type,
      'cardinality' => $cardinality,
      'settings' => [],
    ])
      ->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' => [
        'selection' => $selection_handler,
        'selection_settings' => $selection_handler_settings,
      ],
    ])
      ->save();
  }
}