On a flagged node, instead of showing the flag/unflag link, I want to show the flag edit form.

Here's my setup.

Basic page content type can be flagged with the flag Bookmark. Bookmark is a per-user flag that has an int list field Rating, which has values from 1 to 5. On the flag admin page (/admin/structure/flags/manage/bookmark), Link type is set to Field Entry Form.

What happens now:

  1. Go to a basic page that has been flagged.
  2. Click the "Unflag" link for bookmark.
  3. The Field Entry Form pops up and lets me edit the value of Rating or delete the flag.

What I want to happen:

  1. Go to a basic page that has been flagged.
  2. The Field Entry Form is embedded in the page and the user can adjust the rating as he/she likes.

I don't know how to build the form. By digging through the flag module code, I managed to find the class that generates the form in FieldEntry.php:

 * Class FieldEntry
 * @ActionLinkType(
 *  id = "field_entry",
 *  label = @Translation("Field Entry Form"),
 *  description = "Redirects the user to a field entry form."
 * )
class FieldEntry extends FormEntryTypeBase {

But I don't know how to embed this in a node.

I looked at Inline Entity Form but that only supports Commerce modules and Entity Reference fields out of the box.

I also tried the Twig Tweak module and tried to embed an entity edit form using a view that returns the flagging ID like this:

{{ drupal_entity_form('flagging', id__value) }} 

However, this gives me an error:

Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("The "flagging" entity type did not specify a "" form class.") in "__string_template__87f4d3d6f14aa36d2fb8ce031f07b73d8b577189b4e5f3c535809f672ffb6af8" at line 1. in Twig\Template->displayWithErrorHandling() (line 469 of /app/vendor/twig/twig/src/Template.php).

I can embed the entity itself with Twig Tweak just fine (but that is not the goal; the goal is to embed the entity edit form):

{{ drupal_entity('flagging', id__value) }} 
  • Are you trying to add this on the node's edit screen? or on the front end?
    – sonfd
    Commented Jun 26, 2020 at 11:42
  • On the front end. On the flagged node, I want to show the edit form (on the node with full view mode). Commented Jun 26, 2020 at 11:46
  • If you check git.drupalcode.org/project/flag/-/blob/8.x-4.x/src/Plugin/… method getAsFlagLink there is no possibility to render forms, only links for flag/unflag entities. I would suggest only alter the behaviour of this method and add your own render logic Commented Jun 29, 2020 at 13:47
  • 1
    Changing the behavior of getAsFlagLink(), especially when the returned value is changed, is a bad idea, IMO. At that point, since the OP would write code, he could also use his own class, even if that could mean writing more code. At least, the compatibility with existing modules would be kept.
    – apaderno
    Commented Jun 29, 2020 at 20:54

1 Answer 1


Here's the code I ended up using:

 * 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')
      $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;

For more details, see this question.

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.