Move a Field into an Existing Vertical Tab
In this blog post, we'll move a field into an existing fieldset in Drupal 7. There is a 'Path Override' field that we want to move into the 'path' fieldset, which is created by the Pathauto module. This change must be accomplished through code because it's not possible through the UI. The 'path' fieldset isn't available under a content type's 'Manage Fields' form:
Drupal's Form API is a powerful tool for building forms, and the hook_form_alter and hook_form_FORM_ID_alter functions are tools for extending and modifying existing forms. In this example, we're using the hook_form_FORM_ID_alter function since the alter applies to the node form only. Before employing this function, the form appears as below:
Below is the implementation of the hook_form_FORM_ID_alter. The 'Path Override' field exists on several content types, so we check if the field exists on a given content type before proceeding. Then we define the field inside the 'path' fieldset and unset the original field definition.
The field has been moved using the hook_form_FORM_ID_alter function and now appears as below:
The form alter gets us halfway there, but the field won't save in its present state. It's necessary to use the hook_node_update function to move the field back to its original location before saving:
Using the hook_form_FORM_ID_alter function and the hook_node_update function, we have moved a field into an existing fieldset.
Add new comment