Set the paragraph field configuration value programmatically based on configuration settings.
/**
* AttachField function implementation.
*/
public static function attachField($bundle, $flag) {
// Field field_gc_coordinates is already created at module install.
// Now attach this field to the bundle as per the configuration settings.
$field = FieldConfig::loadByName('node', $bundle, 'field_gc_coordinates');
// Default field setting form for paragraph.
$field_settings = [
'type' => 'entity_reference_paragraphs',
'weight' => '4',
'settings' => [
'title' => 'Paragraph',
'title_plural' => 'Paragraphs',
'edit_mode' => 'open',
'add_mode' => 'dropdown',
'form_display_mode' => 'default',
'default_paragraph_type' => '',
'region' => 'content',
],
];
// Attach field if there is not already.
if (empty($field) && $flag === $bundle) {
$field = FieldConfig::create([
'field_name' => 'field_gc_coordinates',
'entity_type' => 'node',
'bundle' => $bundle,
'label' => 'Geographic Coordinates',
'settings' => [
'handler' => 'default:paragraph',
'handler_settings' => [
'negate' => 0,
'target_bundles' => [
'geographic_coordinates' => 'geographic_coordinates',
],
'target_bundles_drag_drop' => [
'geographic_coordinates' => [
'enabled' => TRUE,
'weight' => 2,
],
],
],
'field_type' => 'entity_reference_revisions',
],
])->save();
// Set component in form display section.
entity_get_form_display('node', $bundle, 'default')
->setComponent('field_gc_coordinates', $field_settings)
->save();
// Set component in display section.
entity_get_display('node', $bundle, 'default')
->setComponent('field_gc_coordinates')
->save();
}
// Detach field if already there.
elseif (!empty($field) && !$flag) {
$field->delete();
}
}
- Log in to post comments