You are here

function opigno_certificate_entity_view_mode_alter in Opigno certificate 8

Same name and namespace in other branches
  1. 3.x opigno_certificate.module \opigno_certificate_entity_view_mode_alter()

Implements hook_entity_view_mode_alter().

This hook was saved from module view_mode_selector.

File

./opigno_certificate.module, line 30
Contains opigno_certificate.module.

Code

function opigno_certificate_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  if ($view_mode !== 'view_mode_selector.') {
    return;
  }

  // Set view mode to 'Default', this is for entities without
  // a view mode selector field.
  $view_mode = 'default';

  // Find the first view mode selector field and change the view mode.
  $fields = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions($entity
    ->getEntityTypeId(), $entity
    ->bundle());
  foreach ($fields as $field_name => $field) {
    if ($field
      ->getType() == 'view_mode_selector' && ($field_value = $entity->{$field_name}->value)) {
      $view_mode = $field_value;
      break;
    }
  }
  return $view_mode;
}