Commit bbd01719 authored by frjo's avatar frjo
Browse files

Issue #2649886 by frjo, snsblvd: Content image style for first image is not working

parent 6429cc95
......@@ -8,6 +8,7 @@
use Drupal\Core\Template\Attribute;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Unicode;
use Drupal\image\Entity\ImageStyle;
/**
* Prepares variables for colorbox formatter templates.
......@@ -27,10 +28,11 @@ function template_preprocess_colorbox_formatter(&$variables) {
$item = $variables['item'];
$item_attributes = isset($variables['item_attributes']) ? $variables['item_attributes'] : array();
$entity = $variables['entity'];
$settings = $variables['settings'];
$variables = array(); // Reset the $variables parameter.
$image_uri = $item->entity->getFileUri();
$classes_array = array('colorbox');
// Build the caption.
$entity_title = $entity->label();
......@@ -114,16 +116,14 @@ function template_preprocess_colorbox_formatter(&$variables) {
}
// Set up the $variables['image'] parameter.
if ($settings['style_first']) {
$variables['image'] = array(
'#theme' => 'image_style',
'#style_name' => $settings['colorbox_node_style_first'],
);
if ($settings['style_name'] == 'hide') {
$variables['image'] = array();
$classes_array[] = 'js-hide';
}
elseif (!empty($settings['colorbox_node_style'])) {
elseif (!empty($settings['style_name'])) {
$variables['image'] = array(
'#theme' => 'image_style',
'#style_name' => $settings['colorbox_node_style'],
'#style_name' => $settings['style_name'],
);
}
else {
......@@ -131,6 +131,8 @@ function template_preprocess_colorbox_formatter(&$variables) {
'#theme' => 'image',
);
}
if (!empty($variables['image'])) {
$variables['image']['#attributes'] = $item_attributes;
// Do not output an empty 'title' attribute.
......@@ -142,23 +144,19 @@ function template_preprocess_colorbox_formatter(&$variables) {
$variables['image']["#$key"] = $item->$key;
}
$image_uri = $item->entity->getFileUri();
$variables['image']['#uri'] = empty($item->uri) ? $image_uri : $item->uri;
}
if (!empty($settings['colorbox_image_style'])) {
$style = entity_load('image_style', $settings['colorbox_image_style']);
$style = ImageStyle::load($settings['colorbox_image_style']);
$variables['url'] = $style->buildUrl($image_uri);
}
else {
$variables['url'] = file_create_url($image_uri);
}
$class = array('colorbox');
if ($settings['colorbox_node_style'] == 'hide') {
$class[] = 'js-hide';
}
$variables['attributes'] = new Attribute();
$variables['attributes']['title'] = $caption;
$variables['attributes']['rel'] = $gallery_id;
$variables['attributes']['class'] = $class;
$variables['attributes']['class'] = $classes_array;
}
......@@ -6,7 +6,7 @@ field.formatter.settings.colorbox:
type: string
label: 'Node Style'
colorbox_node_style_first:
type: boolean
type: string
label: 'Node style first'
colorbox_image_style:
type: string
......
......@@ -282,7 +282,7 @@ class ColorboxFormatter extends ImageFormatterBase implements ContainerFactoryPl
// Collect cache tags to be added for each item in the field.
$cache_tags = array();
if (!empty($settings['colorbox_node_style'])) {
if (!empty($settings['colorbox_node_style']) && $settings['colorbox_node_style'] != 'hide') {
$image_style = $this->imageStyleStorage->load($settings['colorbox_node_style']);
$cache_tags = $image_style->getCacheTags();
}
......@@ -296,10 +296,12 @@ class ColorboxFormatter extends ImageFormatterBase implements ContainerFactoryPl
// Check if first image should have separate image style.
if ($delta == 0 && !empty($settings['colorbox_node_style_first'])) {
$settings['style_first'] = TRUE;
$settings['style_name'] = $settings['colorbox_node_style_first'];
$cache_tags = Cache::mergeTags($cache_tags_first, $file->getCacheTags());
}
else {
$settings['style_first'] = FALSE;
$settings['style_name'] = $settings['colorbox_node_style'];
$cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment