Example #1
0
 /**
  * Provides entity-specific defaults to the build process.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity for which the defaults should be provided.
  * @param string $view_mode
  *   The view mode that should be used.
  * @param string $langcode
  *   For which language the entity should be prepared, defaults to
  *   the current content language.
  *
  * @return array
  */
 protected function getBuildDefaults(EntityInterface $entity, $view_mode, $langcode)
 {
     // Allow modules to change the view mode.
     $context = array('langcode' => $langcode);
     $this->moduleHandler()->alter('entity_view_mode', $view_mode, $entity, $context);
     $build = array('#theme' => $this->entityTypeId, "#{$this->entityTypeId}" => $entity, '#view_mode' => $view_mode, '#langcode' => $langcode, '#cache' => array('tags' => Cache::mergeTags($this->getCacheTags(), $entity->getCacheTags()), 'contexts' => $entity->getCacheContexts(), 'max-age' => $entity->getCacheMaxAge()));
     // Cache the rendered output if permitted by the view mode and global entity
     // type configuration.
     if ($this->isViewModeCacheable($view_mode) && !$entity->isNew() && $entity->isDefaultRevision() && $this->entityType->isRenderCacheable()) {
         $build['#cache'] += array('keys' => array('entity_view', $this->entityTypeId, $entity->id(), $view_mode), 'bin' => $this->cacheBin);
         if ($entity instanceof TranslatableInterface && count($entity->getTranslationLanguages()) > 1) {
             $build['#cache']['keys'][] = $langcode;
         }
     }
     return $build;
 }