Skip to main content
1 of 4

Embedding view in form breaks ajax

I'm putting an embedded view into an existing form with hook_form_alter, and when I do this, it's causing the Add another item button for one of the form's elements to stop working. It sends an ajax request and gets a 500 response. Here's how I'm doing it:

$the_view = \Drupal::service('renderer')->render(views_embed_view('images', 'page_1'));
$form['newthing'] = array(
  '#type' => 'markup',
  '#markup' => $the_view,
);

The interesting thing is, it's not actually the second part that causes it, it's just the render() line that causes the problem. The rendered view data doesn't actually have to be inserted into the form.

I've tried it with different views, and I legitimately can't figure out the reason why some views break my form's ajax and some don't. It doesn't seem to be exposed filters, or having "Use ajax" enabled, or having a pager.

Whatever it is, it seems to cause the Form Data to not be sent, when clicking the "Add another item" button. This information, which would normally be sent on the ajax request:

name[0][value]:
form_build_id:form-eX_LK4K7R4X0csFlHfJGIZ7auabXu7kVIa1cctPA1iE
form_token:1tBuaaaRRJ6RHJdTtHwGWpo3gieX4ukYsfkf1QFdeyw
form_id:my_form
user_id[0][target_id]:admin (1)
field_story_reference[0][target_id]:
field_story_reference[0][_weight]:0

...is missing.

EDIT: I guess I never really phrased it in the form of a question. How do I get it to not break my Add another item button? Should this be reported, or is there a known workaround?