Last active
November 13, 2022 23:40
-
-
Save Greg-Boggs/b38198d241d461464ef227b0121b0766 to your computer and use it in GitHub Desktop.
layout switcher for Drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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