MenuLinkAttribute   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 1
b 0
f 0
dl 0
loc 15
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolveValues() 0 10 3
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\MenuLink;
4
5
use Drupal\Component\Utility\NestedArray;
0 ignored issues
show
Bug introduced by
The type Drupal\Component\Utility\NestedArray was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Menu\MenuLinkTreeElement;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Menu\MenuLinkTreeElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\graphql\GraphQL\Execution\ResolveContext;
8
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
9
use GraphQL\Type\Definition\ResolveInfo;
10
11
/**
12
 * Retrieve specific attributes of a menu link.
13
 *
14
 * @GraphQLField(
15
 *   id = "menu_link_attribute",
16
 *   secure = true,
17
 *   name = "attribute",
18
 *   type = "String",
19
 *   parents = {"MenuLink"},
20
 *   arguments = {
21
 *     "key" = "String!"
22
 *   }
23
 * )
24
 */
25
class MenuLinkAttribute extends FieldPluginBase {
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
31
    if ($value instanceof MenuLinkTreeElement) {
32
      $options = $value->link->getOptions();
33
34
      // Certain attributes like class can be arrays. Check for that and implode them.
35
      $attributeValue = NestedArray::getValue($options, ['attributes', $args['key']]);
36
      if (is_array($attributeValue)) {
37
        yield implode(' ', $attributeValue);
38
      } else {
39
        yield $attributeValue;
40
      }
41
    }
42
  }
43
44
}
45