Модификация форм в Drupal 5 and 6

Drupal имеет много форм и не все из них могут быть такими как вы хотите. Изменение форм является темой, которая часто встречается. Есть два метода решения этой задачи. Прочитав эту статью Вы сможете изменять и настраивать формы в самые короткие сроки, в ней мы кратко обсудим, что происходит, а затем сосредоточимся на показе рабочих примеров для обоих методов в Drupal 5 и 6. Вам должно быть удобно и комфортно создавать новые функции и отображение массивов. Имейте также в виду, что в примерах ниже я завернул код в php теги, но вы не должны включать их когда копируете/вставляете код. Просто именно так код выглядит красивым и ясным для статьи.

Deciding to make the change in the theme or a module

So there are two methods for altering form output in Drupal, one at the theme layer and the other through a custom module. Changes to the HTML can be accomplished with either method so most people will use the method they are more comfortable with already; themers use the theme and developers use a module. There are two situations however where you will want to use a module rather than a theme:

1. Changing functionality of a form (e.g. adding new validation rules or submission actions) can only happen in a module.
2. Form elements can be completely removed from the HTML in the theme layer but from an access point of view, someone could still theoretically make use of the field in the form's $_POST because the field is not removed from the form process, only the HTML output is removed. Removing an element in a module can completely remove access to it in any way. This is not often a real problem so removing elements in the theme layer is still acceptable but you should be aware of the distinction.

An example form: User edit

We will not be looking at functional changes here so I'm going to take an example that works well in either method and you can choose the one you prefer. The code is actually quite similar and both rely on two important things: 1) the form's ID and 2) some knowledge of Drupal's Form API.

Our example is altering the form as seen at My Account > Edit in a very simple way to change the name of the Username field from "Username" to say "Login ID" instead.
Find the form ID

All forms in Drupal have a unique ID. You can easily find the form ID in the HTML source code. On your site view the source of the page (Firebug is very handy for this) and look for some hidden fields near the top of the form. One has token stuff in it and another will have the name="form_id". You want to use the value of that form_id input field. In this example it looks something like this (in D5):

The value for our form ID in this case is "user_edit". In Drupal 6 the form ID is "user_profile_form".

