3 namespace Drupal\blazy\Plugin\Field\FieldFormatter;
5 use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10 use Drupal\field\FieldConfigInterface;
11 use Drupal\file\Plugin\Field\FieldFormatter\FileFormatterBase;
12 use Drupal\blazy\BlazyFormatterManager;
13 use Drupal\blazy\Dejavu\BlazyDefault;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Base class for blazy/slick image, and file ER formatters.
19 * Defines one base class to extend for both image and file ER formatters as
20 * otherwise different base classes: ImageFormatterBase or FileFormatterBase.
22 * @see Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatter.
23 * @see Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFileFormatter.
24 * @see Drupal\slick\Plugin\Field\FieldFormatter\SlickImageFormatter.
25 * @see Drupal\slick\Plugin\Field\FieldFormatter\SlickFileFormatter.
27 abstract class BlazyFileFormatterBase extends FileFormatterBase implements ContainerFactoryPluginInterface {
29 use BlazyFormatterBaseTrait;
32 * Constructs a BlazyFormatter object.
34 public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, BlazyFormatterManager $blazy_manager) {
35 parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
36 $this->blazyManager = $blazy_manager;
42 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
46 $configuration['field_definition'],
47 $configuration['settings'],
48 $configuration['label'],
49 $configuration['view_mode'],
50 $configuration['third_party_settings'],
51 $container->get('blazy.formatter.manager')
58 public static function defaultSettings() {
59 return BlazyDefault::imageSettings() + BlazyDefault::gridSettings();
65 public function settingsForm(array $form, FormStateInterface $form_state) {
67 $definition = $this->getScopedFormElements();
69 $definition['_views'] = isset($form['field_api_classes']);
70 if (!empty($definition['_views'])) {
71 $view = $form_state->get('view');
72 // Disables problematic options for GridStack plugin since GridStack
73 // will not work with Responsive image, and has its own breakpoints.
74 if ($view->getExecutable()->getStyle()->getPluginId() == 'gridstack') {
75 $definition['breakpoints'] = $definition['ratio'] = FALSE;
76 $definition['responsive_image'] = FALSE;
77 $definition['no_ratio'] = TRUE;
81 $this->admin()->buildSettingsForm($element, $definition);
87 * Defines the scope for the form elements.
89 public function getScopedFormElements() {
90 $field = $this->fieldDefinition;
91 $entity_type = $field->getTargetEntityTypeId();
92 $target_type = $this->getFieldSetting('target_type');
93 $multiple = $field->getFieldStorageDefinition()->isMultiple();
97 'box_captions' => TRUE,
98 'breakpoints' => BlazyDefault::getConstantBreakpoints(),
99 'captions' => ['title' => $this->t('Title'), 'alt' => $this->t('Alt')],
100 'current_view_mode' => $this->viewMode,
101 'entity_type' => $entity_type,
102 'field_name' => $field->getName(),
103 'field_type' => $field->getType(),
104 'grid_form' => $multiple,
105 'image_style_form' => TRUE,
106 'media_switch_form' => TRUE,
107 'namespace' => 'blazy',
108 'plugin_id' => $this->getPluginId(),
109 'settings' => $this->getSettings(),
110 'style' => $multiple,
111 'target_type' => $target_type,
112 'thumbnail_style' => TRUE,
117 * Overrides parent::needsEntityLoad().
119 * One step back to have both image and file ER plugins extend this, because
120 * EntityReferenceItem::isDisplayed() doesn't exist, except for ImageItem
121 * which is always TRUE anyway for type image and file ER.
123 protected function needsEntityLoad(EntityReferenceItem $item) {
124 return !$item->hasNewEntity();
130 * A clone of Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase so
131 * to have one base class to extend for both image and file ER formatters.
133 protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
134 // Add the default image if the type is image.
135 if ($items->isEmpty() && $this->fieldDefinition->getType() === 'image') {
136 $default_image = $this->getFieldSetting('default_image');
137 // If we are dealing with a configurable field, look in both
138 // instance-level and field-level settings.
139 if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
140 $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
142 if (!empty($default_image['uuid']) && $file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $default_image['uuid'])) {
143 // Clone the FieldItemList into a runtime-only object for the formatter,
144 // so that the fallback image can be rendered without affecting the
145 // field values in the entity being rendered.
146 $items = clone $items;
148 'target_id' => $file->id(),
149 'alt' => $default_image['alt'],
150 'title' => $default_image['title'],
151 'width' => $default_image['width'],
152 'height' => $default_image['height'],
155 '_is_default' => TRUE,
157 $file->_referringItem = $items[0];
161 return parent::getEntitiesToView($items, $langcode);