form

Redirecting a Form

Using your hook_form_alter or hook_form_FORM_ID_alter function you can redirect where a node form will go after a user submits the form.

The default behavior for a node form is to go to the new node that the form just created.

If the new node received nid 30, then go to node/30. If, however, you would like to go to a thank you page that has nid of 10, you need to redirect the form to go to node/10 instead.

It's easiest to use hook_form_FORM_ID_alter in a module.

Let's say the module is called my_redirect and you've created a content type that has form_id of my_content_type_node_form. You'll be creating two functions:

my_redirect_form_my_content_type_node_form_alter() - this will add an additional submit handler
my_redirect_node_form_submit() - this will contain the address of the node to redirect to

Within your my_redirect module, you create the following:

function my_redirect_form_my_content_type_node_form_alter(&$form, &$form_state){

    $form['actions']['submit']['#submit'][] = 'my_redirect_node_form_submit';

}

You're appending so you do not override the entire default node_form_submit handler which still does a bunch of stuff we want it to do.

function my_redirect_node_form_submit($form, &$form_state) {

Read more

Basic Form Alterations

One of the most basic things you want to know if you start digging into the code is NOT how to build an entire form from scratch or something super specific. You start with simple things like altering existing forms.

Within Forms API there are two functions that are your friends, in this regard: hook_form_alter and hook_from_FORM_ID_alter.

The child pages of this page should address basic alterations you can make to your forms using these hooks.

Buttons

This page is a draft

Problem

The user needs to perform an action on the data entered in a form, such as submitting a query, or saving or previewing their content.

Solution

Single button

[img, TBA]

Multiple buttons

[img, TBA]

The primary goal of Web form design is to get people through a form quickly and painlessly. Where possible, create forms for a single purpose and provide a single button to complete that task.

Sometimes, however, forms will require additional, secondary actions, such as 'preview' as an alternative to 'save'. In such cases, visual distinctions are a useful method for helping people make good choices. The most important distinction is between the the primary submit button and any secondary buttons (the 'primary' submit button is the submit button that is essential to the form, and which fulfills its primary purpose). Therefore, in multi-button forms, the primary form button should be visually emphasized. For consistency, single-button forms should also visually emphasize their submit button.

Read more

Form Field css

Here you can remove the classes that drupal adds to each form field.

Markup changes:

Remove the inner div from forms:
<form><div>…</div></form>` -> `<form><div>…
In HTML5 theres no need for an inner div for it to validate (yeah)

How to Make a Simple Module with a Form and Menu Link

This guide will show you how to add a form to your website that is accessed through its own URL. This guide will go over module creation, form creation, and the menu hook function.

Creating the module structure
The first step in creating the module is to create a folder for the module. You should put this in "sites/all/modules/{name of your module}." Keep in mind that whenever I say "{name of your module}," you should insert the name of your module. For example, we will use the module directory "sites/all/modules/form_example."

Now, create two empty text files called "{name of your module}.module" and "{name of your module}.info."

These two files are required by your module. The .info file contains information about what your module does, while the .module file contains the actual code including hooks. Hooks are functions with special names that will be called by Drupal when actions occur. For example "form_example_menu()" is called when menu information is gathered. In many examples, you will see names such as "hook_menu()" or "my_theme_menu()" and in these cases "my_theme" and "hook" should be replaced by the name of your module or theme.

Build the basic module files

Here is what we will put into the form_example.info file. This tells Drupal and users about your module.

core = "7.x"

Read more

Enable submit via Enter key on Ajax forms

See first comment for alternative method.

Users often submit forms by pressing the Enter key while the cursor is positioned in a text field. This is a convenient feature to quickly submit a form from the place of last edit (especially on forms that are very long) instead of having to scroll down to the Save button, position the mouse over it and click. An issue I ran into with Drupal 7 is that this is no longer working in node forms, because of the extensive use of Ajax. The Enter key is pressed after editing a text field and nothing happens. After searching quite a bit, I found a solution:

add the following function to template.php, clear cache and then test it out:

function YOURTHEME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'YOURFORMID') {
      $form['YOURINPUTNAME']['#attributes']['onkeypress'][]='if(event.keyCode==13){this.form.submit();}';
  }
}

This can be enabled for multiple inputs. As an example, if you are using the Garland theme and want to add the abitilty to submit the node form by pressing Enter in both the "Title field" and the "Authored by" field for a content type "Page" the code would look like this:

function garland_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'page_node_form') {

Read more
Subscribe with RSS Syndicate content
nobody click here