3 namespace Drupal\entity_embed\Plugin\Derivative;
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;
12 * Provides Entity Embed Display plugin definitions for field formatters.
14 * @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase
16 class FieldFormatterDeriver extends DeriverBase implements ContainerDeriverInterface {
19 * The manager for formatter plugins.
21 * @var \Drupal\Core\Field\FormatterPluginManager.
23 protected $formatterManager;
26 * The config factory service.
28 * @var \Drupal\Core\Config\ConfigFactoryInterface
30 protected $configFactory;
33 * Constructs new FieldFormatterEntityEmbedDisplayBase.
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.
40 public function __construct(FormatterPluginManager $formatter_manager, ConfigFactoryInterface $config_factory) {
41 $this->formatterManager = $formatter_manager;
42 $this->configFactory = $config_factory;
48 public static function create(ContainerInterface $container, $base_plugin_id) {
50 $container->get('plugin.manager.field.formatter'),
51 $container->get('config.factory')
58 * @throws \LogicException
59 * Throws an exception if field type is not defined in the annotation of the
60 * Entity Embed Display plugin.
62 public function getDerivativeDefinitions($base_plugin_definition) {
63 // The field type must be defined in the annotation of the Entity Embed
65 if (!isset($base_plugin_definition['field_type'])) {
66 throw new \LogicException("Undefined field_type definition in plugin {$base_plugin_definition['id']}.");
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
76 if ($formatter == 'entity_reference_entity_view' && $mode == FALSE) {
77 $this->derivatives[$formatter]['no_ui'] = TRUE;
80 return $this->derivatives;