Created
February 16, 2017 17:33
-
-
Save krisdigital/4d74ff7da9b9152de6ff32eb29f58850 to your computer and use it in GitHub Desktop.
Drupal 8 - change view mode programmatically and consider cache
This file contains hidden or 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 | |
// If you do not add the cache key, you will get | |
// interesting results in your live environment | |
// where caching is enabled | |
function yourtheme_preprocess_node(&$vars) { | |
... | |
$textBl = $vars['content']['field_page']; | |
$delta = 0; | |
while(!empty($textBl[$delta])) { | |
$par = $textBl[$delta]['#paragraph']; | |
if ($delta == 0) { | |
$viewmode = 'special'; | |
$textBl[$delta]['#view_mode'] = $viewmode; | |
$textBl[$delta]['#cache']['keys'][] = $viewmode; | |
} | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment