Ok, so, I've been beating my head into the Forms API documentation for a bit. And I understand the basic structure of how to *create* forms in the new 4.7 Forms API. I'm a bit confused, however, about how to *alter* existing forms.

Particularly, I'm trying to alter the gmap module form so that it doesn't ask people to enter in their street address information in the user>edit>location form. Normally, I would do this using a hack (see the commented lines below):

$form = array(1 => array());
    //the hack begins here -- I'm commenting this next line out, because I don't want people to feel pressured to put in their street address ajwwong
	//$form[0]['location'] = location_form(array('street', 'city', 'province', 'postal_code', 'country'), (isset($user->location) && !_location_is_empty($user->location)) ? location_api2form($user->location) : array('country' => variable_get('location_default_country', 'us')));
// and I'm adding this line below -- the new "hacked" line -- without the "street" information field.
    $form[0]['location'] = location_form(array('city', 'province', 'postal_code', 'country'), (isset($user->location) && !_location_is_empty($user->location)) ? location_api2form($user->location) : array('country' => variable_get('location_default_country', 'us')));
    $form[0]['location']['#type'] = 'fieldset';
    $form[0]['location']['#title'] = t('Location');
    $form[0]['location']['#tree'] = TRUE;
    $form[0]['location']['#description'] = t('Enter as much of your address as you are comfortable with. Your address will only be viewable by those who have the appropriate permissions. The site will be able to automatically link you to driving directions and other features if it already knows your address.');
    return $form;
  }

However, all the documentation / message board messages say that the "drupal way" to do this is through a form_alter function.

But here's the problem, I can't figure out how to implement the form_alter function to eliminate the "undesireable field".

Here's my best guess, based on the documentation (don't laugh! I know I'm reaching in the dark):
I create a new module (say, called "changeform.module") with the function"


function gmap_form_alter_location {
  $form[0]['location'] = location_form(array('city', 'province', 'postal_code', 'country'), (isset($user->location) && !_location_is_empty($user->location)) ? location_api2form($user->location) : array('country' => variable_get('location_default_country', 'us')));
}

Based on what I've read, that's my best guess, but heck, I'm kinda reaching in the dark here.
All help much appreciated Thx.

Albert
www.ithou.org

Comments

ajwwong’s picture

btw, I thought it was a hack to the gmap module, but it's really from the location module code.

But still... how do you use form_alter?

Albert
www.ithou.org

nedjo’s picture

1. you need to include the arguments in your form_alter function.

/**
 * Implementation of hook_form_alter().
 */
function changeform_form_alter($form_id, &$form) {

2. test to see if it's the form in question. You first have to determine the id of the form. In your case, I don't know. But if you look at the source of the HTML page that the form is on, it will have a form_id as a hidden variable. I'm just putting 'form_id' here.


  if ($form_id == 'form_id') {

3. then alter it. You need to know the fieldname you're altering and where it is in the form--it might be nested. you could (a) unset the field,


    unset($form['fieldname']);

or (b) turn the field into another type. Here we turn it into a hidden field, giving it the default value it already had.


$form['fieldname'] = array(
  '#type' => 'hidden',
  '#value' => $form['fieldname']['#default_value']
);

Altogether, the task you've chosen is quite complicated. You might be better off commenting out as you've done!

ajwwong’s picture

Thanks, nedjo, for the play-by-play! Those are great tips. It sounds pretty complicated, but I'll toss with it some more... It will at least get me started a little bit further down "the drupal way"! :-)

Albert
www.ithou.org

ymcp’s picture

Thanks for that useful tip. I used it as the basis for this function which shows how to change just one attribute of an already existing form field (note the use of arrary_merge):

function mynodetype_form_alter($form_id, &$form) {
  if ($form_id == 'mynodetype_node_form') {
    $alteration = array('#description' => t('ALTERED!!!!'));
    $form['myfield'] = array_merge($form['myfield'], $alteration);
  }
}

This works on any of "my" fields in a custom module's data-entry screen, but doesn't seem to work on fields such as taxonomy category or file attachment, ie fields created by other modules.

I assume that this is because my form_alter hook is being called before those other modules have had a chance to add "their" fields to my form.

I found this post that seems to suggest that it is related to the "module weight". I've checked & my custom module and the taxonomy module both have a weight of "0". I changed my module's weight to +1 then -1, but neither made any difference.

Any thoughts?

dman’s picture

Good you found module weight.
Your 'alter' weight must be higher (run after) the thing you wish to alter.

When changing the weight, you may have to re-visit and save the modules page to get the new weighting cached/noticed. (guess OTTOMH)

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/