You are here

ds_test.module in Display Suite 8.2

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) {
  $term = entity_load('taxonomy_term', $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 = array();

  // Register a single field to test that
  // extra fields in the hidden region are really hidden.
  $extra['node']['article']['display']['heavy_field'] = array(
    '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'] = array(
    '#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'] = array(
        '#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, $context) {
  $entity = $context['entity'];
  if (isset($entity->title->value) && $entity->title->value === 'Alter me!') {
    $layout_render_array['left'][] = array(
      '#markup' => 'cool!',
      '#weight' => 20,
    );
  }
}