You are here

protected function RecipeTotalTimeConfigTest::attachIntegerField in Recipe 8.2

Attaches an integer field to an entity.

Parameters

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

string $entity_type: The entity type this field will be added to.

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

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

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

array $display_settings: A list of display settings that will be added to the display defaults.

1 call to RecipeTotalTimeConfigTest::attachIntegerField()
RecipeTotalTimeConfigTest::createIntegerField in tests/src/Functional/RecipeTotalTimeConfigTest.php
Creates a new integer field.

File

tests/src/Functional/RecipeTotalTimeConfigTest.php, line 160

Class

RecipeTotalTimeConfigTest
Tests the functionality of the Recipe Total Time third-party settings.

Namespace

Drupal\Tests\recipe\Functional

Code

protected function attachIntegerField($name, $entity_type, $bundle, array $field_settings = [], array $widget_settings = [], array $display_settings = []) {
  $field = [
    'field_name' => $name,
    'label' => $name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
  ];
  $this->entityTypeManager
    ->getStorage('field_config')
    ->create($field)
    ->save();
  $form_display = $this->entityTypeManager
    ->getStorage('entity_form_display')
    ->load($entity_type . '.' . $bundle . '.default');
  $form_display
    ->setComponent($name, [
    'type' => 'number',
    'settings' => $widget_settings,
  ])
    ->save();

  // Assign display settings.
  $view_display = $this->entityTypeManager
    ->getStorage('entity_view_display')
    ->load($entity_type . '.' . $bundle . '.default');
  $view_display
    ->setComponent($name, [
    'label' => 'hidden',
    'type' => 'recipe_duration',
    'settings' => $display_settings,
  ])
    ->save();
}