Last updated June 7, 2014. Created on January 24, 2012.
Edited by acabouet, tecjam. Log in to edit this page.

Background:

I have a custom content type with a lot of form elements (close to 100) and scrolling down to the bottom got a bit irritating when all you wanted to do it update or edit a node of this content type.
So I wanted to copy the form action buttons to the top (but still keep them at the bottom).

Here is how I did it:

1) Create a custom module

1a) In your sites/default/modules/ folder create a new folder CUSTOM_MODULE*

1b) Create the mandatory CUSTOM_MODULE.info and CUSTOM_MODULE.module files.

1c) Fill out the CUSTOM_MODULE.info file with some basic info. eg:

name = Custom node form modify module
description = Custom module to duplicate the form buttons on node creation forms and place them above the form items.
package = Custom
core = 7.x
version = 0.1

1d) Copy the following code into the CUSTOM_MODULE.module file. I'll then go through the code to explain what it does.

/**
* Implementation of HOOK_form_alter()
*/
function CUSTOM_MODULE_form_alter(&$form, $form_state, $form_id) {

  /**
   * Copy the action buttons (submit, preview, etc ..)
   * and place them at the top of the form
   */
  if(!empty($form['actions'])) {

    foreach($form['actions'] as $name => $button) {
     
     // I DEFINE EACH BUTTON SEPERATELY BECAUSE I WISH TO DEFINE THE ORDER
     // OTHERWISE YOU COULD SIMPLY USE:

/*
        $form["$name-copy"] = $button;
        $form["$name-copy"]['#weight'] = -1000;
*/

     
      // SUBMIT BUTTON
      if($name == 'submit') {
        // WE NEED TO USE DOUBLE QUOTATION MARKS
        // SO PHP CAN SEE AND INTERPRET VARIABLES WITHIN
        $form["$name-copy"] = $button;
        $form["$name-copy"]['#weight'] = -1001;
      }
     
      // PREVIEW BUTTON
      if($name == 'preview') {
        // WE NEED TO USE DOUBLE QUOTATION MARKS
        // SO PHP CAN SEE AND INTERPRET VARIABLES WITHIN
        $form["$name-copy"] = $button;
        $form["$name-copy"]['#weight'] = -1000;
      }
     
      // CHANGES DEPENDS ON DIFF MODULE
      if($name == 'preview_changes') {
        // WE NEED TO USE DOUBLE QUOTATION MARKS
        // SO PHP CAN SEE AND INTERPRET VARIABLES WITHIN
        $form["$name-copy"] = $button;
        $form["$name-copy"]['#weight'] = -999;
      }
     
      // DELETE BUTTON
      if($name == 'delete') {
        // WE NEED TO USE DOUBLE QUOTATION MARKS
        // SO PHP CAN SEE AND INTERPRET VARIABLES WITHIN
        $form["$name-copy"] = $button;
        $form["$name-copy"]['#weight'] = -998;
      }           

    } // end foreach
  }

}

As you can see we simply use the hook_form_alter() function to modify the form. If you only wish to apply this module to a single content type, you can filter the function by using:

if ($form_id == 'CONTENT_TYPE_node_form') {

// PLACE CODE IN HERE AND RENAME CONTENT_TYPE TO YOUR CONTENT TYPE
// CHECK THE FORMS SOURCE CODE IF UNCERTAIN OR USE
// dsm($form); // requires the devel module

}

2) Enable the module.

3) Clear Cache.

4) Try it out

* you may rename CUSTOM_MODULE to whatever you want, just make sure you rename it everywhere.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

cfitkin’s picture

Thanks for the code, very helpful. I made a small change so that the button order in the header matches the order in the footer.

<?php
function CUSTOM_MODULE_form_alter(&$form, &$form_state, $form_id) {
   
/**
    * Copy the action buttons (submit, preview, etc ..)
    * and place them at the top of the form
    */
   
if(!empty($form['actions'])) {
        foreach(
$form['actions'] as $name => $button) {
             
$form["$name-copy"] = $button;
           
$form["$name-copy"]['#weight'] = $button['#weight'] - 1000 ;
        }
    }
}
?>
tryitonce’s picture

The button appears at the top but I am getting I am getting this error with your much shorter version of the module:

Notice: Undefined index: #weight in CUSTOM_MODULE_form_alter() (line 10 of /var/www/....../sites/all/modules/CUSTOM_MODULE/CUSTOM_MODULE.module).

Drupal 7.15
--------------
with a bit more checking it seems the error message comes up only when the is just one form submit button on the page as in - ...../admin/modules

Thanks for the code - very helpful ......

-----------
Good luck .....
... more recent results of trying Drupal just once are -
www.native-power.de

tecjam’s picture

Well that makes sense since a single button will not have a weight defined.

NancyDru’s picture

There is a sub-module in the Util module that will do this.

kukle’s picture

Hi,

this one's not working with Display Suite, neither is it working within the function in Util with Display Suite.

It's not the size of the hump, it's the movement of the camel that's important...