0

I am trying to embed the edit form for a flag on my node (the full page view mode).

After some struggle, I finally figured out how to get the flagging form. But once I have the form, how do I actually get it to display on the page?

function MYMODULE_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if ($node->getType() == 'MY_CONTENT_TYPE') {
    $flag = \Drupal::service('flag')->getFlagById('MY_FLAG_ID');
    $flagging_entity = \Drupal::service('flag')->getFlagging($flag, $node);
    $form = \Drupal::service('entity.manager')
      ->getFormObject('flagging', 'edit')
      ->setEntity($flagging_entity);
    $form_obtained = \Drupal::formBuilder()->getForm($form);
    // How do I actually show the form on the node?

I looked at this question on rendering forms (which says use the formBuilder service, which I am already using). I assume I need to put the form in a render array but the documentation is not clear about how to do that.

  • Have you had a look at the Inline Entity Form module yet? – leymannx Jul 8 at 11:03
  • 1
    @leymannx That module doesn't support flag out of the box (only handles commerce stuff + entityreference), so I am stuck doing it in code either way. – Patrick Kenny Jul 8 at 12:37
4

\Drupal::formBuilder()->getForm() already returns a render array, see the FormBuilder::getForm() documentation.

To render the form, you just need to pass the results of getForm() to your $build array, then render it in your template.

In hook_ENTITY_TYPE_view():

function MYMODULE_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  // ...
  $build['my_flagging_form'] = $form_obtained;
}

Then in your twig template:

{{ content.my_flagging_form }}

Or, if you're already rendering content in its entirety, you may not need to update your twig template at all.

As @4k4 noted in the comments, you can also add the appropriate meta data in hook_entity_extra_field_info to make your flagging form addition configurable (reorderable, etc) on your node's Manage Display screen. See Creating pseudo-fields in Drupal 8 for an example.

| improve this answer | |
  • 3
    When using hook_ENTITY_TYPE_view you don't need to print it in the twig template, it ends up in {{ content }} of the entity template automatically and you can provide the meta data to make it configurable in hook_entity_extra_field_info – 4k4 Jul 8 at 11:21
  • Beautiful. Thank you @4k4, I didn't realize it was that easy to do that with hook_entity_extra_field_info(). (And thanks for pointing out my incorrect twig example :) ) – sonfd Jul 8 at 11:36
  • I wouldn't say it is incorrect, it works for twig templates in general, but then you would add the variable in a preprocess hook. – 4k4 Jul 8 at 11:43
0

Thanks to @sonfd and @4k4 for the answer and comment, I was able to fix it with the following code:

/**
 * Implements hook_node_view() for MYMODULE.
 */
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);
    }
  }
}

/**
 * Implements hook_entity_extra_field_info() for MYMODULE.
 *
 * Https://www.webomelette.com/creating-pseudo-fields-drupal-8
 */
function MYMODULE_entity_extra_field_info() {
  $extra = [];

  $extra['node'][NodeType::load('MY_CONTENT_TYPE')->Id()]['display']['my_flagging_form'] = [
    'label' => t('My flagging edit form'),
    'description' => t('This shows the my flagging edit form.'),
    'weight' => 100,
    'visible' => TRUE,
  ];

  return $extra;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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