You are here

function shrinktheweb_entity_view_alter in ShrinkTheWeb 8

Implements hook_entity_view_alter().

File

./shrinktheweb.module, line 351

Code

function shrinktheweb_entity_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
  $shrinktheweb_fields = \Drupal::database()
    ->select('shrinktheweb_fields', 't')
    ->distinct()
    ->fields('t', array(
    'stw_entity_bundle',
  ))
    ->condition('stw_entity_bundle', $entity
    ->bundle())
    ->execute()
    ->fetchCol();
  if (!empty($shrinktheweb_fields)) {
    $cache_days = \Drupal::config('shrinktheweb.settings')
      ->get('shrinktheweb_cache_days');
    switch (TRUE) {
      case $cache_days == '-1':
        $build['#cache']['max-age'] = 0;
        break;
      case $cache_days == '0':
        $build['#cache']['max-age'] = \Drupal\Core\Cache\Cache::PERMANENT;
        break;
      case $cache_days > '0':
        $build['#cache']['max-age'] = $cache_days * 86400;
        break;
    }
  }
}