1

Based on this answer, I added a cancel button to my user picture upload form like this:

  $types = array(
    'profile_picture',
  );

  foreach($types as $type) {
    if($type.'_node_form' == $form_id) {
      if(arg(1) == 'add') {
        // If adding node link to node/add screen
        $link = l(t('Cancel'), 'account/manage-pictures', array('attributes' => array('class' => array('form-submit','cancel'), 'id' => 'edit-cancel')));
      }
      elseif(arg(2) == 'edit') {
        // If editing node, link to node view screen
        $link = l(t('Cancel'), 'node/'.arg(1), array('attributes' => array('class' => 'form-submit, cancel', 'id' => 'edit-cancel')));
      }
      $form['cancel'] = array(
        '#type' => 'markup',
        '#markup' => $link,
        '#weight' => 0,
      );
    }
  }

However, this causes the red cancel button to be placed at the top of the form instead of the bottom of the form next to the purple save button as shown in the screenshot below.

screenshot illustrating placement of cancel button

When I examined the CSS in Firebug, I saw that the save button is under this div: <div id="edit-actions" class="form-actions form-wrapper">, while the cancel button is not. Is there a way to place the cancel button under the same div as the save button?

3
  • Which buttons are the red, and purple ones?
    – apaderno
    Aug 18, 2012 at 6:37
  • I should have been clearer. I see that the top button is red, and the one at the bottom is purple. What I meant is what buttons are those, and which module adds them. Also, your code shows a "Cancel" link, but the screenshot is not in English. It's not clear where in the screenshot that link is.
    – apaderno
    Aug 18, 2012 at 8:35
  • The top red button is the cancel button, which is provided by my custom code. The bottom purple button is the save button for a new content type, which is provided by Drupal by default. Aug 18, 2012 at 10:40

2 Answers 2

2

Use the following code.

$form['actions']['cancel'] = array(
 '#type' => 'markup',
 '#markup' => $link,
);

If it doesn't work, check with print_r() where the submission buttons are added in the form array. Usually, it's $form['actions'], but sometimes can be a different array index.

1

Use $form['buttons']['cancel'] = /* … */;.

buttons is just an example; it depends on how the form was created. Some forms call it actions or something else. Inspect the form variable to find out where to put your button.

Sometimes the save button won't be in a special div. In that case you only need set the weight to something high in order to get it last.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.