diff --git a/flag.module b/flag.module index 8dd6181..d67cc8a 100644 --- a/flag.module +++ b/flag.module @@ -342,82 +342,82 @@ function flag_node_links_alter(array &$links, NodeInterface $entity, array &$con * * Handles the 'show_in_links' and 'show_as_field' flag options. */ -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, - ]; - } -} +// 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, +// ]; +// } +// } /** * Implements hook_entity_build_defaults_alter(). */ -function flag_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { - /** @var \Drupal\flag\FlagService $flag_service */ - $flag_service = \Drupal::service('flag'); - - // Get all possible flags for this entity type. - $flags = $flag_service->getAllFlags($entity->getEntityTypeId(), $entity->bundle()); - - $no_cache = FALSE; - foreach ($flags as $flag) { - $flag_type_plugin = $flag->getFlagTypePlugin(); - - // Make sure we're dealing with an entity flag type. - if (!$flag_type_plugin instanceof EntityFlagType) { - continue; - } - - // Only add max-age to entity render array if contextual links flag - // display is enabled. - if (!$flag_type_plugin->showContextualLink()) { - continue; - } - - $no_cache = TRUE; - } - - if ($no_cache) { - $build['#cache']['max-age'] = 0; - } - - return $build; -} +// function flag_entity_build_defaults_alter(array &$build, EntityInterface $entity, $view_mode = 'full', $langcode = NULL) { +// /** @var \Drupal\flag\FlagService $flag_service */ +// $flag_service = \Drupal::service('flag'); +// +// // Get all possible flags for this entity type. +// $flags = $flag_service->getAllFlags($entity->getEntityTypeId(), $entity->bundle()); +// +// $no_cache = FALSE; +// foreach ($flags as $flag) { +// $flag_type_plugin = $flag->getFlagTypePlugin(); +// +// // Make sure we're dealing with an entity flag type. +// if (!$flag_type_plugin instanceof EntityFlagType) { +// continue; +// } +// +// // Only add max-age to entity render array if contextual links flag +// // display is enabled. +// if (!$flag_type_plugin->showContextualLink()) { +// continue; +// } +// +// $no_cache = TRUE; +// } +// +// if ($no_cache) { +// $build['#cache']['max-age'] = 0; +// } +// +// return $build; +// } /** * Implements hook_entity_view_alter(). @@ -425,41 +425,41 @@ function flag_entity_build_defaults_alter(array &$build, EntityInterface $entity * Alters node contextual links placeholder id to contain flag metadata, so that * contextual links cache considers flags granularity. */ -function flag_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) { - $entity_type = $entity->getEntityTypeId(); - if (isset($build['#contextual_links'][$entity_type])) { - /** @var \Drupal\flag\FlagService $flag_service */ - $flag_service = \Drupal::service('flag'); - - // Get all possible flags for this entity type. - $flags = $flag_service->getAllFlags($entity_type, $entity->bundle()); - - foreach ($flags as $flag) { - $flag_type_plugin = $flag->getFlagTypePlugin(); - - // Make sure we're dealing with an entity flag type. - if (!$flag_type_plugin instanceof EntityFlagType) { - continue; - } - - // Only apply metadata to contextual links if plugin is enabled - if (!$flag_type_plugin->showContextualLink()) { - continue; - } - - $action = 'flag'; - if ($flag->isFlagged($entity)) { - $action = 'unflag'; - } - - $flag_keys[] = $flag->id() . '-' . $action; - } - - if (!empty($flag_keys)) { - $build['#contextual_links'][$entity_type]['metadata']['flag_keys'] = implode(',', $flag_keys); - } - } -} +// function flag_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) { +// $entity_type = $entity->getEntityTypeId(); +// if (isset($build['#contextual_links'][$entity_type])) { +// /** @var \Drupal\flag\FlagService $flag_service */ +// $flag_service = \Drupal::service('flag'); +// +// // Get all possible flags for this entity type. +// $flags = $flag_service->getAllFlags($entity_type, $entity->bundle()); +// +// foreach ($flags as $flag) { +// $flag_type_plugin = $flag->getFlagTypePlugin(); +// +// // Make sure we're dealing with an entity flag type. +// if (!$flag_type_plugin instanceof EntityFlagType) { +// continue; +// } +// +// // Only apply metadata to contextual links if plugin is enabled +// if (!$flag_type_plugin->showContextualLink()) { +// continue; +// } +// +// $action = 'flag'; +// if ($flag->isFlagged($entity)) { +// $action = 'unflag'; +// } +// +// $flag_keys[] = $flag->id() . '-' . $action; +// } +// +// if (!empty($flag_keys)) { +// $build['#contextual_links'][$entity_type]['metadata']['flag_keys'] = implode(',', $flag_keys); +// } +// } +// } /** * Implements hook_contextual_links_alter().