Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / blazy / src / BlazyFormatterManager.php
1 <?php
2
3 namespace Drupal\blazy;
4
5 /**
6  * Provides common field formatter-related methods: Blazy, Slick.
7  */
8 class BlazyFormatterManager extends BlazyManager {
9
10   /**
11    * Returns the field formatter settings inherited by child elements.
12    *
13    * @param array $build
14    *   The array containing: settings, or potential optionset for extensions.
15    * @param object $items
16    *   The items to prepare settings for.
17    */
18   public function buildSettings(array &$build, $items) {
19     $settings       = &$build['settings'];
20     $count          = $items->count();
21     $field          = $items->getFieldDefinition();
22     $entity         = $items->getEntity();
23     $entity_type_id = $entity->getEntityTypeId();
24     $entity_id      = $entity->id();
25     $bundle         = $entity->bundle();
26     $field_name     = $field->getName();
27     $field_type     = $field->getType();
28     $field_clean    = str_replace("field_", '', $field_name);
29     $target_type    = $field->getFieldStorageDefinition()->getSetting('target_type');
30     $view_mode      = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
31     $namespace      = $settings['namespace'] = empty($settings['namespace']) ? 'blazy' : $settings['namespace'];
32     $id             = isset($settings['id']) ? $settings['id'] : '';
33     $id             = Blazy::getHtmlId("{$namespace}-{$entity_type_id}-{$entity_id}-{$field_clean}-{$view_mode}", $id);
34     $switch         = empty($settings['media_switch']) ? '' : $settings['media_switch'];
35     $internal_path  = $absolute_path = NULL;
36
37     // Deals with UndefinedLinkTemplateException such as paragraphs type.
38     // @see #2596385, or fetch the host entity.
39     if (!$entity->isNew() && method_exists($entity, 'hasLinkTemplate')) {
40       if ($entity->hasLinkTemplate('canonical')) {
41         $url = $entity->toUrl();
42         $internal_path = $url->getInternalPath();
43         $absolute_path = $url->setAbsolute()->toString();
44       }
45     }
46
47     $settings['breakpoints']    = isset($settings['breakpoints']) && empty($settings['responsive_image_style']) ? $settings['breakpoints'] : [];
48     $settings['bundle']         = $bundle;
49     $settings['cache_metadata'] = ['keys' => [$id, $count]];
50     $settings['content_url']    = $settings['absolute_path'] = $absolute_path;
51     $settings['count']          = $count;
52     $settings['entity_id']      = $entity_id;
53     $settings['entity_type_id'] = $entity_type_id;
54     $settings['field_type']     = $field_type;
55     $settings['field_name']     = $field_name;
56     $settings['id']             = $id;
57     $settings['internal_path']  = $internal_path;
58     $settings['lightbox']       = ($switch && in_array($switch, $this->getLightboxes())) ? $switch : FALSE;
59     $settings['resimage']       = function_exists('responsive_image_get_image_dimensions');
60     $settings['target_type']    = $target_type;
61
62     unset($entity, $field);
63
64     // @todo: Enable after proper checks.
65     // $settings = array_filter($settings);
66     if (!empty($settings['vanilla'])) {
67       $settings = array_filter($settings);
68       return;
69     }
70
71     if (!empty($settings['breakpoints'])) {
72       $this->cleanUpBreakpoints($settings);
73     }
74
75     $settings['caption']    = empty($settings['caption']) ? [] : array_filter($settings['caption']);
76     $settings['background'] = empty($settings['responsive_image_style']) && !empty($settings['background']);
77     $resimage_lazy          = $this->configLoad('responsive_image') && !empty($settings['responsive_image_style']);
78     $settings['blazy']      = $resimage_lazy || !empty($settings['blazy']);
79
80     if (!empty($settings['blazy'])) {
81       $settings['lazy'] = 'blazy';
82     }
83
84     // Aspect ratio isn't working with Responsive image, yet.
85     // However allows custom work to get going with an enforced.
86     $ratio = FALSE;
87     if (!empty($settings['ratio'])) {
88       $ratio = empty($settings['responsive_image_style']);
89       if ($settings['ratio'] == 'enforced' || $settings['background']) {
90         $ratio = TRUE;
91       }
92     }
93
94     $settings['ratio'] = $ratio ? $settings['ratio'] : FALSE;
95
96     // Sets dimensions once, if cropped, to reduce costs with ton of images.
97     // This is less expensive than re-defining dimensions per image.
98     if (!empty($settings['image_style']) && !$resimage_lazy) {
99       if ($field_type == 'image' && $items[0]) {
100         $settings['item'] = $items[0];
101         $settings['uri']  = ($file = $items[0]->entity) && empty($items[0]->uri) ? $file->getFileUri() : $items[0]->uri;
102       }
103
104       if (!empty($settings['uri'])) {
105         $this->setDimensionsOnce($settings);
106       }
107     }
108
109     // Add the entity to formatter cache tags.
110     $settings['cache_tags'][] = $settings['entity_type_id'] . ':' . $settings['entity_id'];
111
112     $this->getModuleHandler()->alter($namespace . '_settings', $build, $items);
113   }
114
115 }