[Note that if you find the form element, e.g.

Creating your override functions

Now let's start creating the function we will need to hook into the form. Of course, naming and placing the function is slightly different for themes and modules. In all of these examples replace themename and modulename as appropriate. Here is the starting code:
Option 1: Override the theme function

In the theme layer place your function in your theme's template.php file. You will create a new function for each form you wish to modify.

D5 theme override

<?php
 
/**
   * Theme override for user edit form.
   *
   * The function is named themename_formid.
   */
 
function themename_user_edit($form) {
   
// Our kickin' mods go here.
 
}
?>

D6 theme override

In D6 theming this gets interesting. Many forms in D6 now have template files or "registered" functions. For those you can simply edit the .tpl file or override the function as in D5, respectively. For forms that do not have templates or registered functions though, we need to not only create the override function but we also need to register it with the theme system so it knows about it. For more information on this see the handbook page about using the theme registry for special cases. Our user edit form is one such beast. Once you add the following functions to your template.php, reset the the theme registry by clearing cached data on the Administer > Performance page.

Here is the theme registry function we need:

<?php
/**
* Implementation of hook_theme.
*
* Register custom theme functions.
*/
function themename_theme() {
  return array(
   
// The form ID.
   
'user_profile_form' => array(
     
// Forms always take the form argument.
     
'arguments' => array('form' => NULL),
    ),
  );
}
?>

And here is the override function; very much like the D5 version except the form ID.

<?php
 
/**
   * Theme override for user edit form.
   *
   * The function is named themename_formid.
   */
 
function themename_user_profile_form($form) {
   
// Our kickin' mods go here.
 
}
?>

Option 2: Use hook_form_alter() in a module

D5 hook_form_alter()

If using a module, place the hook_form_alter function (D5 and D6) in your .module file. You will use this one function to change as many forms as you want, using the form ID in a switch statement for each one.

<?php
 
/**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
 
function modulename_form_alter($form_id, &$form) {
   
// Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
   
switch ($form_id) {
     
// This is our form ID.
     
case 'user_edit':
       
// Our kickin' mods go here.
       
break;
  }
?>

D6 hook_form_alter()

<?php
 
/**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
 
function modulename_form_alter(&$form, $form_state, $form_id) {
   
// Normally a switch is used because you may want to alter more than
    // one form and it is easy to add a new case for each form.
   
switch ($form_id) {
     
// This is our form ID.
     
case 'user_profile_form':
       
// Our kickin' mods go here.
       
break;
  }
?>

Viewing the $form array

Once you have gotten the form ID and created the function you will need to see the existing form elements you have to work with. If you have Devel module enabled, you can use the shorthand dsm() function to pretty print the $form array. Otherwise a regular PHP print_r() or var_dump() will work fine. Whichever function you use to see the array, place it in your function where the code above says "// Our kickin' mods go here." For example:

<?php
 
function themename_user_edit($form) {
    return
print_r($form);
  }
?>

When you reload the page with the form on it now you should see a whole bunch of yummy array output. If not, make sure you got the form_id correct and that you have named your function properly.

Find the form element and property

There is quite a bit of documentation on FAPI and I'm not going to dive very deeply here. Here are the quickstart guides for D5 and D6. You may also want to have a handy bookmark to the form property reference, D5 and D6.

Basically we are going to look at the $form array, find what we want to change and translate that into FAPI for our function. Let's look at a section of the $form array that I am seeing on my site:

<h3>Array</h3>

(
  [account] => Array
  (
    [#type] => fieldset
    [#title] => Account information
    [name] => Array
    (
      [#type] => textfield
      [#title] => Username
      [#weight] => 0
      [#maxlength] => 60
      [#description] => Your preferred username; punctuation is not allowed except for periods, hyphens, and underscores.
    )
  )
  [pass] => Array
  (
    [#type] => password_confirm
    // snip...
  )
  // snip...
  [comment_settings] => Array
  (
    [#type] => fieldset
    [#title] => Comment settings
    [#collapsible] => 1
    [#collapsed] =>
    // snip...
  )
)

So basically we need to walk down the array to find the part we want to change. I want to change the title of the Username field. Looking at my array that means I want to target [account][name][#title]. Once I find that, I can just put that in my function and tell Drupal what I want to do with it. If you aren't sure what you can do to a given property, looking at the reference chart linked to above will help out.

$form['account']['name']['#title'] = t('Login ID');

I can do this with any of the FAPI properties. For example I can set the default value of a fieldset to collapsed:

$form['comment_settings']['#collapsed'] = TRUE;

Or I can even remove an element entirely using the unset function. This would remove the Signature text area from the form:

unset($form['comment_settings']);

So, go have fun and get those rascally forms just the way you want them!
Full example functions

Here are the complete, commented example functions.

Theme override: D5

<?php
/**
* Theme override for user edit form.
*
* The function is named themename_formid.
*/
function themename_user_edit($form) {
 
$output = '';
 
// Print out the $form array to see all the elements we are working with.
  //$output .= dsm($form);
  // Once I know which part of the array I'm after we can change it.

  // You can use the normal Form API structure to change things, like so:
  // Change the Username field title to Login ID.
 
$form['account']['name']['#title'] = t('Login ID');

 
// Make sure you call a drupal_render() on the entire $form to make sure you
  // still output all of the elements (particularly hidden ones needed
  // for the form to function properly.)
 
$output .= drupal_render($form);
  return
$output;
}
?>

Theme override: D6

Theme registry function:

<?php
/**
* Implementation of hook_theme.
*
* Register custom theme functions.
*/
function themename_theme() {
   return array(
    
// The form ID.
    
'user_profile_form' => array(
      
// Forms always take the form argument.
      
'arguments' => array('form' => NULL),
     ),
   );
}
?>

Form override function:

<?php
/**
* Theme override for user edit form.
*
* The function is named themename_formid.
*/
function themename_user_profile_form($form) {
 
$output = '';
 
// Print out the $form array to see all the elements we are working with.
  //$output .= dsm($form);
  // Once I know which part of the array I'm after we can change it.

  // You can use the normal Form API structure to change things, like so:
  // Change the Username field title to Login ID.
 
$form['account']['name']['#title'] = t('Login ID');

 
// Make sure you call a drupal_render() on the entire $form to make sure you
  // still output all of the elements (particularly hidden ones needed
  // for the form to function properly.)
 
$output .= drupal_render($form);
  return
$output;
}
?>

Module hook_form_alter(): D5

<?php
/**
* Implementation of hook_form_alter().
*
* This lets you make changes to any form in the site. You can alter, remove
* or add form elements. You can also alter the validation and submission
* behavior. The name will always be modulename_form_alter.
*/
function modulename_form_alter($form_id, &$form) {
 
// Normally a switch is used because you may want to alter more than
  // one form and it is easy to add a new case for each form.
 
switch ($form_id) {
   
// This is our form ID.
   
case 'user_edit':
     
// Print out the $form array to see all the elements we are working with.
      //$output .= dsm($form);
      // Once I know which part of the array I'm after we can change it.
    
      // Change the Username field title to Login ID.
     
$form['account']['name']['#title'] = t('Login ID');
      break;
  }
}
?>

Module hook_form_alter(): D6

<?php
/**
* Implementation of hook_form_alter().
*
* This lets you make changes to any form in the site. You can alter, remove
* or add form elements. You can also alter the validation and submission
* behavior. The name will always be modulename_form_alter.
*/
function modulename_form_alter(&$form, $form_state, $form_id) {
 
// Normally a switch is used because you may want to alter more than
  // one form and it is easy to add a new case for each form.
 
switch ($form_id) {
   
// This is our form ID.
   
case 'user_profile_form':
     
// Print out the $form array to see all the elements we are working with.
      //$output .= dsm($form);
      // Once I know which part of the array I'm after we can change it.
    
      // Change the Username field title to Login ID.
     
$form['account']['name']['#title'] = t('Login ID');
      break;
  }
}
?>

Il n'est gnralement pas bien

Il n'est gnralement pas bien bon approvisionnement de ce genre. Impossible d'accord avec cela, l'article trs intressant, vous avez produit quelques facteurs bnfiques l.]]>air max pas cher]]>


Je suis abonn votre flux monte qui doivent faire l'affaire! Have a nice day! Merci pour l'effort, continuez sur cette bonne ?uvre la Grande, il est bonne ide.]]>air max 90]]> J'espre que vous avez une bonne journe!

