I need to change the form displayed on a page using ajax.

I created an ajax command (having class CommandInterface). The command is called from JavaScript on the client, which in turns sets a callback command that takes the rendered form markup and injects it in a <div>. The form is displayed, but when submit is clicked, the page redirects to: http://as.local/outline/form_action_p_pvdeGsVG5zNF_XLGPTvYSKCf43t8qZYSwc.... The url is a hardcoded value that acts as a placeholder (hardcoded in the Drupal core FormBuilder class).

This issue exists on the D8 Outline module development version (code below is from that module), available at https://www.drupal.org/project/outline. If you download and install this module, and create a simple outline, and set the outline's "render as" setting to "Form", you can reproduce the issue.

Here is the client js code that calls the command:

     ...
      var url = 'outline/render-entry/' +
          type + '/' +
          id + '/' +
          render + '/' +
          mode;
      var ajax = Drupal.ajax({
        url: Drupal.url(url)
      });
      ajax.execute();

Here is the render method of the Ajax Command class:

  /**
   * {@inheritdoc}
   */
  public function render() {

    // Get the entity.
    $entity = \Drupal::entityTypeManager()
      ->getStorage($this->type)
      ->load($this->id);

    // Create render array.
    if ($this->render == Entry::RENDER_AS_DISPLAY) {
      $renderArray =
        \Drupal::entityTypeManager()
          ->getViewBuilder($this->type)
          ->view($entity, $this->mode);
    }
    elseif ($this->render == Entry::RENDER_AS_FORM) {
      $form = \Drupal::service('entity.manager')
        ->getFormObject('outline_entry', 'default')
        ->setEntity($entity);
      $renderArray = \Drupal::formBuilder()->getForm($form);
    }

    // Return the markup.
    $markup = \Drupal::service('renderer')->render($renderArray);
    return [
      'command' => 'renderEntry',
      'renderedEntry' => $markup,
    ];
  }

Here is the "renderEntry" command that is called on the client:

  Drupal.AjaxCommands.prototype.renderEntry = function (ajax, response, status) {
    var renderedEntry = response.renderedEntry;
    var entryContainer = $('.entry-container');
    entryContainer.html(renderedEntry);
    var form = entryContainer.find('form')[0];
    Drupal.attachBehaviors(form, Drupal.settings);
  };

Comments

captaindav created an issue. See original summary.

captaindav’s picture

Issue summary: View changes
captaindav’s picture

Issue summary: View changes
captaindav’s picture

Title: Injecting form with ajax command. » Injecting rendered form with an Ajax command.
cilefen’s picture

Issue summary: View changes
Issue tags: -form entity render ajax command

Hi @captaindav:

You filed this one as a support request but did you intend to mark it a bug?

P.S. It enhances readability to surround code snippets in <code> tags so I did that.

captaindav’s picture

Thanks @cilefen for fixing the code snippets!

I wasn't sure if I am doing something incorrectly, or if there is an actual bug in the core form system, so I marked it "support request". I was hoping someone else might know if it is a bug.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

cilefen’s picture

Status: Active » Closed (outdated)

There have been no comments in a few years.