You are here

ds_switch_view_mode.module in Display Suite 8.4

Display Suite Switch View mode.

File

modules/ds_switch_view_mode/ds_switch_view_mode.module
View source
<?php

/**
 * @file
 * Display Suite Switch View mode.
 */
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ds\Ds;

/**
 * Implements hook_entity_base_field_info().
 */
function ds_switch_view_mode_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type
    ->id() == 'node') {

    // Add the switch field as a base field.
    $fields = [];
    $fields['ds_switch'] = BaseFieldDefinition::create('string')
      ->setLabel(t('DS switch'))
      ->setName('ds_switch')
      ->setRevisionable(TRUE)
      ->setDescription(t('Holds the DS view mode of the node.'))
      ->setSetting('default_value', '')
      ->setPropertyConstraints('value', [
      'Length' => [
        'max' => 60,
      ],
    ]);
    return $fields;
  }
}

/**
 * Implements hook_entity_view_mode_alter().
 */
function ds_switch_view_mode_entity_view_mode_alter(&$view_mode, EntityInterface $entity, $context) {
  if ($entity
    ->getEntityTypeId() == 'node' && node_is_page($entity) && !empty($entity->ds_switch->value) && $view_mode == 'full') {
    $original_view_mode = $view_mode;
    $view_mode = $entity->ds_switch->value;
    \Drupal::moduleHandler()
      ->alter('ds_switch_view_mode', $view_mode, $original_view_mode, $entity);
  }
}

/**
 * Implements hook_form_BASE_FORM_ID_alter() for node_form.
 *
 * Adds the switch view mode form element.
 */
function ds_switch_view_mode_form_node_form_alter(&$form, FormStateInterface $form_state) {

  // Switch full view mode.

  /* @var Drupal\node\NodeInterface $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  if (\Drupal::currentUser()
    ->hasPermission('ds switch ' . $node
    ->bundle()) || \Drupal::currentUser()
    ->hasPermission('ds switch view mode')) {

    // Get the view modes.
    $ds_vm = \Drupal::service('entity_display.repository')
      ->getViewModes('node');
    $layouts = [];
    $options = [
      '' => t('Default'),
    ];
    foreach ($ds_vm as $key => $item) {
      $overridden = FALSE;
      $entity_display = EntityViewDisplay::load('node.' . $node
        ->bundle() . '.' . $key);
      if ($entity_display) {
        $overridden = $entity_display
          ->status();
      }
      if ($overridden) {
        $layout = Ds::getDisplay('node', $node
          ->bundle(), $key, FALSE);
        $layouts[$key] = $layout;
        $options[$key] = $item['label'];
      }
    }

    // Add default layout settings.
    $layouts[''] = Ds::getDisplay('node', $node
      ->bundle(), 'default', FALSE);

    // Only fire if we have more than 1 option.
    if (count($options) > 1) {
      if (!isset($form['ds_switch_view_mode'])) {
        $form['ds_switch_view_mode'] = [
          '#type' => 'details',
          '#title' => t('Display settings'),
          '#weight' => 100,
          '#group' => 'advanced',
        ];
      }
      $form['ds_switch_view_mode']['ds_switch'] = [
        '#type' => 'select',
        '#title' => t('View mode'),
        '#options' => $options,
        '#default_value' => $node->ds_switch->value,
        '#description' => t('Switch to a different view mode to display the full page view of this node.'),
        '#weight' => -1,
        '#ajax' => [
          'callback' => 'ds_switch_view_mode_switch_view_mode_preview_callback',
          'wrapper' => 'ds_switch_preview_wrapper',
        ],
      ];
      $form['ds_switch_view_mode']['preview'] = [
        '#type' => 'container',
        '#prefix' => '<div id="ds_switch_preview_wrapper">',
        '#suffix' => '</div>',
      ];
      $mode = $form_state
        ->getValue('ds_switch');
      if (!$mode) {
        $mode = $node
          ->get('ds_switch')->value;
      }
      $chosen_layout = $layouts[$mode];
      if (empty($chosen_layout)) {
        if (isset($layouts['full'])) {
          $chosen_layout = $layouts['full'];
        }
        else {
          $chosen_layout = $layouts[''];
        }
      }
      $layout_settings = $chosen_layout
        ->getThirdPartySettings('ds');
      $ds_layouts = Ds::getLayouts();
      $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
      if (isset($layout_settings['layout'])) {

        /** @var \Drupal\Core\Layout\LayoutDefinition $chosen_layout */
        $chosen_layout = $ds_layouts[$layout_settings['layout']['id']];
        $image = $chosen_layout
          ->getIconPath();
        if (empty($image)) {
          $image = $fallback_image;
        }
      }
      else {
        $image = $fallback_image;
      }
      $form['ds_switch_view_mode']['preview']['image'] = [
        '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
      ];
    }
  }
}

/**
 * Ajax callback for _ds_field_ui_table_layouts_preview().
 */
function ds_switch_view_mode_switch_view_mode_preview_callback($form, $form_state) {
  return $form['ds_switch_view_mode']['preview'];
}

/**
 * Implements hook_panelizer_pre_view_builder_alter().
 */
function ds_switch_view_mode_panelizer_pre_view_builder_alter(&$view_mode, EntityInterface $entity, &$langcode) {
  if ($entity
    ->getEntityTypeId() == 'node' && node_is_page($entity) && !empty($entity->ds_switch->value) && $view_mode == 'full') {
    $view_mode = $entity->ds_switch->value;
  }
}