function flag_entity_view in Flag 8.4
Same name and namespace in other branches
- 7.3 flag.module \flag_entity_view()
- 7.2 flag.module \flag_entity_view()
Implements hook_entity_view().
Handles the 'show_in_links' and 'show_as_field' flag options.
File
- ./
flag.module, line 345 - The Flag module.
Code
function flag_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Don't show on previews.
if ($entity
->isNew()) {
return;
}
$build['#cache']['contexts'][] = 'user.permissions';
if (empty($build['#cache']['tags'])) {
$build['#cache']['tags'] = [];
}
// 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) {
$build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], $flag
->getCacheTags());
// Do not display the flag if disabled.
if (!$flag
->status()) {
continue;
}
$flag_type_plugin = $flag
->getFlagTypePlugin();
// Only add cache key if flag link is displayed.
if (!$flag_type_plugin
->showAsField() || !$display
->getComponent('flag_' . $flag
->id())) {
continue;
}
$build['flag_' . $flag
->id()] = [
'#lazy_builder' => [
'flag.link_builder:build',
[
$entity
->getEntityTypeId(),
$entity
->id(),
$flag
->id(),
],
],
'#create_placeholder' => TRUE,
];
}
}