~]]>moncler

]]>moncler jackets]]>~current time point daily life, there does exist together different characters check lighting good ~]]>moncler jackets discount]]>~ energy. For the successful implementation of the rapid time of day time points common development and industry enhancement,~]]>moncler jackets 2011]]>~living standard has any extension almighty, ~]]>Moncler Handbags]]>~ and greatly reduce Moncler advertising costs continue with leather ~]]>Moncler Boots]]>~ to get in the structure of the solution.the characteristics of a ~]]>Moncler Men Jackets]]>~, any information for cultivation, exchange clothes, any a use for affectionate, ~]]>Moncler Men Vest]]>~ is surrounded by each ordinary essentials, ~]]>Moncler Men Shoes]]>~ including the giant waves space structure point of view. Bright the reasonable layout of the teenagers leather coats features ~]]>Moncler Women Sweater]]>~ almost did not happen in a longer period of small, will education including biology, comprehensive array unite together and suit ~]]>Moncler Women Jackets]]>]]>http://www.topjacketsmarket.com/]]> is undoubtedly an absolute thorn usually the last word.

The latest #]]>nba basketball

The latest #]]>nba basketball shoes]]># in 2011 the supreme can be found ]]>http://www.nbashoesmvp.com/]]>. #]]>Lebron James Shoes]]># Basketballshoes series is through years of development, this #]]>Kobe Bryant Shoes]]># series of Flywire always "fly line" technology based on #]]>Tracy McGrady Shoes]]># basketball shoes,#]]>Derrick Rose Shoes]]># including a series of players, including #]]>Rajon Rondo Shoes]]># have a soft spot for this series. Innovation on the Flywire greatly reduce the use of #]]>Dwight Howard Shoes]]>#, light weight. The latest #]]>Dwyane Wade Shoes]]>#use a new generation of fly line Flywire technology,#]]>Kevin Durant Shoes]]># make the light of the shoe more than two generations before more breathable design. The new glass fiber reinforced materials parts 2011 enough #]]>Michael Jordan Shoes]]># black and white red for the wearer's mobile,#]]>Lebron James 9]]># make changes to fast response and support.

