Completed
Push — 8.x-1.x ( c35fc2...6cff3f )
by Janez
02:06
created

entity_embed.api.php ➔ hook_ENTITY_TYPE_embed_context_alter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %
Metric Value
cc 4
eloc 4
nc 3
nop 2
dl 7
loc 7
rs 9.2
1
<?php
2
3
/**
4
 * @file
5
 * Hooks provided by the Entity Embed module.
6
 */
7
8
/**
9
 * @addtogroup hooks
10
 * @{
11
 */
12
13
/**
14
 * Alter the Entity Embed Display plugin definitions.
15
 *
16
 * @param array &$info
17
 *   An associative array containing the plugin definitions keyed by plugin ID.
18
 */
19
function hook_entity_embed_display_plugins_alter(array &$info) {
0 ignored issues
show
Unused Code introduced by
The parameter $info 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...
20
21
}
22
23
/**
24
 * Alter the Entity Embed Display plugin definitions for a given context.
25
 *
26
 * Usually used to remove certain Entity Embed Display plugins for specific
27
 * entities.
28
 *
29
 * @param array &$definitions
30
 *   Remove options from this list if they should not be available for the given
31
 *   context.
32
 * @param array $contexts
33
 *   The provided context, typically an entity.
34
 */
35
function hook_entity_embed_display_plugins_for_context_alter(array &$definitions, array $contexts) {
36
  // Do nothing if no entity is provided.
37
  if (!isset($contexts['entity'])) {
38
    return;
39
  }
40
  $entity = $contexts['entity'];
41
42
  // For video and audio files, limit the available options to the media player.
43 View Code Duplication
  if ($entity instanceof \Drupal\file\FileInterface && in_array($entity->bundle(), ['audio', 'video'])) {
0 ignored issues
show
Bug introduced by
The class Drupal\file\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
    $definitions = array_intersect_key($definitions, array_flip(['file:jwplayer_formatter']));
45
  }
46
47
  // For images, use the image formatter.
48 View Code Duplication
  if ($entity instanceof \Drupal\file\FileInterface && in_array($entity->bundle(), ['image'])) {
0 ignored issues
show
Bug introduced by
The class Drupal\file\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    $definitions = array_intersect_key($definitions, array_flip(['image:image']));
50
  }
51
52
  // For nodes, use the default option.
53
  if ($entity instanceof \Drupal\node\NodeInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\node\NodeInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
    $definitions = array_intersect_key($definitions, array_flip(['entity_reference:entity_reference_entity_view']));
55
  }
56
}
57
58
/**
59
 * Alter the context of an embedded entity before it is rendered.
60
 *
61
 * @param array &$context
62
 *   The context array.
63
 * @param \Drupal\Core\Entity\EntityInterface $entity
64
 *   The entity object.
65
 */
66 View Code Duplication
function hook_entity_embed_context_alter(array &$context, \Drupal\Core\Entity\EntityInterface $entity) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
  if (isset($context['overrides']) && is_array($context['overrides'])) {
68
    foreach ($context['overrides'] as $key => $value) {
69
      $entity->key = $value;
70
    }
71
  }
72
}
73
74
/**
75
 * Alter the context of an particular embedded entity type before it is rendered.
76
 *
77
 * @param array &$context
78
 *   The context array.
79
 * @param \Drupal\Core\Entity\EntityInterface $entity
80
 *   The entity object.
81
 */
82 View Code Duplication
function hook_ENTITY_TYPE_embed_context_alter(array &$context, \Drupal\Core\Entity\EntityInterface $entity) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
  if (isset($context['overrides']) && is_array($context['overrides'])) {
84
    foreach ($context['overrides'] as $key => $value) {
85
      $entity->key = $value;
86
    }
87
  }
88
}
89
90
/**
91
 * Alter the results of an embedded entity build array.
92
 *
93
 * This hook is called after the content has been assembled in a structured
94
 * array and may be used for doing processing which requires that the complete
95
 * block content structure has been built.
96
 *
97
 * @param array &$build
98
 *   A renderable array representing the embedded entity content.
99
 * @param \Drupal\Core\Entity\EntityInterface $entity
100
 *   The embedded entity object.
101
 * @param array $context
102
 *   The context array.
103
 */
104
function hook_entity_embed_alter(array &$build, \Drupal\Core\Entity\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...
105
  // Remove the contextual links.
106
  if (isset($build['#contextual_links'])) {
107
    unset($build['#contextual_links']);
108
  }
109
}
110
111
/**
112
 * Alter the results of the particular embedded entity type build array.
113
 *
114
 * @param array &$build
115
 *   A renderable array representing the embedded entity content.
116
 * @param \Drupal\Core\Entity\EntityInterface $entity
117
 *   The embedded entity object.
118
 * @param array $context
119
 *   The context array.
120
 */
121
function hook_ENTITY_TYPE_embed_alter(array &$build, \Drupal\Core\Entity\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...
122
  // Remove the contextual links.
123
  if (isset($build['#contextual_links'])) {
124
    unset($build['#contextual_links']);
125
  }
126
}
127
128
/**
129
 * @} End of "addtogroup hooks".
130
 */
131