You are here

ds_test.module in Display Suite 8.3

Display Suite test module.

File

tests/modules/ds_test/ds_test.module
View source
<?php

/**
 * @file
 * Display Suite test module.
 */
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;

/**
 * Implements hook_theme_suggestions_alter().
 */
function ds_test_theme_suggestions_alter(&$suggestions, $variables, $base_theme_hook) {
  if (isset($variables['content']) && is_array($variables['content']) && isset($variables['content']['#ds_configuration']) && $base_theme_hook != 'ds_entity_view') {
    if (isset($_GET['store_suggestions'])) {
      \Drupal::cache()
        ->set('ds_test_suggestions', $suggestions);
    }
  }
}

/**
 * Helper function to return the tag name basid on tid.
 */
function ds_test_get_tag_name($raw_value, $object) {
  $entity_manager = \Drupal::entityTypeManager();
  $term = $entity_manager
    ->getStorage('taxonomy_term')
    ->load($raw_value);
  return $term->name->value;
}

/**
 * Helper function to return advanced view mode.
 */
function ds_test_ds_views_row_render_entity($entity, $view_mode) {
  return 'Advanced display for id ' . $entity
    ->id();
}

/**
 * Implements hook_entity_extra_field_info().
 */
function ds_test_entity_extra_field_info() {
  $extra = [];

  // Register a single field to test that
  // extra fields in the hidden region are really hidden.
  $extra['node']['article']['display']['heavy_field'] = [
    'label' => t('Heavy extra test field'),
    'weight' => 10,
  ];
  return $extra;
}

/**
 * Implements hook_entity_view().
 */
function ds_test_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  $build['ds_extras_extra_test_field'] = [
    '#markup' => 'This is an extra field made available through "Extra fields" functionality.',
    '#weight' => 10,
  ];

  // Check whether the heavy extra field is rendered or not.
  if ($entity
    ->getEntityTypeId() == 'node' && $entity
    ->bundle() == 'article') {
    $heavy_field = $display
      ->getComponent('heavy_field');
    if ($heavy_field) {
      $build['heavy_field'] = [
        '#markup' => 'Heavy field',
        '#weight' => $heavy_field['weight'],
      ];
    }
  }
}

/**
 * Implements hook_layout_alter().
 */
function ds_test_layout_alter(&$layouts) {
  unset($layouts['ds_3col_stacked_equal_width']);
}

/**
 * Implements hook_ds_fields_info_alter().
 */
function ds_test_ds_fields_info_alter(&$definitions) {
  if (isset($definitions['test_field_2'])) {
    $definitions['test_field_2']['title'] = 'Field altered';
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ds_test_form_entity_view_display_edit_form_alter(&$form, FormStateInterface $form_state) {
  foreach (Element::children($form['fields']) as $key) {
    if (isset($form['fields'][$key]['settings_edit'])) {
      $settings = $form['fields'][$key]['settings_edit'];
      if (!empty($settings)) {
        $form['fields'][$key]['settings_edit']['#type'] = 'submit';
        $form['fields'][$key]['settings_edit']['#value'] = 'edit ' . $key;
      }
    }
  }
}

/**
 * Implements hook_ds_pre_render_alter().
 */
function ds_test_ds_pre_render_alter(&$layout_render_array, array $context, array &$vars) {

  /** @var EntityInterface $entity */
  $entity = $context['entity'];
  if (isset($entity->title->value) && $entity->title->value === 'Alter me!') {
    $layout_render_array['left'][] = [
      '#markup' => 'cool!',
      '#weight' => 20,
    ];
  }
  if ($entity
    ->getEntityTypeId() == 'user') {
    $vars['attributes']['class'][] = 'entity-label-class-' . $entity
      ->label();
  }
}

/**
 * Implements hook_install().
 */
function ds_test_install() {
  module_set_weight('ds_test', 10);
}