Completed
Push — 8.x-1.x ( 6cff3f...08e509 )
by Janez
02:05
created

tests/modules/entity_embed_test/entity_embed_test.module::entity_embed_test_entity_embed_context_alter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @file
5
 * Helper module for the Entity Embed tests.
6
 */
7
8
use Drupal\Core\Entity\EntityInterface;
9
10
/**
11
 * Implements hook_theme().
12
 */
13
function entity_embed_test_theme($existing, $type, $theme, $path) {
0 ignored issues
show
Unused Code introduced by
The parameter $existing is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $theme is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
  $items['entity_embed_twig_test'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$items was never initialized. Although not strictly required by PHP, it is generally a good practice to add $items = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
15
    'template' => 'entity_embed_twig_test',
16
    'variables' => array(
17
      'entity_type' => '',
18
      'id' => '',
19
      'display_plugin' => 'default',
20
      'display_settings' => array(),
21
    ),
22
  );
23
  return $items;
24
}
25
26
27
/**
28
 * Implements hook_entity_embed_display_plugins_alter().
29
 */
30
function entity_embed_test_entity_embed_display_plugins_alter(&$info) {
31
  // Allow tests to enable or disable this hook.
32
  if (!\Drupal::state()->get('entity_embed_test_entity_embed_display_plugins_alter', FALSE)) {
33
    return;
34
  }
35
36
  // Prefix each plugin name with 'testing_hook:'.
37
  $new_info = array();
38
  foreach ($info as $key => $value) {
39
    $new_key = "testing_hook:" . $key;
40
    $new_info[$new_key] = $info[$key];
41
    unset($info[$key]);
42
  }
43
  $info = $new_info;
44
}
45
46
/**
47
 * Implements hook_entity_embed_context_alter().
48
 */
49
function entity_embed_test_entity_embed_context_alter(array &$context, EntityInterface $entity) {
50
  // Allow tests to enable or disable this hook.
51
  if (!\Drupal::state()->get('entity_embed_test_entity_embed_context_alter', FALSE)) {
52
    return;
53
  }
54
55
  // Force to use 'Label' plugin.
56
  $context['data-entity-embed-display'] = 'entity_reference:entity_reference_label';
57
  $context['data-entity-embed-settings'] = array('link' => 1);
58
59
  // Set title of the entity.
60
  $entity->setTitle("Title set by hook_entity_embed_context_alter");
61
}
62
63
/**
64
 * Implements hook_entity_embed_alter().
65
 */
66
function entity_embed_test_entity_embed_alter(array &$build, EntityInterface $entity, array $context) {
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
67
  // Allow tests to enable or disable this hook.
68
  if (!\Drupal::state()->get('entity_embed_test_entity_embed_alter', FALSE)) {
69
    return;
70
  }
71
72
  // Set title of the 'node' entity.
73
  $node = $build['#node'];
74
  $node->setTitle("Title set by hook_entity_embed_alter");
75
}
76