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

FieldFormatterDeriver::getDerivativeDefinitions()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 20
rs 8.8571
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\Plugin\Discovery\ContainerDeriverInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Drupal\Core\Field\FormatterPluginManager;
10
11
/**
12
 * Provides Entity Embed Display plugin definitions for field formatters.
13
 *
14
 * @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase
15
 */
16
class FieldFormatterDeriver extends DeriverBase implements ContainerDeriverInterface {
17
18
  /**
19
   * The manager for formatter plugins.
20
   *
21
   * @var \Drupal\Core\Field\FormatterPluginManager.
22
   */
23
  protected $formatterManager;
24
25
  /**
26
   * The config factory service.
27
   *
28
   * @var \Drupal\Core\Config\ConfigFactoryInterface
29
   */
30
  protected $configFactory;
31
32
  /**
33
   * Constructs new FieldFormatterEntityEmbedDisplayBase.
34
   *
35
   * @param \Drupal\Core\Field\FormatterPluginManager $formatter_manager
36
   *   The field formatter plugin manager.
37
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
38
   *   A config factory for retrieving required config objects.
39
   */
40
  public function __construct(FormatterPluginManager $formatter_manager, ConfigFactoryInterface $config_factory) {
41
    $this->formatterManager = $formatter_manager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $formatter_manager of type object<Drupal\Core\Field\FormatterPluginManager> is incompatible with the declared type object<Drupal\Core\Field\FormatterPluginManager.> of property $formatterManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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('plugin.manager.field.formatter'),
51
      $container->get('config.factory')
52
    );
53
  }
54
55
  /**
56
   * {@inheritdoc}
57
   *
58
   * @throws \LogicException
59
   *   Throws an exception if field type is not defined in the annotation of the
60
   *   Entity Embed Display plugin.
61
   */
62
  public function getDerivativeDefinitions($base_plugin_definition) {
63
    // The field type must be defined in the annotation of the Entity Embed
64
    // Display plugin.
65
    if (!isset($base_plugin_definition['field_type'])) {
66
      throw new \LogicException("Undefined field_type definition in plugin {$base_plugin_definition['id']}.");
67
    }
68
    $mode = $this->configFactory->get('entity_embed.settings')->get('rendered_entity_mode');
69
    foreach ($this->formatterManager->getOptions($base_plugin_definition['field_type']) as $formatter => $label) {
70
      $this->derivatives[$formatter] = $base_plugin_definition;
71
      $this->derivatives[$formatter]['label'] = $label;
72
      // Don't show entity_reference_entity_view in the UI if the rendered
73
      // entity mode is FALSE. In that case we show view modes from
74
      // ViewModeDeriver, entity_reference_entity_view is kept for backwards
75
      // compatibility.
76
      if ($formatter == 'entity_reference_entity_view' && $mode == FALSE) {
77
        $this->derivatives[$formatter]['no_ui'] = TRUE;
78
      }
79
    }
80
    return $this->derivatives;
81
  }
82
83
}
84