Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / src / Plugin / Field / FieldFormatter / BlazyFileFormatterBase.php
1 <?php
2
3 namespace Drupal\blazy\Plugin\Field\FieldFormatter;
4
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;
15
16 /**
17  * Base class for blazy/slick image, and file ER formatters.
18  *
19  * Defines one base class to extend for both image and file ER formatters as
20  * otherwise different base classes: ImageFormatterBase or FileFormatterBase.
21  *
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.
26  */
27 abstract class BlazyFileFormatterBase extends FileFormatterBase implements ContainerFactoryPluginInterface {
28
29   use BlazyFormatterBaseTrait;
30
31   /**
32    * Constructs a BlazyFormatter object.
33    */
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;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
43     return new static(
44       $plugin_id,
45       $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')
52     );
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   public static function defaultSettings() {
59     return BlazyDefault::imageSettings() + BlazyDefault::gridSettings();
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function settingsForm(array $form, FormStateInterface $form_state) {
66     $element    = [];
67     $definition = $this->getScopedFormElements();
68
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;
78       }
79     }
80
81     $this->admin()->buildSettingsForm($element, $definition);
82
83     return $element;
84   }
85
86   /**
87    * Defines the scope for the form elements.
88    */
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();
94
95     return [
96       'background'        => TRUE,
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,
113     ];
114   }
115
116   /**
117    * Overrides parent::needsEntityLoad().
118    *
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.
122    */
123   protected function needsEntityLoad(EntityReferenceItem $item) {
124     return !$item->hasNewEntity();
125   }
126
127   /**
128    * {@inheritdoc}
129    *
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.
132    */
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');
141       }
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;
147         $items->setValue([
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'],
153           'entity' => $file,
154           '_loaded' => TRUE,
155           '_is_default' => TRUE,
156         ]);
157         $file->_referringItem = $items[0];
158       }
159     }
160
161     return parent::getEntitiesToView($items, $langcode);
162   }
163
164 }