Completed
Push — 8.x-1.x ( c248cd...375743 )
by Janez
02:14
created

ViewModeDeriver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 5
lcom 1
cbo 0

3 Methods

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