Attach or embed node add form to a view I want to attach or embed a node form to a view so that when a user submits the form, the view below the form will be updated in realtime via ajax. The form will be shown above the view. My views block name is Statuses Stream.

I tried formblock module and putting the form in the header section of my view using views ui, but after submit, it was redirecting to the node created page.

Then I tried Advanced Form Block module. I created a form using AFB module. It is submitting the form with ajax, but it is not updating the view in the realtime.

I have just tried the Ajax Form Entity module. It is submitting and updating via ajax, but I cannot find a way to attach a view with it. However, in its settings, I can select a view mode. I need a little more flexibility.

How to Attach or embed node form to a view so that it submits via ajax and updates the view in realtime? I am open to any solution programmatically or through a module. Better if done programmatically because it will give more flexibility.
I'ld take it from the other side: Attach a view to a node form.

First I tried it with the built-in node form. But this turned out to be quite a hassle. Instead I built a small custom form and created new nodes programmatically. Ajax submit. View refreshed. Happy, happy.

Here comes the code for a custom module. Presuming a fresh D7 standard installation.

Info file

name = MYMODULE
description = Provides cool stuff.
core = 7.x
package = Custom
dependencies[] = views


Module file

First provide a view.

/**
 * Implements hook_views_api().
 */
function MYMODULE_views_api() {

  return [
    'api' =&gt; 3,
  ];
}

/**
 * Implements hook_views_default_views().
 */
function MYMODULE_views_default_views() {

  $view = new view();
  $view-&gt;name = 'nodes';
  $view-&gt;description = '';
  $view-&gt;tag = 'default';
  $view-&gt;base_table = 'node';
  $view-&gt;human_name = 'Nodes';
  $view-&gt;core = 7;
  $view-&gt;api_version = '3.0';
  $view-&gt;disabled = FALSE;

  /* Display: Master */
  $handler = $view-&gt;new_display('default', 'Master', 'default');
  $handler-&gt;display-&gt;display_options['title'] = 'Nodes';
  $handler-&gt;display-&gt;display_options['use_more_always'] = FALSE;
  $handler-&gt;display-&gt;display_options['access']['type'] = 'none';
  $handler-&gt;display-&gt;display_options['cache']['type'] = 'none';
  $handler-&gt;display-&gt;display_options['query']['type'] = 'views_query';
  $handler-&gt;display-&gt;display_options['exposed_form']['type'] = 'basic';
  $handler-&gt;display-&gt;display_options['pager']['type'] = 'none';
  $handler-&gt;display-&gt;display_options['pager']['options']['offset'] = '0';
  $handler-&gt;display-&gt;display_options['style_plugin'] = 'default';
  $handler-&gt;display-&gt;display_options['row_plugin'] = 'fields';
  /* Field: Content: Title */
  $handler-&gt;display-&gt;display_options['fields']['title']['id'] = 'title';
  $handler-&gt;display-&gt;display_options['fields']['title']['table'] = 'node';
  $handler-&gt;display-&gt;display_options['fields']['title']['field'] = 'title';
  $handler-&gt;display-&gt;display_options['fields']['title']['label'] = '';
  $handler-&gt;display-&gt;display_options['fields']['title']['alter']['word_boundary'] = FALSE;
  $handler-&gt;display-&gt;display_options['fields']['title']['alter']['ellipsis'] = FALSE;
  /* Sort criterion: Content: Post date */
  $handler-&gt;display-&gt;display_options['sorts']['created']['id'] = 'created';
  $handler-&gt;display-&gt;display_options['sorts']['created']['table'] = 'node';
  $handler-&gt;display-&gt;display_options['sorts']['created']['field'] = 'created';
  $handler-&gt;display-&gt;display_options['sorts']['created']['order'] = 'DESC';

  /* Display: Block */
  $handler = $view-&gt;new_display('block', 'Block', 'block');

  $views[$view-&gt;name] = $view;
  return $views;
}


Then do the form.

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

  $form['wrapper'] = [
    '#type'       =&gt; 'container',
    '#attributes' =&gt; [
      'id' =&gt; 'mywrapper',
    ],
  ];

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

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

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

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

  return $form;
}

/**
 * Do some validation.
 */
function MYMODULE_form_validate($form, &amp;$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, &amp;$form_state) {

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

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

  node_object_prepare($node);
  node_save($node);

  // 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, &amp;$form_state) {

  return $form['wrapper'];
}


I've put it on a sample page to see it in action.

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {

  $items['example/form'] = [
    'title'            =&gt; 'Example Form',
    'description'      =&gt; 'A form to mess around with.',
    'page callback'    =&gt; 'drupal_get_form',
    'page arguments'   =&gt; ['MYMODULE_form'],
    'access arguments' =&gt; ['create article content'],
  ];

  return $items;
}