#StackBounty: #8 #forms Multiple forms on the same page are causing conflicts?

Bounty: 200

I have the following code.

function mymodule_get_node_create_or_edit_form($nodeType = '', $inputNode = null, $prepopulationFields = array())
{
  if (empty($inputNode))
  {
    $values = array(
      'type' => $nodeType,
    );
    $values = array_merge($values, $prepopulationFields);
    $node = Drupal::entityTypeManager()
      ->getStorage('node')
      ->create($values);
    $form = Drupal::service('entity.form_builder')->getForm($node);
  }
  else
  {
    $form = Drupal::entityTypeManager()
      ->getFormObject('node', 'default')
      ->setEntity($inputNode);
    $form = Drupal::formBuilder()->getForm($form);
  }

  return Drupal::service('renderer')->render($form);
}

I use this function to create edit forms for a node. So I can, for example, create a form that can be used to create a form:

print mymodule_get_node_create_or_edit_form('page', null, array());

Or I can get the edit form:

$node = node_load(1);
print mymodule_get_node_create_or_edit_form($node->getType(), $node , array());

The problem is if I execute both on the same page. Eg:

print mymodule_get_node_create_or_edit_form('page', null, array());
$node = node_load(1);
print mymodule_get_node_create_or_edit_form($node->getType(), $node , array());

If I now make changes to node 1, instead of saving it, it creates a new node with the changes I made, Instead of actually editing the existing node.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.