Closed (fixed)
Project:
Simple Background image formatter
Version:
8.x-1.1
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
26 Oct 2016 at 12:11 UTC
Updated:
13 Oct 2017 at 20:07 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
niekheezemans commentedHi,
I am experiencing the same thing. I created a fix by only adding the '_$id' when using the inline styling option and adding the rendering.
In the BackgroundImageFormatter.php file on line 143 I modified the code like this:
Also the styling is not rendered in the switch statement case 'css'. Add this line just before the break:
When you use a selector make sure you use a '.' for a class or '#' for an id. F.e `.image-bg or `#image-bg`
Grtz Niek
The complete BackgroundImageFormatter.php file:
will be created with the image set to the background-image property.', [
'@background_image_selector' => $this->getSetting('background_image_selector') . '_id',
]);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$image_style = NULL;
if (!$this->isBackgroundImageDisplay()) {
return $elements;
}
$image_style = $this->getSetting('image_style');
if (!empty($image_style)) {
$image_style = ImageStyle::load($image_style);
}
foreach ($items as $delta => $item) {
if (!$item->entity) {
continue;
}
$image_uri = $item->entity->url();
$id = $item->entity->id();
if ($image_style) {
$image_uri = $item->entity->getFileUri();
$image_uri = ImageStyle::load($image_style->getName())->buildUrl($image_uri);
}
$selector = strip_tags($this->getSetting('background_image_selector'));
if( $this->getSetting('background_image_output_type') == 'inline' ){
$selector .= '_' . $id;
}
$theme = array(
'#background_image_selector' => $selector,
'#image_uri' => $image_uri,
);
switch ($this->getSetting('background_image_output_type')) {
case 'css':
$data = [
'#tag' => 'style',
'#value' => $this->generateCssString($theme),
];
$elements['#attached']['html_head'][] = [
$data,
'background_image_formatter_' . $id,
];
\Drupal::service('renderer')->render($elements);
break;
case 'inline':
$theme['#theme'] = 'background_image_formatter_inline';
$elements[$delta] = array(
'#markup' => \Drupal::service('renderer')->render($theme),
);
break;
}
}
return $elements;
}
protected function isBackgroundImageDisplay() {
return $this->getPluginId() == 'background_image_formatter';
}
protected function generateCssString($theme) {
return $theme['#background_image_selector'] . '{ background-image: url("' . $theme['#image_uri'] . '"); }';
}
}
Comment #3
garethhallnz commentedHi guys, just checked one of my sites and it's working for me.
A few questions:
1. Have you entered a css class in the field UI?
2. What version of Drupal?
3. Is single of multi field?
4. Exact steps to reproduce or what expectation has not been met?
Comment #4
joonapenttila commentedThanks niekheezemans! :)
Comment #5
alex_optimCreate patch.
Comment #6
niekheezemans commentedThnx for creating the patch Alex, will do it next time.
@ garethhallnz:
1. Have you entered a css class in the field UI? => Yes
2. What version of Drupal? => Drupal 8.2.1
3. Is single of multi field? => What do you mean? I am using the CSS selector class once on a page...
4. Exact steps to reproduce or what expectation has not been met? => When using the option Background Image Format -> Output to: Write Background-image to CSS selector, the background image was not attached to the CSS selector since the module added an $id value to the css classname that is used for attaching the backgroun-image style. F.e. when you entered
as a selector, the module added an $id to the selector making the CSS selector f.e.
Comment #7
garethhallnz commentedOhh I see. I think the current functionality is correct. The entityID must be added for when there is more than 1 image on the page.
For example
If you have and article content type with a full and teaser view mode showing a background image with the class of test
Now if you create a view showing recent articles with the teaser view mode; you need to append the entityID the make the selector unique.
I think the main thing that missing for the "Write Background-image to CSS selector" option is that it doesn't create an empty div for the css selector?
NOTE:
It's also worth mentioning this project has been marked as obsolete.
Therefor it's not worth doing any work here as we are in the process of merging this with Background Images Formatter module.
Progress to be document at: https://www.drupal.org/node/2820564
Comment #10
zulljin commentedalex_optim, thanks for the patch.
Comment #12
alex_optim