You are here

protected function ConditionalFieldsUserTest::addField in Conditional Fields 8

Same name and namespace in other branches
  1. 4.x tests/src/FunctionalJavascript/Entity/ConditionalFieldsUserTest.php \Drupal\Tests\conditional_fields\FunctionalJavascript\Entity\ConditionalFieldsUserTest::addField()

Add field to User CT.

Parameters

string $field_name: Field name to create.

string $type: Field type.

string $widget: Field Widget to use.

1 call to ConditionalFieldsUserTest::addField()
ConditionalFieldsUserTest::setUp in tests/src/FunctionalJavascript/Entity/ConditionalFieldsUserTest.php

File

tests/src/FunctionalJavascript/Entity/ConditionalFieldsUserTest.php, line 64

Class

ConditionalFieldsUserTest
Test Conditional Fields check User entity.

Namespace

Drupal\Tests\conditional_fields\FunctionalJavascript\Entity

Code

protected function addField($field_name, $type, $widget) {
  $fieldStorageDefinition = [
    'field_name' => $field_name,
    'entity_type' => 'user',
    'type' => $type,
    'cardinality' => -1,
  ];
  $fieldStorage = FieldStorageConfig::create($fieldStorageDefinition);
  $fieldStorage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $fieldStorage,
    'bundle' => 'user',
  ]);
  $field
    ->save();
  $entity_form_display = \Drupal::entityTypeManager()
    ->getStorage('entity_form_display')
    ->load('user.user.default');
  if (!$entity_form_display) {
    $entity_form_display = EntityFormDisplay::create([
      'targetEntityType' => 'user',
      'bundle' => 'user',
      'mode' => 'default',
      'status' => true,
    ]);
  }
  if ($entity_form_display instanceof EntityDisplayInterface) {
    $entity_form_display
      ->setComponent($field_name, [
      'type' => $widget,
    ])
      ->save();
  }
}