I developed a module that create, with field api, a custom field in order to be used in content types. It's similar to link module (https://drupal.org/project/link), because I create a custom field to be used in content types.

I'm developing now a module that have a custom form, that I built with hook_form. I wanna know how do I reference in my form, the field I had created in other module?

Do I have to use some hook or drupal function? I dont know what to do.

Simplifing, How do I insert my custom field (created in other module) in my custom form?

Thanks!

I'm using Drupal 7.

Comments

nevets’s picture

The form API uses form elements defined with hook_element_info(). This is different that fields defined using the field API.

Jaypan’s picture

The form API uses form elements defined with hook_element_info(). This is different that fields defined using the field API.

The Field API also uses form elements defined with hook_element_info().

Jaypan’s picture

You cannot use Field API elements in your form. However, you can create a form then tie into the Field API in the submission. Why don't you tell us more about what you are trying to do, as you haven't provided enough information yet to be able to explain. Why do you want to use a Field in your form? Please give a longer more detailed explanation about your overall goals etc.

gaja_daran’s picture

Following code to add existing entity in the custom form.
If the entity field is field_collection_item then we can use this one.

function mymodule_something_form($form, &$form_state) {
  $attach_form = array();
  $field_collection = entity_create('field_collection_item', array('field_name' => 'field_collection_name'));
  field_attach_form('field_collection_item', $field_collection, $attach_form, $form_state, NULL, array('field_name' => 'field_collection_field_name'));
  $form['new_field'] = $attach_form['field_collection_field_name'];
}

If the entity field is user form

function mymodule_something_form($form, &$form_state) {
  global $user;
  $user_obj = user_load($user->uid);
  $attach_form = array();
  field_attach_form('user', $user_obj, $attach_form, $form_state, NULL, array('field_name' => 'field_name'));
  $form['new_field'] = $attach_form['field_name'];
}

If the field is certain content type

function mymodule_something_form($form, &$form_state) {
  $attach_form = array();
  $node = new stdClass();
  $node->type = 'article'; //content type
  field_attach_form('node', $node, $attach_form, $form_state, NULL, array('field_name' => 'field_name'));
  $form['new_field'] = $attach_form['field_name'];
}
bora-89’s picture

gaja_daran You are genius! Thank you a lot! That worked!

dx007’s picture

Work for me ! Last...How can I save the values in database?

Cheers.