You are here

function flag_entity_build_defaults_alter in Flag 8.4

Implements hook_entity_build_defaults_alter().

File

./flag.module, line 390
The Flag module.

Code

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;
}