You are here

protected function Select2TestTrait::createField in Select 2 8

Creates a new file field.

Parameters

string $name: The name of the new field (all lowercase), exclude the "field_" prefix.

string $entity_type: The entity type.

string $bundle: The bundle that this field will be added to.

string $field_type: The field type.

array $storage_settings: A list of field storage settings that will be added to the defaults.

array $field_settings: A list of instance settings that will be added to the instance defaults.

string $widget_type: The widget for the new field.

array $widget_settings: A list of widget settings that will be added to the widget defaults.

Throws

\Drupal\Core\Entity\EntityStorageException

16 calls to Select2TestTrait::createField()
PublishTest::testAutocomplete in modules/select2_publish/tests/src/FunctionalJavascript/PublishTest.php
Tests that the autocomplete.
PublishTest::testMultipleSelection in modules/select2_publish/tests/src/FunctionalJavascript/PublishTest.php
Test autocomplete in a single value field.
Select2EntityReferenceWidgetTest::testAjaxCallbacksInBetween in tests/src/FunctionalJavascript/FieldWidget/Select2EntityReferenceWidgetTest.php
Test that in-between ajax calls are not creating new entities.
Select2EntityReferenceWidgetTest::testAutocompleteDragnDrop in tests/src/FunctionalJavascript/FieldWidget/Select2EntityReferenceWidgetTest.php
Tests the autocomplete drag n drop.
Select2EntityReferenceWidgetTest::testAutocompleteMatchLimit in tests/src/FunctionalJavascript/FieldWidget/Select2EntityReferenceWidgetTest.php
Tests that the autocomplete ordering is alphabetically.

... See full list

File

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

Class

Select2TestTrait
Test trait with helper functions.

Namespace

Drupal\Tests\select2\Traits

Code

protected function createField($name, $entity_type, $bundle, $field_type, array $storage_settings = [], array $field_settings = [], $widget_type = 'string', array $widget_settings = []) {
  $field_storage = FieldStorageConfig::create([
    'entity_type' => $entity_type,
    'field_name' => $name,
    'type' => $field_type,
    'settings' => $storage_settings,
    'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  ]);
  $field_storage
    ->save();
  $field = [
    'field_name' => $name,
    'label' => $name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
  ];
  FieldConfig::create($field)
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository */
  $entity_display_repository = \Drupal::service('entity_display.repository');
  $entity_display_repository
    ->getFormDisplay($entity_type, $bundle, 'default')
    ->setComponent($name, [
    'type' => $widget_type,
    'settings' => $widget_settings,
  ])
    ->save();
}