TextFieldCreationTrait.php in Insert 8
File
tests/src/FunctionalJavaScript/TextFieldCreationTrait.php
View source
<?php
namespace Drupal\Tests\insert\FunctionalJavascript;
use Drupal\Core\Entity\Display\EntityDisplayInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
trait TextFieldCreationTrait {
protected function createTextField($name, $type_name, $type = 'text_long', $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') {
FieldStorageConfig::create([
'field_name' => $name,
'entity_type' => 'node',
'type' => $type,
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
])
->save();
$field_config = FieldConfig::create([
'field_name' => $name,
'label' => $name,
'entity_type' => 'node',
'bundle' => $type_name,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
'description' => $description,
]);
$field_config
->save();
$entity = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('node.' . $type_name . '.default');
$entity
->setComponent($name, [
'type' => 'string_textarea',
'settings' => $widget_settings,
])
->save();
$entity = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('node.' . $type_name . '.default');
$entity
->setComponent($name, [
'type' => 'basic_string',
'settings' => $formatter_settings,
])
->save();
return $field_config;
}
}