function easy_email_create_field in Easy Email 2.0.x
Same name and namespace in other branches
- 8 easy_email.module \easy_email_create_field()
Creates a configurable field from the given field definition.
Borrowed from Drupal Commerce: ConfigurableFieldManager class
Parameters
\Drupal\Core\Field\BaseFieldDefinition $field_definition: The field definition.
bool $lock: Whether the created field should be locked.
Throws
\InvalidArgumentException Thrown when given an incomplete field definition (missing name, target entity type ID, or target bundle).
\RuntimeException
\Drupal\Core\Entity\EntityStorageException
15 calls to easy_email_create_field()
- EasyEmailTestBase::addUserField in tests/
src/ Functional/ EasyEmailTestBase.php - easy_email_field_add_attachment_field in ./
easy_email.module - easy_email_field_add_attachment_path_field in ./
easy_email.module - easy_email_field_add_bcc_address_field in ./
easy_email.module - easy_email_field_add_bcc_uid_field in ./
easy_email.module
File
- ./
easy_email.module, line 877 - Contains easy_email.module.
Code
function easy_email_create_field(BaseFieldDefinition $field_definition, $lock = TRUE) {
$field_name = $field_definition
->getName();
$entity_type_id = $field_definition
->getTargetEntityTypeId();
$bundle = $field_definition
->getTargetBundle();
if (empty($field_name) || empty($entity_type_id) || empty($bundle)) {
throw new \InvalidArgumentException('The passed $field_definition is incomplete.');
}
// loadByName() is an API that doesn't exist on the storage classes for
// the two entity types, so we're using the entity classes directly.
$field_storage = FieldStorageConfig::loadByName($entity_type_id, $field_name);
$field = FieldConfig::loadByName($entity_type_id, $bundle, $field_name);
if (!empty($field)) {
throw new \RuntimeException(sprintf('The field "%s" already exists on bundle "%s" of entity type "%s".', $field_name, $bundle, $entity_type_id));
}
// The field storage might already exist if the field was created earlier
// on a different bundle of the same entity type.
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => $entity_type_id,
'type' => $field_definition
->getType(),
'cardinality' => $field_definition
->getCardinality(),
'settings' => $field_definition
->getSettings(),
'translatable' => $field_definition
->isTranslatable(),
'locked' => $lock,
]);
$field_storage
->save();
}
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $bundle,
'label' => $field_definition
->getLabel(),
'required' => $field_definition
->isRequired(),
'settings' => $field_definition
->getSettings(),
'translatable' => $field_definition
->isTranslatable(),
'default_value' => $field_definition
->getDefaultValueLiteral(),
'default_value_callback' => $field_definition
->getDefaultValueCallback(),
]);
$field
->save();
// Show the field on default entity displays, if specified.
if ($view_display_options = $field_definition
->getDisplayOptions('view')) {
$view_display = easy_email_get_entity_display($entity_type_id, $bundle, 'view');
$view_display
->setComponent($field_name, $view_display_options);
$view_display
->save();
}
if ($form_display_options = $field_definition
->getDisplayOptions('form')) {
$form_display = easy_email_get_entity_display($entity_type_id, $bundle, 'form');
$form_display
->setComponent($field_name, $form_display_options);
$form_display
->save();
}
}