FieldFormatterDeriver.php in Entity Embed 8
File
src/Plugin/Derivative/FieldFormatterDeriver.php
View source
<?php
namespace Drupal\entity_embed\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FormatterPluginManager;
class FieldFormatterDeriver extends DeriverBase implements ContainerDeriverInterface {
protected $formatterManager;
protected $configFactory;
public function __construct(FormatterPluginManager $formatter_manager, ConfigFactoryInterface $config_factory) {
$this->formatterManager = $formatter_manager;
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('plugin.manager.field.formatter'), $container
->get('config.factory'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
if (!isset($base_plugin_definition['field_type'])) {
throw new \LogicException("Undefined field_type definition in plugin {$base_plugin_definition['id']}.");
}
$no_media_image_decorator = [
'entity_reference_entity_id',
'entity_reference_label',
];
foreach ($this->formatterManager
->getOptions($base_plugin_definition['field_type']) as $formatter => $label) {
$this->derivatives[$formatter] = $base_plugin_definition;
$this->derivatives[$formatter]['label'] = $label;
if (in_array($formatter, $no_media_image_decorator, TRUE)) {
$this->derivatives[$formatter]['supports_image_alt_and_title'] = FALSE;
}
}
return $this->derivatives;
}
}