You are here

public function ContentEntityViewModesExtractor::getViewModeMinimalHtml in Acquia Content Hub 8

Renders all the view modes that are configured to be rendered.

In this method we also switch to an user with role defined in the module entity configuration.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $object: The Content Entity Object.

string $view_mode: The request view mode identifier.

Return value

array The render array for the complete page, as minimal as possible.

See also

https://www.drupal.org/node/218104

File

src/Normalizer/ContentEntityViewModesExtractor.php, line 327

Class

ContentEntityViewModesExtractor
Extracts the rendered view modes from a given ContentEntity Object.

Namespace

Drupal\acquia_contenthub\Normalizer

Code

public function getViewModeMinimalHtml(ContentEntityInterface $object, $view_mode) {

  // Creating a fake user account to give as context to the normalization.
  $account = $this->renderUser;

  // Checking for entity access permission to this particular account.
  $entity_access = $object
    ->access('view', $account, TRUE);
  if (!$entity_access
    ->isAllowed()) {
    return [];
  }
  $this->accountSwitcher
    ->switchTo($this->renderUser);

  // Render View Mode.
  $entity_type_id = $object
    ->getEntityTypeId();
  $use_block_content_view_builder = $this->config
    ->get('acquia_contenthub.entity_config')
    ->get('use_block_content_view_builder');
  if ($entity_type_id === 'block_content' && $use_block_content_view_builder) {
    $build = $this
      ->getBlockMinimalBuildArray($object, $view_mode);
  }
  else {
    $build = $this->entityTypeManager
      ->getViewBuilder($entity_type_id)
      ->view($object, $view_mode);
  }

  // Add our cacheable dependency. If this config changes, clear the render
  // cache.
  $contenthub_entity_config_id = $this
    ->getContentHubEntityTypeConfigEntity($entity_type_id);
  $this->renderer
    ->addCacheableDependency($build, $contenthub_entity_config_id);

  // Add a role cacheable dependency.
  $render_role_id = $this->config
    ->get('acquia_contenthub.entity_config')
    ->get('user_role');
  $render_role = $this->entityTypeManager
    ->getStorage('user_role')
    ->load($render_role_id);
  $this->renderer
    ->addCacheableDependency($build, $render_role);

  // Restore user account.
  $this->accountSwitcher
    ->switchBack();
  return $build;
}