function FieldStorageReuseForm::getExistingFieldDefaults
Same name in other branches
- 10 core/modules/field_ui/src/Form/FieldStorageReuseForm.php \Drupal\field_ui\Form\FieldStorageReuseForm::getExistingFieldDefaults()
Get default options from an existing field and bundle.
Parameters
string $field_name: The machine name of the field.
Return value
array An array of settings with keys 'field_config', 'entity_form_display', and 'entity_view_display' if these are defined for an existing field instance. If the field is not defined for the specified bundle (or for any bundle if $existing_bundle is omitted) then return an empty array.
1 call to FieldStorageReuseForm::getExistingFieldDefaults()
- FieldStorageReuseForm::reuseCallback in core/
modules/ field_ui/ src/ Form/ FieldStorageReuseForm.php - Callback function to handle re-using an existing field.
File
-
core/
modules/ field_ui/ src/ Form/ FieldStorageReuseForm.php, line 306
Class
- FieldStorageReuseForm
- Provides a form for the "field storage" add page.
Namespace
Drupal\field_ui\FormCode
protected function getExistingFieldDefaults(string $field_name) : array {
$default_options = [];
$field_map = $this->entityFieldManager
->getFieldMap();
if (empty($field_map[$this->entityTypeId][$field_name]['bundles'])) {
return [];
}
$bundles = $field_map[$this->entityTypeId][$field_name]['bundles'];
// Sort bundles to ensure deterministic behavior.
sort($bundles);
$existing_bundle = reset($bundles);
// Copy field configuration.
$existing_field = $this->entityFieldManager
->getFieldDefinitions($this->entityTypeId, $existing_bundle)[$field_name];
$default_options['field_config'] = [
'description' => $existing_field->getDescription(),
'settings' => $existing_field->getSettings(),
'required' => $existing_field->isRequired(),
'default_value' => $existing_field->getDefaultValueLiteral(),
'default_value_callback' => $existing_field->getDefaultValueCallback(),
];
// Copy form and view mode configuration.
$properties = [
'targetEntityType' => $this->entityTypeId,
'bundle' => $existing_bundle,
];
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $existing_forms */
$existing_forms = $this->entityTypeManager
->getStorage('entity_form_display')
->loadByProperties($properties);
foreach ($existing_forms as $form) {
if ($settings = $form->getComponent($field_name)) {
$default_options['entity_form_display'][$form->getMode()] = $settings;
}
}
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $existing_views */
$existing_views = $this->entityTypeManager
->getStorage('entity_view_display')
->loadByProperties($properties);
foreach ($existing_views as $view) {
if ($settings = $view->getComponent($field_name)) {
$default_options['entity_view_display'][$view->getMode()] = $settings;
}
}
return $default_options;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.