You are here

view_mode_selector.module in View Mode Selector 8

Same filename and directory in other branches
  1. 7 view_mode_selector.module

Main file of View Mode Selector module.

File

view_mode_selector.module
View source
<?php

/**
 * @file
 * Main file of View Mode Selector module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
 * Implements hook_entity_view_mode_alter().
 */
function view_mode_selector_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;
}

/**
 * Implements hook_entity_view_mode_info_alter().
 */
function view_mode_selector_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();
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function view_mode_selector_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\field_ui\Form\EntityViewDisplayEditForm $form_object */
  $form_object = $form_state
    ->getFormObject();

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $view_display */
  $view_display = $form_object
    ->getEntity();
  if ($view_display
    ->getMode() == 'view_mode_selector') {
    \Drupal::messenger()
      ->addMessage(t('This is a placeholder view mode from the <a href="@view-mode-selector">View Mode Selector</a> module. It will be replaced with a selected view mode.', [
      '@view-mode-selector' => Url::fromUri('https://www.drupal.org/project/view_mode_selector')
        ->toUriString(),
    ]));
    \Drupal::messenger()
      ->addWarning(t('The field settings have been disabled by the <a href="@view-mode-selector">View Mode Selector</a> module.', [
      '@view-mode-selector' => Url::fromUri('https://www.drupal.org/project/view_mode_selector')
        ->toUriString(),
    ]));
    $form['fields']['#disabled'] = TRUE;
  }
}