0

I am trying to add a cancel button to a custom content type form (typename/type/add). I have looked around and there seems to be two main ways of doing this:

  1. Creating a template file with the name page--node--add--typename.tpl
  2. Implementing the hook_form_alter either in the theme template.tpl.php or in a custom module.

then adding code like

$form['actions']['cancel'] = array(
    '#type'   => 'submit',
    '#value'  => t('Cancel'),
    '#access' => TRUE,
    '#weight' => 55,
    '#submit' => array('cancel_button_form_cancel', 'node_form_submit_build_node'),
    '#limit_validation_errors' => array(),
);

In the first case, after rendering the form with drupal_render($form) the button is added but only the form is rendered. Main menu and other blocks are not rendered.

However in the second case the button never appears. Even though something like drupal_set_title('Title') does change the title.

Any solution to either issue would be most welcome.

Edit The first part of this question is now a separate one here.

2
  • 3
    Can you show the whole form_alter function? It might be you're not passing $form in by reference...
    – Clive
    Feb 12, 2013 at 21:00
  • Just posted the edit at the same time as your comment. Indeed you were right. Thanks. Feb 12, 2013 at 21:04

2 Answers 2

0

Ok found the problem with implementing the hook, I copy pasted the code from some post and it was passing $form variable by value. The correct signature of the alter_form should be

modulename_form_alter(&$form, &$form_state, $form_id)
0

Yes, you have to use hook_form_alter or hook_form_FORM_ID_alter functions to alter your form and to add new form elements to an existing form in your theme template.php or module files. You should not edit your form in template files directly.

Example (template.php):

 function your_theme_form_YOUR_FORM_ID_alter(&$form, &$form_state, $form_id){
$form['actions']['cancel'] = array(
    '#type'   => 'submit',
    '#value'  => t('Cancel'),
    '#access' => TRUE,
    '#weight' => 55,
    '#submit' => array('cancel_button_form_cancel', 'node_form_submit_build_node'),
    '#limit_validation_errors' => array(),
);
}

After that you can get rendered form in template_preprocess_page function:

 function your_theme_preprocess_page(&$variables) {
$variables['your_form'] = render(drupal_get_form('your_form_id'));
   }

Now variable $your_form is available in your page template file.

You can print it this way <?php print $your_form; ?>

Maybe main menu and other blocks are not rendered because you didn't added their regions to the template file like this <?php print $page['content']; ?>

You have to place your drupal_set_title('Title') function in the your_theme_preprocess_page or your_theme_form_YOUR_FORM_ID_alter function to change title.

Your Answer

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

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