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 {
30 * The blazy manager service.
32 * @var \Drupal\blazy\BlazyFormatterManager
34 protected $blazyManager;
37 * Constructs a BlazyFormatter object.
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;
47 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
61 * Returns the blazy manager.
63 public function blazyManager() {
64 return $this->blazyManager;
70 public static function defaultSettings() {
71 return BlazyDefault::imageSettings() + BlazyDefault::gridSettings();
75 * Builds the settings.
77 public function buildSettings() {
78 $settings = $this->getSettings();
79 $settings['plugin_id'] = $this->getPluginId();
87 public function settingsForm(array $form, FormStateInterface $form_state) {
89 $definition = $this->getScopedFormElements();
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;
103 $this->admin()->buildSettingsForm($element, $definition);
109 * Defines the scope for the form elements.
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();
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,
139 * Overrides parent::needsEntityLoad().
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.
145 protected function needsEntityLoad(EntityReferenceItem $item) {
146 return !$item->hasNewEntity();
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.
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');
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'],
177 '_is_default' => TRUE,
179 $file->_referringItem = $items[0];
183 return parent::getEntitiesToView($items, $langcode);