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 Jun 26 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). – Patrick Kenny Jun 26 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 – Yuriy Stenin Jun 29 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. – kiamlaluno Jun 29 at 20:54

Your Answer

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

Browse other questions tagged or ask your own question.