You are here

protected function FileEntityViewBuilder::getBuildDefaults in File Entity (fieldable files) 8.2

Provides entity-specific defaults to the build process.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which the defaults should be provided.

string $view_mode: The view mode that should be used.

Return value

array

Overrides EntityViewBuilder::getBuildDefaults

File

src/Entity/FileEntityViewBuilder.php, line 20

Class

FileEntityViewBuilder
View builder for File Entity.

Namespace

Drupal\file_entity\Entity

Code

protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
  $build = parent::getBuildDefaults($entity, $view_mode);

  // We suppress file entity cache tags, because they are almost exclusively
  // embedded in other pages, except when viewed as a standalone page. To
  // support cache invalidations on those, we pick the first cache tag from
  // the references and add that.
  // @todo Make this available as a method?
  foreach (\Drupal::service('file.usage')
    ->listUsage($entity) as $module => $module_references) {
    foreach ($module_references as $type => $ids) {
      if (\Drupal::entityTypeManager()
        ->hasDefinition($type)) {
        $build['#cache']['tags'] = Cache::mergeTags($build['#cache']['tags'], array(
          $type . ':' . key($ids),
        ));
        break 2;
      }
    }
  }
  return $build;
}