How to modify dropdown field value of drupal content types

Let me tell you , at first attempt drupal can play game with you.

As a developer We can bold drupal at any point of time.

Suppose we have a content type like article in our drupal installisation

There we have created one drop down field and also we have added some default value to that.

Our intention is to manipulate that drop down while rendering in the add content form.

In drupal ,every content type entry form is having its own FORM ID.

Any form in drupal having unique FORM ID.

To manipulate that form or form’s element we need to pick that form id.

How to know form id in drupal

To know formId of drupal form ,Just create a module and write following code.

I have created one module called modulename and written the following code.

1
2
3
4
function modulename_form_alter(&$form, &$form_state, $form_id)
{
    drupal_set_message(t($form_id ));
}

Then from your browser ,just enable this module and goto entry form (Add content of any Drupal content type).

At the top of screen you can find the form id.Just copy that form id .

Procedure to use form alter and Drupal field database table

To manipulate form we need to use hook_form_alter.

In this case i have created a module callde modulename ,so just for that i have written a function called

modulename_form_alter().

If we look after Drupal database , you can find a table called field_config. From there you can get the desire field name ,that you want to manipulate.

In field_config database table there is 1 column called field_name

In my case the Field name is field_dropdown_nm.

1
2
3
4
5
6
7
8
function modulename_form_alter(&$form, &$form_state, $form_id)
{
     if($form_id == 'success_stories_node_form')
     {
        $form['field_dropdown_nm']['und']['#options'] =
                    array(1 => 'Bikash kumar sundaray');
     }
}

In my case i have added 5 data to the list type field ,called field_dropdown_nm

But i want to change content of type field like , i want to remove that 5 values from drop down and just want to add 1 value

that is

  • 1 as key
  • Bikash kumar sundaray as value

This we can monipulate drupal generated fields.