{"id":171,"date":"2012-05-21T08:26:34","date_gmt":"2012-05-21T08:26:34","guid":{"rendered":"http:\/\/wordpress.agileadam.com\/?p=171"},"modified":"2016-05-03T16:44:36","modified_gmt":"2016-05-03T20:44:36","slug":"quick-tip-adding-a-reset-button-to-a-drupal-form","status":"publish","type":"post","link":"https:\/\/agileadam.com\/2012\/05\/quick-tip-adding-a-reset-button-to-a-drupal-form\/","title":{"rendered":"Quick Tip: Adding a Reset button to a Drupal form"},"content":{"rendered":"

Using\u00a0hook_form_alter<\/em>\u00a0or hook_form_FORM_ID_alter\u00a0<\/em>one can easily add a “Reset” button to a Drupal form.<\/p>\n

In this example we’re actually trying to reset anything the user typed after the form loaded. This will not remove\u00a0the default values.<\/p>\n

Here’s a simple example (Drupal 6)<\/p>\n

\/**\r\n * Implementation of hook_form_alter().\r\n *\/\r\nfunction mymodule_form_alter(&$form, &$form_state, $form_id) {\r\n  switch ($form_id) {\r\n    case 'myform':\r\n      $form['buttons']['reset_button'] = array(\r\n        '#type' => 'markup',\r\n        '#value' => '<input class=\"form-button\" type=\"reset\" value=\"Reset\">',\r\n        '#weight' => 2000,\r\n      );\r\n      break;\r\n  }\r\n}<\/pre>\n

It may not be appropriate for you to put your new reset element into the\u00a0buttons<\/em>\u00a0array. Use dpm() (part of the Devel<\/a> module) to show what $form looks like. If you don’t understand this, and the code above isn’t working for you, you may try\u00a0$form[‘reset_button’] = array…<\/em>.<\/p>\n

Adjust the value of #weight to move the button around the form.\u00a0dpm($form)<\/em>\u00a0will show you weights (if any) of existing elements so you can make educated decisions about the weight of your new field.<\/p>\n

One last note about the\u00a0‘#type’ => ‘markup’<\/em>\u00a0line: this is not a requirement, but I like to include it for clarity.<\/p>\n

UPDATE:<\/strong>
\nHere’s what the field might look like for Drupal 7:<\/p>\n

$form['reset'] = array(\r\n  '#type' => 'markup',\r\n  '#markup' => '<input class=\"form-submit\" type=\"reset\" value=\"Reset\">',\r\n);<\/pre>\n","protected":false},"excerpt":{"rendered":"

Using\u00a0hook_form_alter\u00a0or hook_form_FORM_ID_alter\u00a0one can easily add a “Reset” button to a Drupal form. In this example we’re actually trying to reset anything the user typed after the form loaded. This will not remove\u00a0the default values. Here’s a simple example (Drupal 6) \/** * Implementation of hook_form_alter(). *\/ function mymodule_form_alter(&$form, &$form_state, $form_id) { switch ($form_id) { case ‘myform’: $form[‘buttons’][‘reset_button’] = array( ‘#type’ => ‘markup’, ‘#value’ => ‘<input class=”form-button” type=”reset” value=”Reset”>’, ‘#weight’ => 2000, ); break; } } It may not be appropriate for you to put your new reset element into the\u00a0buttons\u00a0array. Use dpm() (part of the Devel module) to show what $form looks like. If you don’t understand this, and the code above isn’t working for you, you may try\u00a0$form[‘reset_button’] = array…. Adjust the value of #weight to move the button around the form.\u00a0dpm($form)\u00a0will show you weights (if any) of existing elements so you can make educated decisions about the weight of your new field. One last note about the\u00a0‘#type’ => ‘markup’\u00a0line: this is not a requirement, but I like to include it for clarity. UPDATE: Here’s what the field might look like for Drupal 7: $form[‘reset’] = array( ‘#type’ => ‘markup’, ‘#markup’ => ‘<input class=”form-submit” type=”reset” value=”Reset”>’, );<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[79],"tags":[84,85,83,22,20],"_links":{"self":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/171"}],"collection":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":4,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":1676,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/posts\/171\/revisions\/1676"}],"wp:attachment":[{"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/agileadam.com\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}