Get the renderable array for a single field instance
I'm building an interface to simplify the user experience during content creation. Users select options in three <select> fields, then on submit, they are forwarded to the correct content type add node form for the options selected. Some of the values from the original form will be be pre-filled in the new node add form.
To create this new form, I need fields from the content types and user profile. Given a content type, field name, and field instance, this is the code you need to get the Form API renderable array for the field.
/* * Gets the renderable array for the specified field. Call from a form function. * * @param $entity * The type of data. Often 'node' or 'user'. * @param $bundle * The entity group type. For nodes, this is the content type. * @param $field_name * The field name of the field to render * @param $form * The form to place the field in * @param $form_state * The form state * */ function {site_customization_module}_get_form_field($entity, $bundle, $field_name, &$form, &$form_state) { // field_default_form() needs this. $form['#parents'] = ''; $field = field_info_field($field_name); $ct_instances = field_info_instances($entity, $bundle); $instance = $ct_instances[$field_name]; //See field_ui_default_value_widget() for original use. $items = $instance['default_value']; $addition = field_default_form(NULL, NULL, $field, $instance, LANGUAGE_NONE, $items, $form, $form_state); $form += $addition; }
Add new comment