Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I need to create a custom form and add an entity reference field to it. The field uses autocomplete with a view for entity selection. The field already exists on a node but i also need it in a custom module. Is there a way to add this field in a custom module with MYMODULE_MYFORM_form()?

share|improve this question

1 Answer

If you are using the references module's node_reference field, then, in your custom form, you'll want to create a field that uses the node_reference's autocomplete, like so:

$form['referenced_field'] = array(
  '#title' => 'Referenced field title',
  '#description' => t('Referenced field description.'),
  '#type' => 'textfield',
  '#autocomplete_path' => 'node_reference/autocomplete',
  '#required' => TRUE,  // Optional depending on your circumstance.
);

See node_reference.module line 892 for more details on parameters, etc.

share|improve this answer
i am using the entity reference module and since there wasn't a replay for a couple days, i've implemented this in a different way. didn't managed to get it to work within the form. thanks anyway for your help – rugar Apr 19 at 21:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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