0

I have an entity reference field using Autocomplete widget type. I´d like to change the widget type to 'select list' for adding new node, and keep autocomplete when editing.

Have been two days I´m working on this. I didn´t found any solution.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
John
  • 33
  • 1
  • 4

1 Answers1

1

One way I've done this is using hook_form_alter. Create a custom module (if you don't have one already (let's call it mymodule for now) and add the function:

function mymodule_form_alter(&$form, &$form_state, $form_id)

In there you can check the id to see which form is being processed, it should be something along the lines of mytype_node_form but you can also inspect it fairly easily by executing something like drupal_set_message(print_r($form_id, true)); in the function.

You can check to see if you're adding or updating by checking $form_state['node']->nid. After that you can modify the form by doing something like the following:

function mymodule_form_alter(&$form, &$form_state, $form_id)
{
    // check to see if this is our form and it is a new node form (doesn't have an id yet)
    if ($form_id == 'mytype_node_form' && !isset($form_state['node']->nid)) {
        $form['field_coordinators']['und']['#type'] = 'select';
    }
}

That's just a start mind you, you'll likely have to change or even remove other properties but you can look into these settings by setting the field to use a select list and inspecting the structure of $form, again.

Godwin
  • 9,739
  • 6
  • 40
  • 58
  • Thanks Godwin, but for entity reference this aproach didn´t work. I dpm() the form and the field type is: ['field_account']['und']['#type'] = 'fieldset'. I don´t have access to the widget in the $form object. – John Jul 26 '13 at 17:34