You are here

entity_embed_test.module in Entity Embed 8

Helper module for the Entity Embed tests.

File

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

/**
 * @file
 * Helper module for the Entity Embed tests.
 */
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Implements hook_theme().
 */
function entity_embed_test_theme($existing, $type, $theme, $path) {
  $items['entity_embed_twig_test'] = [
    'template' => 'entity_embed_twig_test',
    'variables' => [
      'entity_type' => '',
      'id' => '',
      'display_plugin' => 'default',
      'display_settings' => [],
    ],
  ];
  return $items;
}

/**
 * Implements hook_entity_embed_display_plugins_alter().
 */
function entity_embed_test_entity_embed_display_plugins_alter(&$info) {

  // Allow tests to enable or disable this hook.
  if (!\Drupal::state()
    ->get('entity_embed_test_entity_embed_display_plugins_alter', FALSE)) {
    return;
  }

  // Prefix each plugin name with 'testing_hook:'.
  $new_info = [];
  foreach ($info as $key => $value) {
    $new_key = "testing_hook:" . $key;
    $new_info[$new_key] = $info[$key];
    unset($info[$key]);
  }
  $info = $new_info;
}

/**
 * Implements hook_entity_embed_context_alter().
 */
function entity_embed_test_entity_embed_context_alter(array &$context, EntityInterface $entity) {

  // Allow tests to enable or disable this hook.
  if (!\Drupal::state()
    ->get('entity_embed_test_entity_embed_context_alter', FALSE)) {
    return;
  }

  // Force to use 'Label' plugin.
  $context['data-entity-embed-display'] = 'entity_reference:entity_reference_label';
  $context['data-entity-embed-display-settings'] = [
    'link' => 1,
  ];

  // Set title of the entity.
  $entity
    ->setTitle("Title set by hook_entity_embed_context_alter");
}

/**
 * Implements hook_entity_embed_alter().
 */
function entity_embed_test_entity_embed_alter(array &$build, EntityInterface $entity, array $context) {

  // Allow tests to enable or disable this hook.
  if (!\Drupal::state()
    ->get('entity_embed_test_entity_embed_alter', FALSE)) {
    return;
  }

  // Adding classes is as simple as appending to an array.
  $build['#attributes']['class'][] = 'test-class-added-in-alter-hook';

  // Set title of the 'node' entity.
  $entity
    ->setTitle("Title set by hook_entity_embed_alter");
}

/**
 * Implements hook_entity_access().
 */
function entity_embed_test_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($entity
    ->label() == 'Embed Test Node') {
    return AccessResult::neutral()
      ->addCacheTags([
      'foo:' . $entity
        ->id(),
    ]);
  }
}

/**
 * Implements hook_entity_view_alter().
 */
function entity_embed_test_entity_view_alter(&$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  $build['#attributes']['data-entity-embed-test-view-mode'] = $display
    ->getMode();

  // @see \Drupal\Tests\entity_embed\FunctionalJavascript\MediaImageTest::testPreviewUsesDefaultTheme()
  $build['#attributes']['data-entity-embed-test-active-theme'] = \Drupal::theme()
    ->getActiveTheme()
    ->getName();
}