You are here

function opigno_certificate_entity_view_mode_info_alter in Opigno certificate 8

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

Implements hook_entity_view_mode_info_alter().

This hook was saved from module view_mode_selector.

File

./opigno_certificate.module, line 76
Contains opigno_certificate.module.

Code

function opigno_certificate_entity_view_mode_info_alter(&$view_modes) {

  // Load all view mode selector fields.
  $storage = \Drupal::entityTypeManager()
    ->getStorage('field_config');
  $field_ids = \Drupal::entityQuery('field_config')
    ->condition('field_type', 'view_mode_selector')
    ->execute();
  $fields = $storage
    ->loadMultiple($field_ids);

  // Create the extra fields which represent the field deltas.

  /** @var \Drupal\field\Entity\FieldConfig $field */
  foreach ($fields as $field) {
    $entity_type = $field
      ->getTargetEntityTypeId();

    // Load the view mode selector view mode and add it to the list.
    $storage = \Drupal::entityTypeManager()
      ->getStorage('entity_view_mode');
    $id = $entity_type . '.view_mode_selector';
    $view_mode = $storage
      ->load($id);
    if (!$view_mode) {

      // Create the view mode if it does not exist.
      $view_mode = $storage
        ->create([
        'label' => t('View mode selector'),
        'id' => $id,
        'status' => true,
        // must be true; see: drupal.org/node/2322503
        'targetEntityType' => $entity_type,
      ], 'entity_view_mode');
      $view_mode
        ->save();
    }
    $view_modes[$entity_type]['view_mode_selector'] = $view_mode
      ->toArray();
  }
}