When displaying my entity edit form with \Drupal::formBuilder()->getForm($form);
(for a flagging attached to a node) in a View that is set to show Content with the Default display, I noticed the <form>
tag itself is missing:
<form class="flagging-myflag-edit-form flagging-form" data-drupal-selector="flagging-myflag-edit-form-iq8dn9lwifq" action="/flag/details/edit/MYFLAG/52" method="post" id="myform-myflag-edit-form--iq8dn9lWIfQ" accept-charset="UTF-8">
Everything between the form tags is printed properly.
</form>
The <form>
tag appears on the node page correctly, but it does not appear in the view.
So somehow, Views is filtering out the <form>
tag, but I need to keep this tag.
This is the code I am using:
function MYMODULE_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
if ($node->getType() == 'MY_CONTENT_YTPE') {
if ($display->getComponent('my_flagging_form')) {
$flag = \Drupal::service('flag')->getFlagById('MY_FLAG');
$flagging_entity = \Drupal::service('flag')->getFlagging($flag, $node);
$form = \Drupal::service('entity.manager')
->getFormObject('flagging', 'edit')
->setEntity($flagging_entity);
$build['my_flagging_form'] = \Drupal::formBuilder()->getForm($form);
}
}
}
According to twig debugging, the custom form is being output through core/modules/system/templates/form.html.twig
, but there's no form tag when output by Views (the tag does appear on the node page).
This is form.html.twig
:
<form{{ attributes }}>
{{ children }}
</form>
EDIT: I tried to render it based on the comment of @Ales:
$form = \Drupal::service('entity.manager')
->getFormObject('flagging', 'edit')
->setEntity($flagging_entity);
$form_to_render = \Drupal::formBuilder()->getForm($form);
$renderer = \Drupal::service('renderer');
$build['remember_form']['#markup'] = $renderer->render($form_to_render);
This gives me the same output as my original code; the form is there, but the <form>
tag is not...
EDIT 2: I also tried using template_node_preprocess()
instead of hook_node_view()
:
function MYTHEME_preprocess_node__MY_CONTENT_TYPE(&$variables) {
$node = $variables['node'];
$flag = \Drupal::service('flag')->getFlagById('MY_FLAG');
$flagging_entity = \Drupal::service('flag')->getFlagging($flag, $node);
$form = \Drupal::service('entity.manager')
->getFormObject('flagging', 'edit')
->setEntity($flagging_entity);
$form_to_render = \Drupal::formBuilder()->getForm($form);
$variables['MY_FLAG_form'] = $form_to_render;
}
But when I rendered this variable in my theme template, I got the same result-- the <form>
tag appears on the node page, but not in the view that shows Content with mode full content.
<form>
tag that way, either. See my updated question.{{ myform }}