Skip to content

Instantly share code, notes, and snippets.

@krisdigital
Created February 16, 2017 17:33
Show Gist options
  • Save krisdigital/4d74ff7da9b9152de6ff32eb29f58850 to your computer and use it in GitHub Desktop.
Save krisdigital/4d74ff7da9b9152de6ff32eb29f58850 to your computer and use it in GitHub Desktop.
Drupal 8 - change view mode programmatically and consider cache
<?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