drupal 8
drupal-flag
twig

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

Add new comment

Other Articles

Getting permission error when installing CiviCRM under Drupal with Xampp

If you are getting permission error while installing CiviCRM on xampp then, its is default CiviCRM user permission error,

How to get your MailChimp LIST ID

Mailchimp is marketing automation platform and is provide email for marketing your product or business whatever you like, today its most trusted service providing all over the globe.

Recursively delete all files of a specific extension

Usually we all use "rm" command to delete or remove the files and folder on linux, but if we want to delete the specific type of content/files then we can use "find" command to find and "delete" co

send Variable to Js File From PHP in drupal Custom Module

If we need to use the php variable in our js file we can use Core Drupal.settings Functionality :  example:

Allow Less Secure App Yahoo Mail

hey,  If you want to read email form script you need to allow less secure app as same as Gmail,  Just Go to the link, after login and allow less secure app at the bottom.

Wordpress Revolution Slider error after upgrade to php 7

A recently got the error below after updating wordpress website to php 7, error causing the website blank and when the debug mode on we got this error, caused by revolution slider.

Reindex the Array keys

Sometimes we need to slice the array for certain values using program a code and at that time key of array is also unset. If you faced the same then you can use these options

How you can get Mailchimp API KEY

By Follow steps below, you can find the api key or create the api key at mailchimp and use it to your website to connected the site with mailchimp to collect the subscribe in fastest way.

Enable Drupal 8 Error Reporting

To Enable Drupal 8 Error Reporting, Put This code at top of sites/default/settings.php.

Get average number with PHP

<?php //put all integer in array $array = array(9,18,9,5,6,45); //Calculate the average. $average = array_sum($array) / count($array);

Jquery prevent enter to submit form

If you want to prevent the form submit using jquery, then this page helps you to doing this. Code:

Menu template in drupal 8 for specific customization

Drupal 8 gives the flexibility to make menu template and design the menu with our need, to do this --

Facebook, Twitter and LinkedIn post share checker

Social media apps like Facebook, twitter linked allow your website and apps to share on there pages to reach with more peoples.

Slice array Elements using Keys PHP

Many Helpful Function are available to exact the specific set of keys of an array $sliced-arr = array_slice($new_tags, 0, 6);

Search word or file Using SSH(Putty)

Fastest and efficient way is to use grep command For search in a file :grep "wordtofind" config.php

Fix Bug Phpmyadmin [sql.lib.php] + Php7.2 + Ubuntu 16.04

I have faced this error recently , look like this    Warning in ./libraries/sql.lib.php#601

Drupal 8 Opcache Problem with xampp

;Put this code in php.ini (C:\xampp\php\php.ini) zend_extension=php_opcache.dll [opcache] ; Determines if Zend OPCache is enabled opcache.enable=1 ; Determines if Zend OPCach

How to override different version of Jquery in Drupal 8 using theme

Drupal 8 use its own jquery version by default and if you want to use the other version of jQuery instead of default than this tutorials steps is for you, sometimes we need different version to oth

Drupal Country code default array

include_once DRUPAL_ROOT . '/includes/locale.inc'; country_get_list();  

Important Mysql Commands that will we used with ssh

Commands Access monitor: mysql -u [username] -p; (will prompt for password) Show all databases: show databases;

How to Install Civicrm into Drupal 7

There is a direct guide available on CiviCRM Website, Here we follow faster steps to do the same.

Drupal 8 add custom menu classes to using menu.html.twig

In Drupal 8 Twig templates give extra power to drupal to add dynamic content in twig style, In this tutorial we can see how we can add the custom menu classes to menu ul li using menu.html.twig.

How to render block in Drupal 8 theme template

Similar to Drupal 7, Drupal 8 also gives the customization using theme template, If you want to place a block using programmatically you have following options : 

Facebook post customisation posted using web app using Facebook Open Graph

Facebook is Providing autoposting functionality using website application and provides to display you website content on your facebook page.

Recent Posts

[SOLVED] PHP executable not found. Install PHP 7 and add it to your PATH or set the php.executablePath setting

If you get the same error like below in VScode editor then you are the right place to 

Redirect Old Domain to New Domain via .htaccess

Usually we develop the site on developement or subdomain and when we finally mirgate the to new domain then some of thing will remain or page redirect happens in  sometimes , so In this case .htacc

Jquery prevent enter to submit form

If you want to prevent the form submit using jquery, then this page helps you to doing this.

Code:

Play Pause HTML5 Video using Jquery

HTML5 video playing on any web page is very easy, in this tutorial we can show you how to add video on html and make it play and pause without using html controls.

Import and Export Drupal 8 Views, Or Import and Export any configuration Drupal 8

Import and Export configuration is the basic protocol to staging the website or production website deployment, In Drupal 7, we need the extra modules to import and export or we can use features mod

How to override different version of Jquery in Drupal 8 using theme

Drupal 8 use its own jquery version by default and if you want to use the other version of jQuery instead of default than this tutorials steps is for you, sometimes we need different version to oth

Subscribe to Mailchimp programmatically via api with PHP

In this tutorial we can see how to subscribe to the mailchimp subscribe list using php programmatically via api,

For this you need a mailchimp account, api key and list id 

How you can get Mailchimp API KEY

By Follow steps below, you can find the api key or create the api key at mailchimp and use it to your website to connected the site with mailchimp to collect the subscribe in fastest way.

How to get your MailChimp LIST ID

Mailchimp is marketing automation platform and is provide email for marketing your product or business whatever you like, today its most trusted service providing all over the globe.

Drupal 8 add custom menu classes to using menu.html.twig

In Drupal 8 Twig templates give extra power to drupal to add dynamic content in twig style, In this tutorial we can see how we can add the custom menu classes to menu ul li using menu.html.twig.

How to create the directories and sub directories in one command linux

Many times we are want to create multiple directories and sub-directories under it and usually we do cd and mkdir one by one, it is very time consuming and it sometimes slow down our workflow.