Interim commit.
[yaffs-website] / web / modules / contrib / entity_embed / tests / modules / entity_embed_test / entity_embed_test.module
1 <?php
2
3 /**
4  * @file
5  * Helper module for the Entity Embed tests.
6  */
7
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Session\AccountInterface;
11
12 /**
13  * Implements hook_theme().
14  */
15 function entity_embed_test_theme($existing, $type, $theme, $path) {
16   $items['entity_embed_twig_test'] = array(
17     'template' => 'entity_embed_twig_test',
18     'variables' => array(
19       'entity_type' => '',
20       'id' => '',
21       'display_plugin' => 'default',
22       'display_settings' => array(),
23     ),
24   );
25   return $items;
26 }
27
28 /**
29  * Implements hook_entity_embed_display_plugins_alter().
30  */
31 function entity_embed_test_entity_embed_display_plugins_alter(&$info) {
32   // Allow tests to enable or disable this hook.
33   if (!\Drupal::state()->get('entity_embed_test_entity_embed_display_plugins_alter', FALSE)) {
34     return;
35   }
36
37   // Prefix each plugin name with 'testing_hook:'.
38   $new_info = array();
39   foreach ($info as $key => $value) {
40     $new_key = "testing_hook:" . $key;
41     $new_info[$new_key] = $info[$key];
42     unset($info[$key]);
43   }
44   $info = $new_info;
45 }
46
47 /**
48  * Implements hook_entity_embed_context_alter().
49  */
50 function entity_embed_test_entity_embed_context_alter(array &$context, EntityInterface $entity) {
51   // Allow tests to enable or disable this hook.
52   if (!\Drupal::state()->get('entity_embed_test_entity_embed_context_alter', FALSE)) {
53     return;
54   }
55
56   // Force to use 'Label' plugin.
57   $context['data-entity-embed-display'] = 'entity_reference:entity_reference_label';
58   $context['data-entity-embed-display-settings'] = array('link' => 1);
59
60   // Set title of the entity.
61   $entity->setTitle("Title set by hook_entity_embed_context_alter");
62 }
63
64 /**
65  * Implements hook_entity_embed_alter().
66  */
67 function entity_embed_test_entity_embed_alter(array &$build, EntityInterface $entity, array $context) {
68   // Allow tests to enable or disable this hook.
69   if (!\Drupal::state()->get('entity_embed_test_entity_embed_alter', FALSE)) {
70     return;
71   }
72
73   // Set title of the 'node' entity.
74   $entity->setTitle("Title set by hook_entity_embed_alter");
75 }
76
77 /**
78  * Implements hook_entity_access().
79  */
80 function entity_embed_test_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
81   if ($entity->label() == 'Embed Test Node') {
82     return AccessResult::neutral()->addCacheTags(['foo:' . $entity->id()]);
83   }
84 }