I'm writing a module with a custom form. I would to add in this a form a field from an existent content type, with values.

For example: I've a content type "article" wth a node reference field "field_ref".

In my custom form I want to get this "field_ref" field, with values. Is it possible?

I think that I could get the entire form and then get only the field but I don't like this solution :)

2 Answers 2


You can build a custom autocomplete text field to filter node reference results. Check this link http://timonweb.com/how-create-ajax-autocomplete-textfield-drupal-7 to build autocomplete text field.

You just need to change the query in above implementation.


Assuming your field is a list type, you can grab the allowed values from the field using the field_info_field() function:

$info = field_info_field('field_states');
$options = $info['settings']['allowed_values'];

$form['work_state'] = array(
  '#title' => t('Work State'),
  '#type' => 'select',
  '#options' => $options

I got hint from this link of Stack Overflow

Check this link again from stackoverflow. https://stackoverflow.com/questions/11225158/drupal-7-programmatic-field-definition

This allows you to create node reference field

  • This is ok, but I need to create a node_reference field, populated from values of a node (that I get with a view). I'm now searching api to create node_reference field
    – arrubiu
    Commented Sep 21, 2012 at 10:40
  • check my edit of answer Commented Sep 21, 2012 at 10:47
  • getting a list is ok, also add values from a view or a query. But I need to create in the form a node_reference field
    – arrubiu
    Commented Sep 24, 2012 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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