Search code examples
phpdrupaldrupal-7

drupal 7 how to add new fields and access them in a custom module form_alter function


I am trying to make some very simple edits to a legacy Drupal 7 project, but having not done anything with Drupal for several years I am stuck on what is probably quite a simple issue.

I am trying to add some extra fields to a form in a custom module.

I have added new fields via admin:

Home » Administration » Structure » Content types » MyType

I now want to be able to use them in the custom module form alter function

 fuction ..._form_alter(&$form, &$form_state) {}

When I try dumping out the content of the $form variable I can see previously existing fields, but I don't seen any of the new fields I added.

How can I add my fields to the form in my custom module so they can be access via _form_alter


Solution

  • First , ensure to all caches was cleared (bootsrap , webform , registry)

    Hook definition allow you to check $form_id so ensure you are dumping the right form

    yourmodulename_form_alter(&$form, &$form_state, $form_id){
      dsm($form_id); // or var_dump($form_id) if you don't have devel module
    }
    

    or directly make a condition :

    yourmodulename_form_alter(&$form, &$form_state, $form_id){
      if($form_id == 'myformid') dsm($form); // or var_dump($form) if you don't have devel module
    }
    

    And if you have more information, add it to post