The latest ‖]]>cheap nike

The latest ‖]]>cheap nike shox]]>‖series is compatible with "Nike + technology." ‖]]>nike shox shoes]]>‖recently Shox this feature.‖]]>air max lebron 8]]>‖ and ‖]]>derrick rose shoes]]>‖should provide superior damping technology and elastic foam, the same ‖]]>nike shox tl1]]>‖ and ‖]]>nike shox tl3]]>‖quality online sales. ‖]]>nike shox r5]]>‖shoes breathable characteristic shox qualification,‖]]>nike shox r4]]>‖ can make your foot comfortable than ever. In the heel, we can feel the six column Nike shox system, especially when you wear ‖]]>nike shox oz]]>‖,‖]]>nike shox nz]]>‖ can make our feet more comfortable. A nike Shox with green shoes, new design shoes have been to update at ]]>http://www.nikeshoxgreat.com/]]>.

Nowadays, Coach Purses

Nowadays,

Coach Purses Outlet

one of eye-catching fashion designer mark is

Coach Factory Outlet

. There seemed to be actually a big

Coach Factory Online

alteration among alive living of their

Coach Outlet Online

metropolis and USA. The

Coach Outlet Online

majority of girls and ladies

Coach Factory

are usually attacked and

Coach Outlet Online

attracted by fashionable and sexy

Coach Outlet

. Concerning trust by the

Coach Factory Outlet

country people understand

Coach Outlet

very well what to give on today. In the USA,

gucci backpack

everyone has educated everyone.

Coach handbags outlet

and Purses have durable quality,

cheap coach purses

exquisite craftsmanship and expressive design to win the fashion

Coach Factory

industry's attention. Although it's

Coach Factory Online

low tone design is simple, but

Coach Factory Outlet

continuously from the classical

Coach Factory Outlet Online

American tradition for inspiration,

Coach Outlet Canada

and integration of the modern American style,

Coach Outlet

traditional and popular interpretation

Coach Factory

of perfect balance. It is the stylish

Coach Factory Online

design and durable quality of the

Coach Factory Outlet

and Purses that make the name

Coach Factory Online

known by people all over the world.

Cheap Coach Purses

currently are commonly worn by mostly every other female you look around. And if you too want to

Discount Coach Handbags

look perfect and reflect your choice of latest fashion

discount coach purses

, you need to buy a decent and smart

Coach Outlet Online

that is easily available

Coach Factory Online

in the

Coach Factory Outlet Online

. You will need to hunt around a lot of shops to get one

Coach Factory

you always longed for. You also feel satisfied if you get

Coach Outlet Online

the worth for the money you pay for. Anyway, you will find the

Coach Outlet Online Store

and realize its reliability and utility.

Настройки просмотра комментариев

Выберите нужный метод показа комментариев и нажмите "Сохранить установки".