Completed
Pull Request — 8.x-3.x (#495)
by Sebastian
07:40
created

ImageStyleId::buildValues()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Enums\Images;
4
5
use Drupal\Core\Entity\EntityTypeManagerInterface;
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7
use Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase;
8
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * @GraphQLEnum(
13
 *   id = "image_style_id",
14
 *   name = "ImageStyleId",
15
 *   provider = "image"
16
 * )
17
 */
18
class ImageStyleId extends EnumPluginBase 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...
19
20
  /**
21
   * The entity type manager service.
22
   *
23
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
24
   */
25
  protected $entityTypeManager;
26
27
  /**
28
   * ImageStyleId constructor.
29
   *
30
   * @param array $configuration
31
   *   The plugin configuration array.
32
   * @param string $pluginId
33
   *   The plugin id.
34
   * @param array $pluginDefinition
35
   *   The plugin definition array.
36
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
37
   *   The entity type manager.
38
   */
39
  public function __construct(array $configuration, $pluginId, $pluginDefinition, EntityTypeManagerInterface $entityTypeManager) {
40
    parent::__construct($configuration, $pluginId, $pluginDefinition);
41
    $this->entityTypeManager = $entityTypeManager;
42
  }
43
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
48
    return new static(
49
      $configuration,
50
      $pluginId,
51
      $pluginDefinition,
52
      $container->get('entity_type.manager')
53
    );
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function buildValues(PluggableSchemaBuilderInterface $schemaBuilder) {
60
    $items = [];
61
62
    $storage = $this->entityTypeManager->getStorage('image_style');
63
    foreach ($storage->loadMultiple() as $imageStyle) {
64
      $items[$imageStyle->id()] = [
65
        'value' => $imageStyle->id(),
66
        'name' => $imageStyle->id(),
67
        'description' => $imageStyle->label(),
68
      ];
69
    }
70
71
    return $items;
72
  }
73
74
}
75