API, PHP

Using a form hook alter in drupal exmaple1

Modifying Forms Using Alter Hooks

The ability of themes to use alter hooks is new in Drupal 7.

 

Using an alter hook is perfect for quick changes like:

-Simple changes to form labels, descriptions, and other properties.
-Changing the order in which the form elements print using the #weight property.
-Wrapping a few elements in a <div> or <fieldset>.
-Hiding or removing form elements that are not required.
-Adding some markup to a form.

 

  1. hook_form_alter(): Runs for all forms.
  2. hook_form_FORM_ID_alter(): Runs for a specific form ID.

 

Implementation of hook_form_alter() to target all or multiple forms

Code:Implementation of hook_form_alter() to target all or multiple forms

  • <?php
  • /**
  •  * Implements hook_form_alter().
  •  */
  • function mytheme_form_alter(&$form, &$form_state, $form_id) {
  • // Changes made in here affect ALL forms.
  • if (!empty($form[‘title’]) && $form[‘title’][‘#type’] == ‘textfield’) {
  • $form[‘title’][‘#size’] = 40;
  • }
  • }

 

 

Code:Implementation of hook_form_FORM_ID_alter() to target a specific form

CODE:. Implementation of hook_form_FORM_ID_alter() to target a specific form

  • <?php
  • /**
  •  * Implements hook_form_FORM_ID_alter().
  •  */
  • function mytheme_form_contact_site_form_alter(&$form, &$form_state) {
  • // Add a #markup element containing your note and make it display at the top.
  • $form[‘note’][‘#markup’] = t(“We’d love hear from you. Expect to hear back from us in 1-2 business days.”);
  • $form[‘note’][‘#weight’] = -1;
  • // Change labels for the ‘mail’ and ‘name’ elements.
  • $form[‘name’][‘#title’] = t(‘Name’);
  • $form[‘mail’][‘#title’] = t(‘E-mail’);
  • // Hide the subject field and give it a standard subject for value.
  • $form[‘subject’][‘#type’] = ‘hidden’;
  • $form

 

hook_form_alter

Example:

<?php>

/* implements hook_form_alter().  */

function demo_form_alter(&$form_state, $form_id ) {

 #krumo($form_id);          /* this will help you find the dir  , using # disable the location mapping*/

switch ( $form_id) {

case ‘user_profile_form’ :

$form[ ‘account’ ] [‘pass’] [#description’] = t (‘ To change the password here . . .  txt messages edit here . . ‘)

krumo($form);

break;

}

}

krumo(alert)

use the version7 :hook_form_alter(&$form, &$form_state, $form_id)

7 system.api.php hook_form_alter(&$form, &$form_state, $form_id)
4.7 core.php hook_form_alter($form_id, &$form)
5 core.php hook_form_alter($form_id, &$form)
6 core.php hook_form_alter(&$form, &$form_state, $form_id)
8 form.api.php hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)

Parameters

$form: Nested array of form elements that comprise the form.

$form_state: A keyed array containing the current state of the form. The arguments thatdrupal_get_form() was originally called with are available in the array $form_state[‘build_info’][‘args’].

$form_id: String representing the name of the form itself. Typically this is the name of the function that generated the form.

 

Testing Example Code:
*/

<?php
/**
* Implements hook_form_FORM_ID_alter().
*/
function mytheme_form_contact_site_form_alter(&$form, &$form_state) {
// Add a #markup element containing your note and make it display at the top.

// Change labels for the ‘mail’ and ‘name’ elements.
$label[‘Arrival Date’][‘#title’] = t(‘Date Plan’);
$form[‘form-required’][‘#title’] = t(‘Date End’);

}

 

source: api.drupal.org

http://themery.com/dgd7/advanced-theming/forms/with-alter-hooks

Add your comments

Loading Facebook Comments ...
Loading Disqus Comments ...