Completed
Push — 8.x-3.x ( b9f7cd...f28ad9 )
by Sebastian
07:53
created

DisplayModeIdDeriver::getDisplayModes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Enums;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Entity\ContentEntityTypeInterface;
7
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8
use Drupal\Core\Entity\EntityTypeManagerInterface;
9
use Drupal\Core\Entity\EntityViewModeInterface;
10
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
11
use Drupal\Core\StringTranslation\StringTranslationTrait;
12
use Drupal\graphql\Utility\StringHelper;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
class DisplayModeIdDeriver extends DeriverBase implements ContainerDeriverInterface {
16
  use StringTranslationTrait;
17
18
  /**
19
   * Entity type manager.
20
   *
21
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
22
   */
23
  protected $entityTypeManager;
24
25
  /**
26
   * {@inheritdoc}
27
   */
28
  public static function create(ContainerInterface $container, $basePluginId) {
29
    return new static($container->get('entity_type.manager'));
30
  }
31
32
  /**
33
   * DisplayModeIdDeriver constructor.
34
   *
35
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
36
   *   The entity type manager service.
37
   */
38
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
39
    $this->entityTypeManager = $entityTypeManager;
40
  }
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  public function getDerivativeDefinitions($basePluginDefinition) {
46
    foreach ($this->getDisplayModes() as $targetType => $displayModes) {
47
      $this->derivatives[$targetType] = [
48
        'name' => StringHelper::camelCase($targetType, 'display', 'mode', 'id'),
49
        'description' => $this->t("The available display modes for '@type' entities.", [
50
          '@type' => $this->entityTypeManager->getDefinition($targetType)->getLabel(),
51
        ]),
52
        'values' => array_values($displayModes),
53
      ] + $basePluginDefinition;
54
    }
55
56
    return parent::getDerivativeDefinitions($basePluginDefinition);
57
  }
58
59
  /**
60
   * Retrieves a list of entity view modes grouped by their target type.
61
   *
62
   * @return array
63
   *   The list of entity view modes grouped by the target entity type.
64
   */
65
  protected function getDisplayModes() {
66
    $storage = $this->entityTypeManager->getStorage('entity_view_mode');
67
    return array_reduce($storage->loadMultiple(), function ($carry, EntityViewModeInterface $current) {
68
      $target = $current->getTargetType();
69
      list(, $id) = explode('.', $current->id());
70
71
      $carry[$target][$id] = [
72
        'name' => $id,
73
        'value' => $id,
74
        'description' => $this->t("The '@label' display mode for '@type' entities.", [
75
          '@label' => $current->label(),
76
          '@type' => $this->entityTypeManager->getDefinition($target)->getLabel(),
77
        ]),
78
      ];
79
80
      return $carry;
81
    }, []);
82
  }
83
84
}
85