Community

Why we pass arguments with reference in the hook_form_alter function ?

1-Why we need to pass the arguments '&$form, &$form_state' with reference ?
Why we without a reference ?
2-Why we don't pass the '$form_id' argument with reference(&) ?

That is the hook_form_alter function.
http://api.drupal.org/api/drupal/modules%21system%21system.api.php/funct...

<?php
function formexample_form_alter(&$form, &$form_state, $form_id) {
// This code gets called for every form Drupal builds; use an if statement
// to respond only to the user login block and user login forms.
if ($form_id == 'user_login_block' || $form_id == 'user_login') {
// Add a dire warning to the top of the login form.
$form['warning'] = array(
'#markup' => t('We log all login attempts!'),
'#weight' => -5
);
// Change 'Log in' to 'Sign in'.
$form['submit']['#value'] = t('Sign in');
}
}
?>

* The code from the Pro Drupal Development book.

Comments

As the description

As the description says:

Perform alterations before a form is rendered.

You can pass a variable by reference to a function so the function can modify the variable.

http://php.net/manual/en/language.references.pass.php

The form id is not passed by reference because you will never want to change it. Every form calls hook_form_alter() so the id is there to help you identify the form you want to alter so you can then put the below condition to alter only that form:

<?php
if ($form_id == 'my_form') {
 
// code here to alter the form
}
?>
nobody click here