1

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:

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?

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.

1 Answer 1

1

You can remove the rendering, you don't need it:

$the_view = views_embed_view('images', 'page_1');
$form['newthing'] = $the_view;

In fact, you should not render yourself in D8. And if you have the rare case you need to do this, read this first:

https://www.drupal.org/node/2513810

3
  • Thank you, but now something even stranger is happening. 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. Commented Sep 23, 2016 at 18:32
  • Better use a view block and configure the visibility in block layout, that it is displayed together with the form.
    – 4uk4
    Commented Sep 23, 2016 at 18:55
  • Okay, but I'm starting to get really curious. I can see now, actually, that after the html from the embedded view, I'm seeing that the form has been ended, with </form> which would normally be after the save button at the bottom, is way up at the top after my embedded view. It would make sense that it didn't get the form data, the button I'm clicking isn't even in the form anymore. Commented Sep 23, 2016 at 19:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.