a7b63b544b1ce2cb4e52709c546dbb7005125777
[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   /**
30    * The blazy manager service.
31    *
32    * @var \Drupal\blazy\BlazyFormatterManager
33    */
34   protected $blazyManager;
35
36   /**
37    * Constructs a BlazyFormatter object.
38    */
39   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, BlazyFormatterManager $blazy_manager) {
40     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
41     $this->blazyManager = $blazy_manager;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48     return new static(
49       $plugin_id,
50       $plugin_definition,
51       $configuration['field_definition'],
52       $configuration['settings'],
53       $configuration['label'],
54       $configuration['view_mode'],
55       $configuration['third_party_settings'],
56       $container->get('blazy.formatter.manager')
57     );
58   }
59
60   /**
61    * Returns the blazy manager.
62    */
63   public function blazyManager() {
64     return $this->blazyManager;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function defaultSettings() {
71     return BlazyDefault::imageSettings() + BlazyDefault::gridSettings();
72   }
73
74   /**
75    * Builds the settings.
76    */
77   public function buildSettings() {
78     $settings              = $this->getSettings();
79     $settings['plugin_id'] = $this->getPluginId();
80
81     return $settings;
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function settingsForm(array $form, FormStateInterface $form_state) {
88     $element    = [];
89     $definition = $this->getScopedFormElements();
90
91     $definition['_views'] = isset($form['field_api_classes']);
92     if (!empty($definition['_views'])) {
93       $view = $form_state->get('view');
94       // Disables problematic options for GridStack plugin since GridStack
95       // will not work with Responsive image, and has its own breakpoints.
96       if ($view->getExecutable()->getStyle()->getPluginId() == 'gridstack') {
97         $definition['breakpoints'] = $definition['ratio'] = FALSE;
98         $definition['responsive_image'] = FALSE;
99         $definition['no_ratio'] = TRUE;
100       }
101     }
102
103     $this->admin()->buildSettingsForm($element, $definition);
104
105     return $element;
106   }
107
108   /**
109    * Defines the scope for the form elements.
110    */
111   public function getScopedFormElements() {
112     $field       = $this->fieldDefinition;
113     $entity_type = $field->getTargetEntityTypeId();
114     $target_type = $this->getFieldSetting('target_type');
115     $multiple    = $field->getFieldStorageDefinition()->isMultiple();
116
117     return [
118       'background'        => TRUE,
119       'box_captions'      => TRUE,
120       'breakpoints'       => BlazyDefault::getConstantBreakpoints(),
121       'captions'          => ['title' => $this->t('Title'), 'alt' => $this->t('Alt')],
122       'current_view_mode' => $this->viewMode,
123       'entity_type'       => $entity_type,
124       'field_name'        => $field->getName(),
125       'field_type'        => $field->getType(),
126       'grid_form'         => $multiple,
127       'image_style_form'  => TRUE,
128       'media_switch_form' => TRUE,
129       'namespace'         => 'blazy',
130       'plugin_id'         => $this->getPluginId(),
131       'settings'          => $this->getSettings(),
132       'style'             => $multiple,
133       'target_type'       => $target_type,
134       'thumbnail_style'   => TRUE,
135     ];
136   }
137
138   /**
139    * Overrides parent::needsEntityLoad().
140    *
141    * One step back to have both image and file ER plugins extend this, because
142    * EntityReferenceItem::isDisplayed() doesn't exist, except for ImageItem
143    * which is always TRUE anyway for type image and file ER.
144    */
145   protected function needsEntityLoad(EntityReferenceItem $item) {
146     return !$item->hasNewEntity();
147   }
148
149   /**
150    * {@inheritdoc}
151    *
152    * A clone of Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase so
153    * to have one base class to extend for both image and file ER formatters.
154    */
155   protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
156     // Add the default image if the type is image.
157     if ($items->isEmpty() && $this->fieldDefinition->getType() === 'image') {
158       $default_image = $this->getFieldSetting('default_image');
159       // If we are dealing with a configurable field, look in both
160       // instance-level and field-level settings.
161       if (empty($default_image['uuid']) && $this->fieldDefinition instanceof FieldConfigInterface) {
162         $default_image = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('default_image');
163       }
164       if (!empty($default_image['uuid']) && $file = \Drupal::entityManager()->loadEntityByUuid('file', $default_image['uuid'])) {
165         // Clone the FieldItemList into a runtime-only object for the formatter,
166         // so that the fallback image can be rendered without affecting the
167         // field values in the entity being rendered.
168         $items = clone $items;
169         $items->setValue(array(
170           'target_id' => $file->id(),
171           'alt' => $default_image['alt'],
172           'title' => $default_image['title'],
173           'width' => $default_image['width'],
174           'height' => $default_image['height'],
175           'entity' => $file,
176           '_loaded' => TRUE,
177           '_is_default' => TRUE,
178         ));
179         $file->_referringItem = $items[0];
180       }
181     }
182
183     return parent::getEntitiesToView($items, $langcode);
184   }
185
186 }