Have you been in the situation where you've added a new field to a Drupal content type and you want that field to appear somewhere in the sidebar of the node add/edit page for that content type (instead of in the main column along with all the other fields)?
If so, the following snippet of code, added to a custom module on your site is exactly what you're looking for. In this example, a user reference field with the machine name of field_additional_authors was added to a Blog content type. This code places the field in the Authoring information accordion item in the sidebar:
/**
* Implements hook_form_alter().
*/
function my_module_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// Move the "Additional authors" field to the "Authoring information"
// accordion.
if (in_array($form_id, ['node_blog_edit_form', 'node_blog_form'])) {
$form['field_additional_authors']['#group'] = 'author';
}
}
Note that the add and edit forms have slightly different $form_id values.
Add new comment