Skip to content

Instantly share code, notes, and snippets.

@Greg-Boggs
Last active November 13, 2022 23:40
Show Gist options
  • Save Greg-Boggs/b38198d241d461464ef227b0121b0766 to your computer and use it in GitHub Desktop.
Save Greg-Boggs/b38198d241d461464ef227b0121b0766 to your computer and use it in GitHub Desktop.
layout switcher for Drupal
<?php
function lib_layouts_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'landing_page' && $view_mode == 'full') {
if ($entity->hasField('field_layout')) {
$field = $entity->get('field_layout');
if (!$field->isEmpty()) {
$layout = $entity->get('field_layout')->getString();
$available_modes = \Drupal::service('entity_display.repository')->getViewModes('node');
if (array_key_exists($layout, $available_modes)) {
$view_mode = $layout;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment