Skip to main content

Flag

Managing Flag Development In D8

Submitted by system on
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: $flag_service->flag($flag, $entity, $account); // $account is optional

Drupal Custom Flag Operations

Submitted by amitsedai on
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