I wanted to modify the existing fields in a form. I tried using hook_form_FORM_ID_alter but I could only add new fields; I could not alter the properties of existing field. I wanted to add ajax properties to some existing fields.

I wanted to alter the content add form which is created by Content Type. I wanted to add an Ajax property to the content creation form. I couldn't figure out a way to add this while creating the content type. Could anyone please help on how to do this? It's very urgent.

This is the DPM for the field.

field_mstyle (Array, 6 elements)

#type (String, 9 characters ) container
#attributes (Array, 1 element)
#weight (String, 2 characters ) -3
#tree (Boolean) TRUE
#language (String, 3 characters ) und
und (Array, 18 elements)
    #entity_type (String, 4 characters ) node
    #bundle (String, 12 characters ) pz_main_menu
    #field_name (String, 12 characters ) field_mstyle
    #language (String, 3 characters ) und
    #field_parents (Array, 0 elements)
    #columns (Array, 1 element)
    #title (String, 10 characters ) Menu Style
    #description (String, 0 characters )
    #required (Boolean) FALSE
    #delta (Integer) 0
    #type (String, 6 characters ) select
    #default_value (Array, 1 element)
    #multiple (Boolean) FALSE
    #options (Array, 4 elements)
    #value_key (String, 5 characters ) value
    #element_validate (Array, 1 element)
    #properties (Array, 4 elements)
    #after_build (Array, 1 element) 

You'll usually find the elements for fields buried in the form and you'd alter them like so:

$form['field_my_field']['und'][0]['value']['#ajax'] = array(
  // ajax settings

I strongly recommend installing the Devel module and using the dpm() function to perform debugging. You can use it to print any variable to the standard message area, and it allows you to inspect arrays and objects.

You could easily find out where abouts in the $form array the fields you're looking for are by including a line like this in your form alter function:

  • I ve used dpm to see the array list of form. In und of the particular field I dont find #ajax. Can i still add ajax to field? – Drupal Developer Apr 1 '12 at 22:35
  • 2
    Yes you can add #ajax to any element, it doesn't matter if there's no #ajax array attached already...just add your own and it will be processed as normal – Clive Apr 1 '12 at 22:38
  • I have tried attaching the #ajax array to the existing field but It is not getting added to the properties. – Drupal Developer Apr 1 '12 at 23:06
  • I think you didn get my question. The fields are not added by me. The fields are already rendered by the Drupal. I wanted to add Ajax properties to those fields. I think we cant alter those fields by hook_form_alter. is there any other way around. – Drupal Developer Apr 1 '12 at 23:08
  • I'm not sure I understand...Drupal doesn't render any elements until after all hook_form_alters have been run. You can see from you dpm() output that the field has not been rendered, You just need to add ajax to it like so: $form['field_mstyle']['und']['#ajax'] = array(...); – Clive Apr 2 '12 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.