This article is refer to generate the flag link programmatically and render in drupal 8 twig template, before in drupal 7 its was quiet easy to render or generate flag link in just a single line of code. see below
drupal 7 :
<?php flag_create_link('bookmarks', $node->nid); ?>
But In drupal 8 twig is comes in picture so to do in that we have to one extra step, see below
Drupal 8:
1) If you want to load flag link for node then use "hook_preprocess_node(&$variables)" in your "yourthemename.theme" file like this -
function yourthemename_preprocess_node(&$variables) {
$node = $variables['elements']['#node'];
$flag_link_service = \Drupal::service('flag.link_builder');
$flag_link = $flag_link_service->build($node->getEntityTypeId(), $node->id(), 'bookmark'); // instead of bookmark you can use your flag machine name
$variables['article_flag'] = $flag_link;
}
2) Now render it your node template like this -
<div class="flag-link">{{article_flag}}</div>
And that it.
You can similarly to for user, entities, paragraphs etc.
NOTE - please not use double links on single page, only one should be used.
Need any help feel free to comment
Thanks
Please Comment your thoughts and feedback below and add something if you found good in anywhere to help others
Hit a like Button If you like the Post.
Many Thanks