You are here

protected function QuickEditTestBase::createFieldWithStorage in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/quickedit/tests/src/Kernel/QuickEditTestBase.php \Drupal\Tests\quickedit\Kernel\QuickEditTestBase::createFieldWithStorage()
  2. 9 core/modules/quickedit/tests/src/Kernel/QuickEditTestBase.php \Drupal\Tests\quickedit\Kernel\QuickEditTestBase::createFieldWithStorage()

Creates a field.

Parameters

string $field_name: The field name.

string $type: The field type.

int $cardinality: The field's cardinality.

string $label: The field's label (used everywhere: widget label, formatter label).

array $field_settings: The field's settings.

string $widget_type: The widget type.

array $widget_settings: The widget settings.

string $formatter_type: The formatter type.

array $formatter_settings: The formatter settings.

File

core/modules/quickedit/tests/src/Kernel/QuickEditTestBase.php, line 78

Class

QuickEditTestBase
Base class for testing Quick Edit functionality.

Namespace

Drupal\Tests\quickedit\Kernel

Code

protected function createFieldWithStorage($field_name, $type, $cardinality, $label, $field_settings, $widget_type, $widget_settings, $formatter_type, $formatter_settings) {
  $field_storage = $field_name . '_field_storage';
  $this->fields->{$field_storage} = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'entity_test',
    'type' => $type,
    'cardinality' => $cardinality,
  ]);
  $this->fields->{$field_storage}
    ->save();
  $field = $field_name . '_field';
  $this->fields->{$field} = FieldConfig::create([
    'field_storage' => $this->fields->{$field_storage},
    'bundle' => 'entity_test',
    'label' => $label,
    'description' => $label,
    'weight' => mt_rand(0, 127),
    'settings' => $field_settings,
  ]);
  $this->fields->{$field}
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'type' => $widget_type,
    'settings' => $widget_settings,
  ])
    ->save();
  $display_repository
    ->getViewDisplay('entity_test', 'entity_test')
    ->setComponent($field_name, [
    'label' => 'above',
    'type' => $formatter_type,
    'settings' => $formatter_settings,
  ])
    ->save();
}