Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, December 28, 2015

How do I make a back button with Drupal form API?

How do I make a back button with Drupal form API?


I need to do this but with Drupal forms:

  

I tried doing this but it did not work:

$form['back']['#prefix'] = ";  

and also:

$form['back'] = array(    '#type' => 'button',    '#value' => 'Back',    '#attributes' => array(       'class' => 'button-user',       'onclick' => 'location.href=edit',             )          );  

Answer by Marius Ilie for How do I make a back button with Drupal form API?


$form['back']['#markup'] = "";  

Answer by Roger for How do I make a back button with Drupal form API?


Another solution would be this one:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {    switch($form_id) {      case "YOUR_FORM_ID":          unset($form['#validate']); //Maybe necessary          $form['actions']['back'] = array(          "#type" => "button",          "#value" => t('Back'),          "#weight" => 15,          "#executes_submit_callback" => FALSE,          '#limit_validation_errors' => array(),          '#ajax' => array(            'callback' => 'YOUR_MODULE_go_back_callback'          )        );          break;      default:        break;    }  }    function YOUR_MODULE_go_back_callback() {    $html = '        ';    return $html;  }  

Answer by VivMajor for How do I make a back button with Drupal form API?


$form['back']['#markup'] = "";  

This works for any page.

Answer by Grizly for How do I make a back button with Drupal form API?


Just adding my version, which seems to work great in 7, just catch it in the rebuild cycle and redirect instead. Extensible, can add any other buttons to do things, note the spelling of value "Back" is the name of the "op" (operation).. something that confused and annoyed me until I figured it out.

function mymodule_something_form($form,&$form_state){        //... Rest of form        $form['unused_form_id_back'] = array(          '#type' => 'button',          '#value' => 'Back',      );      $form['submit'] = array(          '#type' => 'submit',          '#value' => 'Do Stuff!'      );      return $form;  }    function mymodule_something_form_validate($form, &$form_state)  {      if($form_state['values']['op'] == 'Back'){          drupal_goto('something/that_page');      }  }  

Answer by Neograph734 for How do I make a back button with Drupal form API?


I preferred a solution that required no JavaScript. Similar to the answer by Grizly, but without putting it in the form validation, which felt ugly. But the code below offers a link button.

function my_form(&$form, &$form_state) {    // Some form elements      // Regular submit button    $form['actions']['submit'] = array(      '#type' => 'submit',      '#value' => t('Submit'),    );      // Back button (different submit handler prevent the standard submit and takes us    // to the redirect-submit).    $form['actions']['back'] = array(      '#type' => 'submit',      '#value' => t('Go back'),      '#submit' => array('custom_back_button'),    );  }    // Custom form callback for redirection.  function custom_back_button($form, &$form_state) {    $form_state['redirect'] = '/edit';  }  

Answer by AshwinP for How do I make a back button with Drupal form API?


Simplest option in Drupal Form API, Using #attributes option.

$form['back-btn'] = array(      '#type'                 => 'button',      '#value'                => t('Back'),      '#attributes'           => array('onclick' => onclick='window.history.back();'),  );  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.