ViewModeDeriver::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_embed\Plugin\Derivative;
4
5
use Drupal\Component\Plugin\Derivative\DeriverBase;
6
use Drupal\Core\Config\ConfigFactoryInterface;
7
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
8
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Provides Entity Embed Display plugin definitions for view modes.
13
 *
14
 * @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase
15
 */
16
class ViewModeDeriver extends DeriverBase implements ContainerDeriverInterface {
17
18
  /**
19
   * The entity display repository.
20
   *
21
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
22
   */
23
  protected $entityDisplayRepository;
24
25
  /**
26
   * The config factory service.
27
   *
28
   * @var \Drupal\Core\Config\ConfigFactoryInterface
29
   */
30
  protected $configFactory;
31
32
  /**
33
   * Constructs a ViewModeDeriver object.
34
   *
35
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
36
   *   The entity display repository.
37
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38
   *   A config factory for retrieving required config objects.
39
   */
40
  public function __construct(EntityDisplayRepositoryInterface $entity_display_repository, ConfigFactoryInterface $config_factory) {
41
    $this->entityDisplayRepository = $entity_display_repository;
42
    $this->configFactory = $config_factory;
43
  }
44
45
  /**
46
   * {@inheritdoc}
47
   */
48
  public static function create(ContainerInterface $container, $base_plugin_id) {
49
    return new static(
50
      $container->get('entity_display.repository'),
51
      $container->get('config.factory')
52
    );
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function getDerivativeDefinitions($base_plugin_definition) {
59
    $mode = $this->configFactory->get('entity_embed.settings')->get('rendered_entity_mode');
60
    foreach ($this->entityDisplayRepository->getAllViewModes() as $view_modes) {
61
      foreach ($view_modes as $view_mode => $definition) {
62
        $this->derivatives[$definition['id']] = $base_plugin_definition;
63
        $this->derivatives[$definition['id']]['label'] = $definition['label'];
64
        $this->derivatives[$definition['id']]['view_mode'] = $view_mode;
65
        $this->derivatives[$definition['id']]['entity_types'] = $definition['targetEntityType'];
66
        $this->derivatives[$definition['id']]['no_ui'] = $mode;
67
      }
68
    }
69
    return $this->derivatives;
70
  }
71
72
}
73