5

I want to use existing content type field in my own custom form. Like In content type, we can use existing field.

6

In Drupal 7 you can use field_attach_form


function hook_form($form, &$form_state) {
  $form['fname'] = array(
    '#type' => 'textfield',
    '#title' => t('first name'),
  );
  $form['lname'] = array(
    '#type' => 'textfield',
    '#title' => t('last name'),
  );
  //attaching existing content type fields
  $node = new stdClass();
  $node->type = 'article'; //content type
  field_attach_form('node', $node, $form, $form_state); //it will attach all the fields
  unset($form['body']); //unset other fields like this.
  //..
  return $form;
}
| improve this answer | |
  • How I can attach a user field to a custom form? I will display that field using views in an existing view. Here is the statuses.form.inc file. – Umair May 26 '17 at 10:10
2

You should consider using field_attach_form function.

An example use of this in user.module is as follows.

field_attach_form('user', $form['#user'], $form, $form_state, $langcode);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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