X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fblazy%2Fsrc%2FBlazyFormatterManager.php;fp=web%2Fmodules%2Fcontrib%2Fblazy%2Fsrc%2FBlazyFormatterManager.php;h=de7b8ad188b591aafbafb66ab895df26124bb6b9;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/blazy/src/BlazyFormatterManager.php b/web/modules/contrib/blazy/src/BlazyFormatterManager.php new file mode 100644 index 000000000..de7b8ad18 --- /dev/null +++ b/web/modules/contrib/blazy/src/BlazyFormatterManager.php @@ -0,0 +1,123 @@ +storage->id(); + $settings['current_view_mode'] = $view->current_display; + } + + $count = $items->count(); + $field = $items->getFieldDefinition(); + $entity = $items->getEntity(); + $entity_type_id = $entity->getEntityTypeId(); + $entity_id = $entity->id(); + $bundle = $entity->bundle(); + $field_name = $field->getName(); + $field_type = $field->getType(); + $field_clean = str_replace("field_", '', $field_name); + $target_type = $field->getFieldStorageDefinition()->getSetting('target_type'); + $view_mode = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode']; + $namespace = $settings['namespace'] = empty($settings['namespace']) ? 'blazy' : $settings['namespace']; + $id = isset($settings['id']) ? $settings['id'] : ''; + $id = Blazy::getHtmlId("{$namespace}-{$entity_type_id}-{$entity_id}-{$field_clean}-{$view_mode}", $id); + $switch = empty($settings['media_switch']) ? '' : $settings['media_switch']; + $internal_path = $absolute_path = NULL; + + // Deals with UndefinedLinkTemplateException such as paragraphs type. + // @see #2596385, or fetch the host entity. + if (!$entity->isNew() && method_exists($entity, 'hasLinkTemplate')) { + if ($entity->hasLinkTemplate('canonical')) { + $url = $entity->toUrl(); + $internal_path = $url->getInternalPath(); + $absolute_path = $url->setAbsolute()->toString(); + } + } + + $settings += [ + 'absolute_path' => $absolute_path, + 'bundle' => $bundle, + 'content_url' => $absolute_path, + 'count' => $count, + 'entity_id' => $entity_id, + 'entity_type_id' => $entity_type_id, + 'field_type' => $field_type, + 'field_name' => $field_name, + 'internal_path' => $internal_path, + 'target_type' => $target_type, + 'cache_metadata' => ['keys' => [$id, $count]], + ]; + + unset($entity, $field); + + $settings['id'] = $id; + $settings['lightbox'] = ($switch && in_array($switch, $this->getLightboxes())) ? $switch : FALSE; + $settings['breakpoints'] = isset($settings['breakpoints']) && empty($settings['responsive_image_style']) ? $settings['breakpoints'] : []; + + // @todo: Enable after proper checks. + // $settings = array_filter($settings); + if (!empty($settings['vanilla'])) { + $settings = array_filter($settings); + return; + } + + if (!empty($settings['breakpoints'])) { + $this->cleanUpBreakpoints($settings); + } + + $settings['caption'] = empty($settings['caption']) ? [] : array_filter($settings['caption']); + $settings['resimage'] = function_exists('responsive_image_get_image_dimensions'); + $settings['background'] = empty($settings['responsive_image_style']) && !empty($settings['background']); + $resimage_lazy = $this->configLoad('responsive_image') && !empty($settings['responsive_image_style']); + $settings['blazy'] = $resimage_lazy || !empty($settings['blazy']); + + if (!empty($settings['blazy'])) { + $settings['lazy'] = 'blazy'; + } + + // Aspect ratio isn't working with Responsive image, yet. + // However allows custom work to get going with an enforced. + $ratio = FALSE; + if (!empty($settings['ratio'])) { + $ratio = empty($settings['responsive_image_style']); + if ($settings['ratio'] == 'enforced' || $settings['background']) { + $ratio = TRUE; + } + } + + $settings['ratio'] = $ratio ? $settings['ratio'] : FALSE; + + // Sets dimensions once, if cropped, to reduce costs with ton of images. + // This is less expensive than re-defining dimensions per image. + if (!empty($settings['image_style']) && !$resimage_lazy) { + if ($field_type == 'image' && $items[0]) { + $settings['item'] = $items[0]; + $settings['uri'] = $items[0]->entity->getFileUri(); + } + + if (!empty($settings['uri'])) { + $this->setDimensionsOnce($settings); + } + } + + $this->getModuleHandler()->alter($namespace . '_settings', $build, $items); + } + +}