protected function BlazyCreationTestTrait::setUpFieldConfig in Blazy 8
Same name and namespace in other branches
- 8.2 tests/src/Traits/BlazyCreationTestTrait.php \Drupal\Tests\blazy\Traits\BlazyCreationTestTrait::setUpFieldConfig()
Setup a new image field.
Parameters
string $bundle: The bundle name.
array $data: (Optional) A list of field data.
1 call to BlazyCreationTestTrait::setUpFieldConfig()
- BlazyCreationTestTrait::setUpContentTypeTest in tests/
src/ Traits/ BlazyCreationTestTrait.php - Build dummy content types.
File
- tests/
src/ Traits/ BlazyCreationTestTrait.php, line 283
Class
- BlazyCreationTestTrait
- A Trait common for Blazy tests.
Namespace
Drupal\Tests\blazy\TraitsCode
protected function setUpFieldConfig($bundle = '', array $data = []) {
$config = [];
$default = empty($this->testFieldType) ? 'image' : $this->testFieldType;
$field_type = empty($data['field_type']) ? $default : $data['field_type'];
$field_name = empty($data['field_name']) ? $this->testFieldName : $data['field_name'];
$multiple = strpos($field_name, 'mul') !== FALSE;
if (in_array($field_type, [
'file',
'image',
])) {
$config['file_directory'] = $this->testPluginId;
$config['file_extensions'] = 'png gif jpg jpeg';
if ($field_type == 'file') {
$config['file_extensions'] .= ' txt';
}
if ($field_type == 'image') {
$config['title_field'] = 1;
$config['title_field_required'] = 1;
}
$multiple = TRUE;
}
$field_storage = FieldStorageConfig::loadByName($this->entityType, $field_name);
$storage_settings = [];
if ($field_type == 'entity_reference') {
$storage_settings['target_type'] = isset($this->targetType) ? $this->targetType : $this->entityType;
$bundle = $this->bundle;
$multiple = FALSE;
}
if ($field_name == 'field_image') {
$multiple = FALSE;
}
if (!$field_storage) {
FieldStorageConfig::create([
'entity_type' => $this->entityType,
'field_name' => $field_name,
'type' => $field_type,
'cardinality' => $multiple ? -1 : 1,
'settings' => $storage_settings,
])
->save();
}
$field_config = FieldConfig::loadByName($this->entityType, $bundle, $field_name);
if ($field_type == 'entity_reference' && !empty($this->targetBundles)) {
$config['handler'] = 'default';
$config['handler_settings']['target_bundles'] = $this->targetBundles;
$config['handler_settings']['sort']['field'] = '_none';
$bundle = $this->bundle;
}
if (!$field_config) {
$field_config = FieldConfig::create([
'field_storage' => $field_storage,
'field_name' => $field_name,
'entity_type' => $this->entityType,
'bundle' => $bundle,
'label' => str_replace('_', ' ', $field_name),
'settings' => $config,
]);
$field_config
->save();
}
return $field_config;
}