Completed
Pull Request — 8.x-3.x (#456)
by Philipp
02:11
created

RouteEntity::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 4
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Routing;
4
5
use Drupal\Core\Cache\CacheableMetadata;
6
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7
use Drupal\Core\Entity\EntityInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Language\Language;
10
use Drupal\Core\Language\LanguageManagerInterface;
11
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12
use Drupal\Core\TypedData\TranslatableInterface;
13
use Drupal\Core\Url;
14
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Youshido\GraphQL\Execution\ResolveInfo;
17
18
/**
19
 * Retrieve the current routes entity, if it is an entity route.
20
 *
21
 * TODO: Move this to `InternalUrl` (breaking change).
22
 *
23
 * @GraphQLField(
24
 *   id = "route_entity",
25
 *   secure = true,
26
 *   name = "entity",
27
 *   description = @Translation("The entity belonging to the current url."),
28
 *   parents = {"EntityCanonicalUrl"},
29
 *   type = "Entity"
30
 * )
31
 */
32
class RouteEntity extends FieldPluginBase implements ContainerFactoryPluginInterface {
0 ignored issues
show
Bug introduced by
There is one abstract method getPluginDefinition in this class; you could implement it, or declare this class as abstract.
Loading history...
33
  use DependencySerializationTrait;
34
35
  /**
36
   * The entity type manager.
37
   *
38
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39
   */
40
  protected $entityTypeManager;
41
42
  /**
43
   * The language manager.
44
   *
45
   * @var \Drupal\Core\Language\LanguageManagerInterface
46
   */
47
  protected $languageManager;
48
49
  /**
50
   * {@inheritdoc}
51
   */
52
  public function __construct(
53
    array $configuration,
54
    $pluginId,
55
    $pluginDefinition,
56
    EntityTypeManagerInterface $entityTypeManager,
57
    LanguageManagerInterface $languageManager
58
  ) {
59
    $this->entityTypeManager = $entityTypeManager;
60
    $this->languageManager = $languageManager;
61
    parent::__construct($configuration, $pluginId, $pluginDefinition);
62
  }
63
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
68
    return new static(
69
      $configuration,
70
      $pluginId,
71
      $pluginDefinition,
72
      $container->get('entity_type.manager'),
73
      $container->get('language_manager')
74
    );
75
  }
76
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public function resolveValues($value, array $args, ResolveInfo $info) {
81
    if ($value instanceof Url) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Url 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...
82
      list($prefix, $entityType, $suffix) = explode('.', $value->getRouteName());
83
      $parameters = $value->getRouteParameters();
84
85
      if (!($prefix === 'entity' && $suffix === 'canonical') || !array_key_exists($entityType, $parameters)) {
86
        return;
87
      }
88
89
      $entity = $this->entityTypeManager
90
        ->getStorage($entityType)
91
        ->load($parameters[$entityType]);
92
93
      $lang = $this->languageManager->getCurrentLanguage(Language::TYPE_CONTENT)->getId();
94
95
      if ($entity instanceof TranslatableInterface && $entity->hasTranslation($lang)) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\TypedData\TranslatableInterface 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...
96
        $translation = $entity->getTranslation($lang);
97
        if ($translation->access('view')) {
98
          yield $translation;
99
        }
100
      }
101
      else if ($entity->access('view')) {
102
        yield $entity;
103
      }
104
105
    }
106
  }
107
108
  /**
109
   * Intermediate solution. Shouldn't the entity (stored in result) have it's
110
   * cache tag? Right now thats not the case.
111
   */
112
  protected function getCacheDependencies($result, $parent, array $args) {
113
    $dependencies = parent::getCacheDependencies($result, $parent, $args);
114
    foreach ($result as $item) {
115
      if ($item instanceof EntityInterface) {
0 ignored issues
show
Bug introduced by
The class Drupal\Core\Entity\EntityInterface 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...
116
        $metadata = new CacheableMetadata();
117
        $metadata->addCacheTags(['node:' . $item->id()]);
118
        $dependencies[] = $metadata;
119
      }
120
    }
121
    return $dependencies;
122
  }
123
124
}
125