Flag

Managing Flag Development In D8

Technologies: 

Check if an entity has been flagged:

/**@var \Drupal\flag\FlagService $flag_service */
$flag_service = \Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
return $flag->isFlagged($node);

Get all flagging users for an entity and flag:

$flagging = $flag_service->getFlaggingUsers($entity, $flag); //  $flag is optional

Flag an entity:

Drupal Custom Flag Operations

Technologies: 

Create a flag link for a user:

<?php
print flag_create_link('user_flagname', $user_uid);
?>

Get Flag of a Type and see if it is flagged for a node and not the number of times it got flagged.

<?php
$flag
= flag_get_flag('bookmarks') or die('no "bookmarks" flag defined');
if (
$flag->is_flagged($node->nid)) {
  print
"This node is bookmarked!";
}
print
"The number of people who voted for this proposal:";
print
$flag->get_count($node->nid);  //
?>

Flagging or Unflagging an item:
You use the $flag->flag() method to either flag or unflag an item. Example:

<?php