function flag_contextual_links_alter in Flag 8.4
Implements hook_contextual_links_alter().
File
- ./
flag.module, line 467 - The Flag module.
Code
function flag_contextual_links_alter(array &$links, $group, array $route_parameters) {
// Assume that $group is one of known entity types and try to load an entity
// based on that.
$entity_type = $group;
if (isset($route_parameters[$entity_type]) && !is_null(\Drupal::entityTypeManager()
->getDefinition($entity_type, FALSE))) {
$entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($route_parameters[$entity_type]);
}
if (!isset($entity)) {
return;
}
// Get all possible flags for this entity type.
$flag_service = \Drupal::service('flag');
$flags = $flag_service
->getAllFlags($entity
->getEntityTypeID(), $entity
->bundle());
foreach ($flags as $flag) {
/** @var \Drupal\flag\FlagInterface $flag */
// Do not display the flag if disabled.
if (!$flag
->status()) {
continue;
}
/** @var \Drupal\flag\Plugin\Flag\EntityFlagType $flag_type_plugin */
$flag_type_plugin = $flag
->getFlagTypePlugin();
// Make sure we're dealing with an entity flag type.
if (!$flag_type_plugin instanceof EntityFlagType) {
continue;
}
// Skip flags for which contextual links setting is disabled.
if (!$flag_type_plugin
->showContextualLink()) {
continue;
}
$flag_link = $flag
->getLinkTypePlugin()
->getAsLink($flag, $entity);
$flag_url = $flag_link
->getUrl();
$links["flag_{$flag->id()}"] = [
'route_name' => $flag_url
->getRouteName(),
'route_parameters' => $flag_url
->getRouteParameters(),
'title' => $flag_link
->getText(),
'localized_options' => [],
];
}
}