Version 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
21     // Sniffs for Views to allow block__no_wrapper, views_no_wrapper, etc.
22     if (function_exists('views_get_current_view') && $view = views_get_current_view()) {
23       $settings['view_name'] = $view->storage->id();
24       $settings['current_view_mode'] = $view->current_display;
25     }
26
27     $count          = $items->count();
28     $field          = $items->getFieldDefinition();
29     $entity         = $items->getEntity();
30     $entity_type_id = $entity->getEntityTypeId();
31     $entity_id      = $entity->id();
32     $bundle         = $entity->bundle();
33     $field_name     = $field->getName();
34     $field_type     = $field->getType();
35     $field_clean    = str_replace("field_", '', $field_name);
36     $target_type    = $field->getFieldStorageDefinition()->getSetting('target_type');
37     $view_mode      = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
38     $namespace      = $settings['namespace'] = empty($settings['namespace']) ? 'blazy' : $settings['namespace'];
39     $id             = isset($settings['id']) ? $settings['id'] : '';
40     $id             = Blazy::getHtmlId("{$namespace}-{$entity_type_id}-{$entity_id}-{$field_clean}-{$view_mode}", $id);
41     $switch         = empty($settings['media_switch']) ? '' : $settings['media_switch'];
42     $internal_path  = $absolute_path = NULL;
43
44     // Deals with UndefinedLinkTemplateException such as paragraphs type.
45     // @see #2596385, or fetch the host entity.
46     if (!$entity->isNew() && method_exists($entity, 'hasLinkTemplate')) {
47       if ($entity->hasLinkTemplate('canonical')) {
48         $url = $entity->toUrl();
49         $internal_path = $url->getInternalPath();
50         $absolute_path = $url->setAbsolute()->toString();
51       }
52     }
53
54     $settings += [
55       'absolute_path'  => $absolute_path,
56       'bundle'         => $bundle,
57       'content_url'    => $absolute_path,
58       'count'          => $count,
59       'entity_id'      => $entity_id,
60       'entity_type_id' => $entity_type_id,
61       'field_type'     => $field_type,
62       'field_name'     => $field_name,
63       'internal_path'  => $internal_path,
64       'target_type'    => $target_type,
65       'cache_metadata' => ['keys' => [$id, $count]],
66     ];
67
68     unset($entity, $field);
69
70     $settings['id']          = $id;
71     $settings['lightbox']    = ($switch && in_array($switch, $this->getLightboxes())) ? $switch : FALSE;
72     $settings['breakpoints'] = isset($settings['breakpoints']) && empty($settings['responsive_image_style']) ? $settings['breakpoints'] : [];
73
74     // @todo: Enable after proper checks.
75     // $settings = array_filter($settings);
76     if (!empty($settings['vanilla'])) {
77       $settings = array_filter($settings);
78       return;
79     }
80
81     if (!empty($settings['breakpoints'])) {
82       $this->cleanUpBreakpoints($settings);
83     }
84
85     $settings['caption']    = empty($settings['caption']) ? [] : array_filter($settings['caption']);
86     $settings['resimage']   = function_exists('responsive_image_get_image_dimensions');
87     $settings['background'] = empty($settings['responsive_image_style']) && !empty($settings['background']);
88     $resimage_lazy          = $this->configLoad('responsive_image') && !empty($settings['responsive_image_style']);
89     $settings['blazy']      = $resimage_lazy || !empty($settings['blazy']);
90
91     if (!empty($settings['blazy'])) {
92       $settings['lazy'] = 'blazy';
93     }
94
95     // Aspect ratio isn't working with Responsive image, yet.
96     // However allows custom work to get going with an enforced.
97     $ratio = FALSE;
98     if (!empty($settings['ratio'])) {
99       $ratio = empty($settings['responsive_image_style']);
100       if ($settings['ratio'] == 'enforced' || $settings['background']) {
101         $ratio = TRUE;
102       }
103     }
104
105     $settings['ratio'] = $ratio ? $settings['ratio'] : FALSE;
106
107     // Sets dimensions once, if cropped, to reduce costs with ton of images.
108     // This is less expensive than re-defining dimensions per image.
109     if (!empty($settings['image_style']) && !$resimage_lazy) {
110       if ($field_type == 'image' && $items[0]) {
111         $settings['item'] = $items[0];
112         $settings['uri']  = $items[0]->entity->getFileUri();
113       }
114
115       if (!empty($settings['uri'])) {
116         $this->setDimensionsOnce($settings);
117       }
118     }
119
120     $this->getModuleHandler()->alter($namespace . '_settings', $build, $items);
121   }
122
123 }