function opigno_certificate_entity_view_mode_alter in Opigno certificate 3.x
Same name and namespace in other branches
- 8 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 37 - 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;
}