22740687edce3267cb1d4568f441847d1af2a32d
[yaffs-website] / web / modules / contrib / entity_embed / src / Plugin / Derivative / FieldFormatterDeriver.php
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;
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 }