You are here

protected function UniqueFieldAjaxBase::createField in Unique field ajax 2.x

Helper method to create a field to use.

Parameters

string $fieldType: Type of field.

string $widgetType: Type of field widget.

array $fieldConfigSettings: Any extra field config settings.

Throws

\Drupal\Core\Entity\EntityStorageException

7 calls to UniqueFieldAjaxBase::createField()
UniqueFieldAjaxLanguageTest::testUniqueFieldPerLang in tests/src/Functional/UniqueFieldAjaxLanguageTest.php
Tests unique field per language.
UniqueFieldAjaxTest::testUniqueFieldAllowsSavingSameField in tests/src/Functional/UniqueFieldAjaxTest.php
Test if field unique is enabled you can still save the same node.
UniqueFieldAjaxTest::testUniqueFieldCustomMessage in tests/src/Functional/UniqueFieldAjaxTest.php
Tests unique field custom message.
UniqueFieldAjaxTest::testUniqueFieldCustomMessageWithLabelToken in tests/src/Functional/UniqueFieldAjaxTest.php
Tests unique field custom message with an added label token.
UniqueFieldAjaxTest::testUniqueFieldCustomMessageWithLinkToken in tests/src/Functional/UniqueFieldAjaxTest.php
Tests unique field custom message with an added link token.

... See full list

File

tests/src/Functional/UniqueFieldAjaxBase.php, line 147

Class

UniqueFieldAjaxBase
The base testing class for unique_field_ajax.

Namespace

Drupal\Tests\unique_field_ajax\Functional

Code

protected function createField(string $fieldType, string $widgetType, array $fieldConfigSettings = []) {
  $field_name = $this
    ->createRandomData();
  $this->fieldStorage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => $fieldType,
  ]);
  $this->fieldStorage
    ->save();
  $field_config = [
    'field_storage' => $this->fieldStorage,
    'bundle' => $this->contentTypeName,
    'label' => $field_name . '_label',
  ];
  if (!empty($fieldConfigSettings)) {
    $field_config['settings'] = $fieldConfigSettings;
  }
  $this->field = FieldConfig::create($field_config);
  $this->field
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('node', $this->contentTypeName)
    ->setComponent($field_name, [
    'type' => $widgetType,
  ])
    ->save();
  $display_repository
    ->getViewDisplay('node', $this->contentTypeName, 'full')
    ->setComponent($field_name)
    ->save();
}