View source
<?php
namespace Drupal\blazy;
class BlazyFormatter extends BlazyManager implements BlazyFormatterInterface {
private $isImageDimensionSet;
protected $cropStyles;
protected $isCrop;
public function buildSettings(array &$build, $items) {
$settings =& $build['settings'];
$this
->getCommonSettings($settings);
$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_clean = str_replace("field_", '', $field_name);
$view_mode = empty($settings['current_view_mode']) ? '_custom' : $settings['current_view_mode'];
$namespace = $settings['namespace'];
$id = isset($settings['id']) ? $settings['id'] : '';
$gallery_id = "{$namespace}-{$entity_type_id}-{$bundle}-{$field_clean}-{$view_mode}";
$id = Blazy::getHtmlId("{$gallery_id}-{$entity_id}", $id);
$internal_path = $absolute_path = NULL;
if (!$entity
->isNew() && method_exists($entity, 'hasLinkTemplate')) {
if ($entity
->hasLinkTemplate('canonical')) {
if ($entity
->hasTranslation($settings['current_language'])) {
$url = $entity
->getTranslation($settings['current_language'])
->toUrl();
}
else {
$url = $entity
->toUrl();
}
$internal_path = $url
->getInternalPath();
$absolute_path = $url
->setAbsolute()
->toString();
}
}
$settings['bundle'] = $bundle;
$settings['cache_metadata'] = [
'keys' => [
$id,
$count,
],
];
$settings['cache_tags'][] = $entity_type_id . ':' . $entity_id;
$settings['caption'] = empty($settings['caption']) ? [] : array_filter($settings['caption']);
$settings['content_url'] = $settings['absolute_path'] = $absolute_path;
$settings['count'] = $count;
$settings['entity_id'] = $entity_id;
$settings['entity_type_id'] = $entity_type_id;
$settings['gallery_id'] = str_replace('_', '-', $gallery_id . '-' . $settings['media_switch']);
$settings['id'] = $id;
$settings['internal_path'] = $internal_path;
$settings['use_field'] = !$settings['lightbox'] && isset($settings['third_party'], $settings['third_party']['linked_field']) && !empty($settings['third_party']['linked_field']['linked']);
if (!empty($settings['vanilla'])) {
$settings = array_filter($settings);
return;
}
$settings['blazy'] = !empty($settings['blazy']) || !empty($settings['background']) || $settings['resimage'];
$settings['lazy'] = $settings['blazy'] ? 'blazy' : (isset($settings['lazy']) ? $settings['lazy'] : '');
$settings['lazy'] = empty($settings['is_preview']) ? $settings['lazy'] : '';
}
public function preBuildElements(array &$build, $items, array $entities = []) {
$this
->buildSettings($build, $items);
$settings =& $build['settings'];
if (isset($items[0]) && ($item = $items[0])) {
$this
->extractFirstItem($settings, $item, reset($entities));
}
if (!empty($settings['_uri'])) {
if (empty($settings['resimage'])) {
$this
->setImageDimensions($settings);
}
elseif (!empty($settings['resimage']) && !empty($settings['ratio']) && $settings['ratio'] == 'fluid') {
$this
->setResponsiveImageDimensions($settings);
}
}
$this
->getModuleHandler()
->alter('blazy_settings', $build, $items);
}
public function postBuildElements(array &$build, $items, array $entities = []) {
}
public function extractFirstItem(array &$settings, $item, $entity = NULL) {
if ($settings['field_type'] == 'image') {
$settings['_item'] = $item;
$settings['_uri'] = ($file = $item->entity) && empty($item->uri) ? $file
->getFileUri() : $item->uri;
}
elseif ($entity && $entity
->hasField('thumbnail') && ($image = $entity
->get('thumbnail')
->first())) {
if (isset($image->entity) && ($file = $image->entity)) {
$settings['_item'] = $image;
$settings['_uri'] = $file
->getFileUri();
}
}
if (!empty($settings['_item'])) {
BlazyUtil::imageDimensions($settings, $settings['_item'], TRUE);
}
}
protected function setImageDimensions(array &$settings = []) {
if (!isset($this->isImageDimensionSet[md5($settings['id'])])) {
if (!empty($settings['image_style']) && ($style = $this
->isCrop($settings['image_style']))) {
$settings = array_merge($settings, BlazyUtil::transformDimensions($style, $settings, TRUE));
$settings['_dimensions'] = TRUE;
}
$this->isImageDimensionSet[md5($settings['id'])] = TRUE;
}
}
private function cropStyles() {
if (!isset($this->cropStyles)) {
$this->cropStyles = [];
foreach ($this
->entityLoadMultiple('image_style') as $style) {
foreach ($style
->getEffects() as $effect) {
if (strpos($effect
->getPluginId(), 'crop') !== FALSE) {
$this->cropStyles[$style
->getName()] = $style;
break;
}
}
}
}
return $this->cropStyles;
}
public function isCrop($style) {
if (!isset($this->isCrop[$style])) {
$this->isCrop[$style] = $this
->cropStyles() && isset($this
->cropStyles()[$style]) ? $this
->cropStyles()[$style] : FALSE;
}
return $this->isCrop[$style];
}
}