public function ContentEntityViewModesExtractor::getRenderedViewModes in Acquia Content Hub 8
Renders all the view modes that are configured to be rendered.
Parameters
\Drupal\Core\Entity\ContentEntityInterface $object: The Content Entity object.
Return value
array|null The normalized array.
Overrides ContentEntityViewModesExtractorInterface::getRenderedViewModes
File
- src/
Normalizer/ ContentEntityViewModesExtractor.php, line 199
Class
- ContentEntityViewModesExtractor
- Extracts the rendered view modes from a given ContentEntity Object.
Namespace
Drupal\acquia_contenthub\NormalizerCode
public function getRenderedViewModes(ContentEntityInterface $object) {
$normalized = [];
// Exit if the class does not support normalizing to the given format or
// the entity is not yet saved in the database (does not have an ID).
if (!$this
->isChildOfSupportedClass($object) || $object
->isNew()) {
return NULL;
}
$entity_type_id = $object
->getEntityTypeId();
$entity_bundle_id = $object
->bundle();
$contenthub_entity_config_id = $this
->getContentHubEntityTypeConfigEntity($entity_type_id);
// Stop processing if 'view modes' are not configured for this entity type.
if (!$contenthub_entity_config_id || $contenthub_entity_config_id
->isEnabledViewModes($entity_bundle_id) === FALSE) {
return NULL;
}
// Obtain the list of view modes.
$configured_view_modes = $contenthub_entity_config_id
->getRenderingViewModes($entity_bundle_id);
// Normalize.
$view_modes = $this->entityDisplayRepository
->getViewModeOptionsByBundle($entity_type_id, $entity_bundle_id);
// Generate preview image URL, if possible.
$preview_image_url = $this
->getPreviewImageUrl($object);
foreach ($view_modes as $view_mode_id => $view_mode_label) {
if (!in_array($view_mode_id, $configured_view_modes)) {
continue;
}
$view_mode_label = $view_mode_label instanceof FormattableMarkup ? $view_mode_label
->render() : $view_mode_label;
// Generate our URL where the isolated rendered view mode lives.
// This is the best way to really make sure the content in Content Hub
// and the content shown to any user is 100% the same.
$url = Url::fromRoute('acquia_contenthub.content_entity_display.entity', [
'entity_type' => $object
->getEntityTypeId(),
'entity_id' => $object
->id(),
'view_mode_name' => $view_mode_id,
])
->getInternalPath();
$url = '/' . $url;
$request = Request::create($url, 'GET');
$request = $this->contentHubSubscription
->setHmacAuthorization($request, TRUE);
/** @var \Drupal\Core\Render\HtmlResponse $response */
$response = $this->kernel
->handle($request, HttpKernelInterface::SUB_REQUEST);
$normalized[$view_mode_id] = [
'id' => $view_mode_id,
'preview_image' => $preview_image_url,
'label' => $view_mode_label,
'url' => $url,
'html' => $response
->getContent(),
];
}
return $normalized;
}