ezcontent-8.x-dev/modules/ezcontent_node/modules/ezcontent_landing_page/ezcontent_landing_page.module
modules/ezcontent_node/modules/ezcontent_landing_page/ezcontent_landing_page.module
<?php
/**
* @file
* Contains ezcontent_landing_page hook implementations.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_entity_view_alter().
*/
function ezcontent_landing_page_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
// Check if entity is a non-new node in either full or AMP view mode.
/** @var \Drupal\node\NodeInterface $entity */
if ($entity instanceof NodeInterface && $entity->bundle() === 'landing_page' && !$entity->isNew() && $build['#view_mode'] == 'amp') {
// Get a list of available view modes for the current entity.
$view_modes = \Drupal::service('entity_display.repository')
->getViewModeOptionsByBundle('node', 'landing_page');
// Double-check that the AMP view mode is enabled for this node type.
if (isset($view_modes['amp'])) {
foreach ($build['#node']->layout_builder__layout->getValue() as $key => $value) {
foreach ($value['section']->getComponents() as $component) {
$configuration = [];
$configuration = $component->get('configuration');
$configuration['view_mode'] = 'amp';
$component->setConfiguration($configuration);
}
$build['_layout_builder'][$key] = $value['section']->toRenderArray();
}
}
}
}
