EDIT4: As it turns out, the view was using exposed filters, which causes the view to return html for a form. HTML isn't allowed to have multiple forms, so the browser might remove the second one, confusingly. The only solution I could imagine was putting the view somewhere else, or putting it inside of an iframe.
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:
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?
EDIT 2: Thank you 4k4, but now something even stranger is happening, now that I'm not doing the render part. When I click the Add another story button, it now disappears and prints the message "An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (2 MB) that this server supports. But, obviously I'm not uploading any file. The ajax request is still not sending the form data that was missing when I did it the other way.
EDIT 3: I can see that after I've embedded my view, there is a tag, which would normally have been at the end of the form, after the save button. It hasn't been moved, it's just that there's an extra one. When I embed the view, there are two forms, one inside of the other. It looks like Chrome's Inspector had removed the second tag automatically from the html, but left the extra closing tag. I can see it when I go to view page source, though.