1

I'm using Drupal 8 with Flag 4.0-beta1.

On my flag, I have several fields. I want users to be able to edit these fields after making the flag.

How can I make a link to edit the flagging?

I tried various things in Views (making a view of flaggings; making a view of nodes with flaggings as a relationship), but I can't find a way to get an edit link to the flagging (to edit the fields on the flag itself).

NOTE: This question is about flaggings (an instance of a flag). Here's the same question about flags.

6
  • I can't find a way to get an edit link edit link to the node that was flagged?
    – No Sssweat
    Jun 18 '20 at 15:32
  • @NoSssweat On the flagging; I've updated the question. Jun 18 '20 at 15:35
  • I'm lost, so... then, what's a "flagging" ?
    – No Sssweat
    Jun 18 '20 at 15:39
  • @NoSssweat A flagging is apparently the terminology that is being used to refer to an instance of a flag. So the flag is like the content type and the flagging is the node. (Could be wrong on this; it's my second attempt to ask this question.) Jun 18 '20 at 15:41
  • 1
    @NoSssweat No, I want to edit the flag entity that has fields on it. Say the node is a page, and the page is tagged with a flag called "favorites", and the favorites flag has a field called "score" that goes from 1 to 10. I want a link to edit the score field on the flag entity. Jun 18 '20 at 15:49
2

Digging into the flag module a little, I can see this entry in flag.routing.yml that appears to be for your Flagging edit screen.

flag.field_entry.edit:
  path: '/flag/details/edit/{flag}/{entity_id}'
  defaults:
    _controller: '\Drupal\flag\Controller\FieldEntryFormController::edit'
    _title_callback: '\Drupal\flag\Controller\FieldEntryFormController::editTitle'
  requirements:
    _flag_access: 'entity:flag{flag}'

You can build out the link to the flagging manually, e.g.

<a href='/flag/details/edit/FLAG_ID/FLAGGING_ID'>Edit My Flagging</a>

Or if you're working with twig, e.g. rewriting a view's field, make sure you're also including the flag's id and flagging's id in the view then rewrite doing something like:

<a href='/flag/details/edit/{{ FLAG_ID }}/{{ FLAGGING_ID }}'>{{ "Edit Flagging"|t }}</a>

Or you can use PHP to build the url directly from the route. It's recommended to use one of the following methods if you're working in PHP.

To get just the url text or object in PHP, use Url::fromRoute():

use \Drupal\Core\Url;

$my_flagging_url_object = Url::fromRoute('flag.field_entry.edit', [
  'flag' => FLAG_ID,
  'entity_id' => FLAGGING_ID,
]);
// $my_flagging_url_string returns something like "/flag/details/edit/123/1234"
$my_flagging_url_string = $my_flagging_url_object->toString();

To build the whole link in PHP, use Link::createFromRoute():

use \Drupal\core\Link;

$my_flagging_edit_link = Link::createFromRoute('MY FLAGGING EDIT TEXT', 'flag.field_entry.edit', [
  'flag' => FLAG_ID,
  'entity_id' => FLAGGING_ID,
]);
0

It turns out that you can also use the "Unflag" link, and if the flag has fields, it will open an edit form... if you set the Link type to Field Entry Form on the flag admin page.

I didn't even think to try this because "unflag" and "edit" sound like completely different operations to me.

Thankfully, on the flag's edit page, you can change the text from "unflag" to "edit", which makes it much more clear.

Then you can add the "flag/unflag" link field in Views to give an easy way to get to the edit form.

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.