I have a custom form which shows the title field and a view embedded into the form. Both form and the view are updated via Ajax. I am trying to add the body field, but it is not showing up.

This is my form

 * Provide a form.
function MYMODULE_form($form, &$form_state) {

  $form['wrapper'] = [
    '#type'       => 'container',
    '#attributes' => [
      'id' => 'mywrapper',

  $form['wrapper']['node_form'] = [
    '#type'  => 'fieldset',
    '#title' => t('Add article'),

  $form['wrapper']['node_form']['node_title'] = [
    '#type'     => 'textfield',
    '#title'    => t('Article title'),
    '#required' => TRUE,

  $form['wrapper']['submit'] = [
    '#type'   => 'submit',
    '#value'  => t('Save'),
    '#submit' => ['MYMODULE_form_submit'],
    '#ajax'   => [
      'callback' => 'MYMODULE_MYCALLBACK',
      'wrapper'  => 'mywrapper',

  $form['wrapper']['view_display'] = [
    // view machine name && display/block name
    '#markup' => views_embed_view('nodes', 'block'),

  return $form;

 * Do some validation.
function MYMODULE_form_validate($form, &$form_state) {
  // Better do some validation.
  // Here or via #element_validate.
  // @see https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#element_validate

 * Form submit. Node creation.
function MYMODULE_form_submit($form, &$form_state) {

  global $user;
  $node = new stdClass();

  $node->type     = 'article';
  $node->title    = $form_state['values']['node_title'];
  $node->language = LANGUAGE_NONE;
  $node->uid      = $user->uid;


  // Empty out title field on rebuild.
  $form_state['input']['node_title'] = '';

  // Rebuild form.
  $form_state['rebuild'] = TRUE;

 * Ajax callback to return the whole wrapper.
function MYMODULE_MYCALLBACK($form, &$form_state) {

  return $form['wrapper'];
  • 1
    Where is the body field in your code? Jun 25, 2017 at 14:32
  • @Neograph734 I want to add it. I tried it similar to how title field is added, but no result
    – Umair
    Jun 25, 2017 at 15:41
  • 1
    When asking a question, please provide all relevant information. Your question is currently lacking what you have tried and is missing a back reference to a previous question. If you do not provide all information, the chance of a good answer are smaller. Also be aware that we are not a code-writing service or a tutorial site. Please share your own effort, rather than copying code in a question and stating that something for which you have not provided any code 'does not show up'. Jun 25, 2017 at 15:54
  • On one hand there's the Form API to provide fields in a form. As in your example code. See api.drupal.org/api/drupal/… And on the other hand there's the Fields API api.drupal.org/api/drupal/modules%21field%21field.module/group/… to provide fields to entities. The form submit then is the place where these both get connected together. Form values can programmatically be transformed into field values attached to a node. And that's your task now.
    – leymannx
    Jun 25, 2017 at 15:58
  • Add fields to your form. And then take care of the submit.
    – leymannx
    Jun 25, 2017 at 17:07

1 Answer 1


The field system in Drupal is a bit complex, but I will try to explain.

It starts with the field type. This is the same #type you are referring to in your form, or the type a field instance will refer to. The field types are defined in hook_field_info(). This defined the options a field can have, the default form render mode (widget) and the default display render mode (formatter).

The attachment of such a field to an entity is performed by a field instance. A specific implementation of a specific field for a specific entity, which can have an overridden widget or formatter and its own settings.

Since you are creating a custom form, you have no association with the entity form. This also means that you do not have access to the field instances that one would normally find on an entity.

You can try to:

  • Load the node_form to obtain a part of the original entity form.
  • Attach the original field instances to your form with field_attach_form('node', $node, $form, $form_state, entity_language('node', $node));
  • create a custom form element of type text_with_summary to replicate the default body field.
  • Thank you for your answer but I did not understand much of it. I did not create this form
    – Umair
    Jun 25, 2017 at 15:44
  • I got it from here Why did I get -2 points on this question?
    – Umair
    Jun 25, 2017 at 15:46
  • 1
    Then please take the effort to read into the subject and see what each of the functions does. Jun 25, 2017 at 15:56

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.