form api

Using form_alter to change the default value of a date

If you need to set a default value for an exposed views filter, you can do it like this:

this example code will set a ranged date to have a default value of -30 days for the start date, and today for the end date

<?php
function hook_form_views_exposed_form_alter(&$form, $form_state, $form_id) {

  switch (
$form_state['view']->name) {
  case
'my_view_name':
   
// the Date object works only with this date format:
   
$datemask = 'Y-m-d';
   
$form['field_date_value']['min']['#default_value'] = date($datemask, time()-30*86400);
   
$form['field_date_value']['max']['#default_value'] = date($datemask);
    break;

   
$datemask = 'd/m/Y';
   
$form['field_date_value']['min']['#date_format'] = $datemask;
   
$form['field_date_value']['max']['#date_format'] = $datemask;
  }
}
?>

Forms API - Modify Forms with hook_form_alter()

Drupal 7 forms that modules create can be changed by other modules and even themes with the help of Drupal FAPI hooks system. See Drupal 7 Hooks for a list of Drupal system hooks. The hook that allows to edit the existing forms is hook_form_alter. See the hook_form_alter() documentation at Drupal API site.

The syntax is hook_form_alter(&$form, &$form_state, $form_id).

In this hook call, $form is the array of form elements (see Drupal 7 Form API Overview article). $form_state has the keyed array of the state of the form, that can change on form submission or in multi-step forms. And an important parameter is $form_id, which has in it the unique name of the form. Drupal core will cycle through all the registered forms, passing their initial array, state, and unique id to this hook function.

Example:

Read more

Creating a builder function to generate a form

Form workflow usually follows these stages:

  1. Declaration of a form and its elements in a function
  2. Modification of existing forms with the FAPI hook system
  3. Theming of forms
  4. Form validation and submission

Forms are represented as arrays in form builder functions. Each item within the $form array on api.drupal.org corresponds either to a form element (an input or other HTML on the rendered form) or an element property (meta-data used by FAPI during the rendering or processing of elements). A property has a key name that begins with a "#", while an element does not.

Example

In this example, we have a module named mymodule and we want to declare a form builder function in it. The form declaration function receives at least two parameters:

  • $form - An array of elements and element properties
  • &$form_state - An array containing information about the current state of this form while a user is filling it out and submitting it. Importantly, this includes the values that the user may have entered into the form.

This simple form has a text field for the user name and a submit button.

<?php
/**
* Create a new form
*/

Read more

Multi-step forms

<< mforms project page

Installation and configuration

Mforms is a library, so by itself it does not provide any functionality (besides the real-life examples in the mforms example module). Therefore there is no configuration or installation besides enabling the module in the module list.

Basic usage

Included module mforms_example contains several real life examples that are documented in code. To find out what it does visit /mforms page after you enable mforms_example module.

Mforms architecture

For faster dive into mforms here is the basic architecture:

Prerequisites

STORE_KEY - not just identifies the storage slot for submitted values, it represents the identifier of whole multi-step form implementation.

Steps file - The file where individual form steps with their validate and submit callbacks reside. See mforms_example/mforms/mforms_example.session_store_example.inc for demonstration. This file must be named in the following pattern: MODULE_NAME.STORE_KEY.inc and placed in "mforms" directory of the module in which you are implementing the multi step form.

Store object - MformsIstore implementation responsible for storing submitted values and internal mforms data needed to control the stepping process.

Read more

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

HowTo: Copy the form buttons on node creation pages to the top

Background:

I have a custom content type with a lot of form elements (close to 100) and scrolling down to the bottom got a bit irritating when all you wanted to do it update or edit a node of this content type.
So I wanted to copy the form action buttons to the top (but still keep them at the bottom).

Here is how I did it:

1) Create a custom module

1a) In your sites/default/modules/ folder create a new folder CUSTOM_MODULE*

1b) Create the mandatory CUSTOM_MODULE.info and CUSTOM_MODULE.module files.

1c) Fill out the CUSTOM_MODULE.info file with some basic info. eg:

name = Custom node form modify module
description = Custom module to duplicate the form buttons on node creation forms and place them above the form items.
package = Custom
core = 7.x
version = 0.1

1d) Copy the following code into the CUSTOM_MODULE.module file. I'll then go through the code to explain what it does.

/**
* Implementation of HOOK_form_alter()
*/
function CUSTOM_MODULE_form_alter(&$form, $form_state, $form_id) {

/**
* Copy the action buttons (submit, preview, etc ..)
* and place them at the top of the form
*/
if(!empty($form['actions'])) {

foreach($form['actions'] as $name => $button) {

// I DEFINE EACH BUTTON SEPERATELY BECAUSE I WISH TO DEFINE THE ORDER

Read more
Subscribe with RSS Syndicate content
nobody click here