0

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 the flag edit page?

I found this comment on using VBO for Drupal 7, but on Drupal 8, VBO doesn't have the "modify entity" option for flaggings suggested in that post.

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.

EDIT: I got confused by the terminology and mixed up flags (the "type") and flaggings (the instance of the type) are different. I'm leaving this question as is, and I created a new question for flaggings.

1 Answer 1

3

I'm not sure about any view field for an edit link, but you can link to the flag's edit screen, like most entities, in a number of ways.

Looking at the module's flag.routing.yml file, we see the edit route defined like:

entity.flag.edit_form:
  path: '/admin/structure/flags/manage/{flag}'
  defaults:
    _entity_form: flag.edit
    _title: 'Edit Flag'
  requirements:
    _entity_access: 'flag.update'

The flag argument here is for the flag's id.

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

<a href='/admin/structure/flags/manage/MY_FLAG_ID'>Edit MY FLAG</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 in the view then rewrite doing something like:

<a href='/admin/structure/flags/manage/{{ flag_id }}'>{{ "Edit"|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_flag_url_object = Url::fromRoute('entity.flag.edit_form', ['flag' => MY_FLAG_ID]);
// $my_flag_url_string returns something like "/admin/structure/flags/manage/123"
$my_flag_url_string = $my_flag_url_object->toString();

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

use \Drupal\core\Link;

$my_flag_edit_link = Link::createFromRoute('MY FLAG EDIT TEXT', 'entity.flag.edit_form', ['flag' => MY_FLAG_ID]);
1
  • Thanks, this is a great answer. Unfortunately, I realized I was confused about the terms and looking for a link to the flagging edit page, not the flag edit page. However, since you answered the question so extensively, I modified this question to match your answer and opened a new question here: drupal.stackexchange.com/questions/294643/… Jun 18, 2020 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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