0

I am building a custom form using the Form API. I want to show a nodereference_select in my form. However I have some difficulties setting the default value and the list of values.

//Create array of values
$items['6039']="MT";

// add field
$field = content_fields('field_nodereference_select');
$form['#field_info']['field_nodereference_select'] = $field;
$form += content_field_form($form, $form_state, $field);
$form['field_nodereference_select']['#weight'] = 6;
$form['field_nodereference_select']['#default_value']=$items;
$form['field_nodereference_select']['#options']=$items;

When I render the form I see the widget but without any values. When I am able to render the $items array, my next step would be to executing a view (because the nodereference_select is using a view to select certain values). and fill the $items array with the values.

Any help on this one?

2
  • You will struggle with this as field components are not meant to be used in this way. Fields are intended to belong to entities, you cannot just put them into arbitrary forms. Commented Apr 24, 2013 at 8:43
  • That's not true. It is perfectly acceptable to create custom forms with any form elements that have no relation to an entity form. However you might be better doing it without content_fields(), just manually implement to form api element.
    – rooby
    Commented Apr 24, 2013 at 12:25

1 Answer 1

0

I've managed to use such a field widget in a custom form in the following manner

  $field = content_fields('<field name on a node type>', '<node type that uses this field>');
  $form['#field_info']['<field name on a node type>'] = $field;
  $form += content_field_form($form, $form_state, $field);

I don't know if it would work without a node type actually having such a field. I had a node type with that reference so I could "invoke" that.

The resulting form element does not look like a simple select element, it does not have '#options' and '#default_value' is fancy too.

1
  • Upon submission I could just do $node-><field name> = $form_state['values']['<form element name>'] I'm not sure though what that variable contained. Commented May 8, 2013 at 15:07

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.