You are here

protected function SelectOtherInstallTestScript::createSelectOtherListField in CCK Select Other 8

Creates a select other field on the content type.

Parameters

string $entity_type: The entity type id.

string $bundle: The bundle id.

string $type: The field type plugin ID.

array $fieldInfo: The field storage configuration.

array $instanceInfo: The field configuration.

Return value

\Drupal\Core\Entity\EntityInterface|\Drupal\field\Entity\FieldStorageConfig The field config instance.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to SelectOtherInstallTestScript::createSelectOtherListField()
SelectOtherInstallTestScript::setup in tests/src/TestSite/SelectOtherInstallTestScript.php
Run the code to setup the test environment.

File

tests/src/TestSite/SelectOtherInstallTestScript.php, line 133

Class

SelectOtherInstallTestScript
Setup file used for cck_select_other Nightwatch tests.

Namespace

Drupal\Tests\cck_select_other\TestSite

Code

protected function createSelectOtherListField($entity_type = 'node', $bundle = 'page', $type = 'list_string', array $fieldInfo = [], array $instanceInfo = []) {
  $random = $this
    ->getRandomGenerator();
  if (!isset($fieldInfo['field_name'])) {
    $fieldInfo['field_name'] = strtolower($random
      ->name(8, TRUE));
  }

  // Create field storage instance.
  $storage_values = NestedArray::mergeDeep($fieldInfo, [
    'entity_type' => $entity_type,
    'type' => $type,
  ]);
  $fieldStorage = FieldStorageConfig::create($storage_values);
  $fieldStorage
    ->save();

  // Create field instance.
  if (!isset($instanceInfo['label'])) {
    $instanceInfo['label'] = $random
      ->string(15);
  }
  $field_values = NestedArray::mergeDeep($instanceInfo, [
    'field_name' => $fieldStorage
      ->getName(),
    'entity_type' => $entity_type,
    'bundle' => $bundle,
  ]);
  $field = FieldConfig::create($field_values);
  $field
    ->save();

  // Create form and display entities for select other field.
  $display_id = $entity_type . '.' . $bundle . '.default';
  $formDisplay = EntityFormDisplay::load($display_id);
  $formDisplay
    ->setComponent($fieldStorage
    ->getName(), [
    'type' => 'cck_select_other',
    'weight' => -1,
  ]);
  $formDisplay
    ->save();
  $viewDisplay = EntityViewDisplay::load($display_id);
  $viewDisplay
    ->setComponent($fieldStorage
    ->getName(), [
    'type' => 'cck_select_other',
    'weight' => -1,
  ]);
  $viewDisplay
    ->save();
  return $fieldStorage;
}