You are here

protected function TestBase::createField in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/FunctionalJavascript/TestBase.php \Drupal\Tests\double_field\FunctionalJavascript\TestBase::createField()

Creates a field.

11 calls to TestBase::createField()
FormatterTest::testAllowedValues in tests/src/FunctionalJavascript/FormatterTest.php
Test callback.
FormatterTest::testBooleanAndString in tests/src/FunctionalJavascript/FormatterTest.php
Test callback.
FormatterTest::testDateAndUri in tests/src/FunctionalJavascript/FormatterTest.php
Test callback.
FormatterTest::testDetailsFormatter in tests/src/FunctionalJavascript/FormatterTest.php
Test callback.
FormatterTest::testEmailAndTelephone in tests/src/FunctionalJavascript/FormatterTest.php
Test callback.

... See full list

File

tests/src/FunctionalJavascript/TestBase.php, line 144

Class

TestBase
Base class for Double Field JavaScript tests.

Namespace

Drupal\Tests\double_field\FunctionalJavascript

Code

protected function createField(array $settings) : void {
  if ($field_storage_config = FieldStorageConfig::loadByName('node', $this->fieldName)) {
    $field_storage_config
      ->delete();
  }

  // -- Build settings arrays acceptable by field API.
  $cardinality = $settings['storage']['cardinality'] ?? 1;
  unset($settings['storage']['cardinality']);
  $widget_settings = $settings['widget'] ?? [];
  unset($settings['widget']);
  $widget_type = $widget_settings['type'] ?? NULL;
  unset($widget_type['type']);
  $formatter_settings = $settings['formatter'] ?? [];
  unset($settings['formatter']);
  $formatter_type = $formatter_settings['type'] ?? NULL;
  unset($formatter_settings['type']);
  $field_storage = FieldStorageConfig::create([
    'field_name' => $this->fieldName,
    'entity_type' => 'node',
    'type' => 'double_field',
    'cardinality' => $cardinality,
    'settings' => NestedArray::mergeDeep(DoubleField::defaultStorageSettings(), $settings),
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $this->contentTypeId,
    'required' => TRUE,
    'settings' => NestedArray::mergeDeep(DoubleField::defaultFieldSettings(), $settings),
  ]);
  $field
    ->save();

  /** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form_display */
  $form_display = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('node.' . $this->contentTypeId . '.default');
  $options = [
    'type' => $widget_type,
    'weight' => 100,
    'settings' => NestedArray::mergeDeep(DoubleFieldWidget::defaultSettings(), $widget_settings),
    'third_party_settings' => [],
  ];
  $form_display
    ->setComponent($this->fieldName, $options);
  $form_display
    ->save();

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $view_display */
  $view_display = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display')
    ->load("node.{$this->contentTypeId}.default");
  $options = [
    'label' => 'hidden',
    'type' => $formatter_type,
    'weight' => 100,
    'settings' => NestedArray::mergeDeep(BaseFormatter::defaultSettings(), $formatter_settings),
    'third_party_settings' => [],
  ];
  $view_display
    ->setComponent($this->fieldName, $options);
  $view_display
    ->save();
}