View source
<?php
namespace Drupal\bg_img_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\bg_img_field\Component\Render\CSSSnippet;
use Drupal\Core\Logger\LoggerChannelTrait;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerFactoryPluginInterface {
use LoggerChannelTrait;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityStorageInterface $responsive_image_style_storage, EntityStorageInterface $image_style_storage, LinkGeneratorInterface $link_generator, AccountInterface $current_user) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $responsive_image_style_storage, $image_style_storage, $link_generator, $current_user);
$this->logger = $this
->getLogger('bg_img_field');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$container = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $container;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$options = $elements['responsive_image_style']['#options'];
$new_options = [];
foreach ($options as $key => $option) {
$storage = $this->responsiveImageStyleStorage
->load($key);
$image_style_mappings = $storage
->get('image_style_mappings');
if (isset($image_style_mappings[0]) && $image_style_mappings[0]['image_mapping_type'] === 'image_style') {
$new_options += [
$key => $option,
];
}
}
$elements['responsive_image_style']['#options'] = $new_options;
unset($elements['image_link']);
return $elements;
}
public function settingsSummary() {
$summary = [];
$responsive_image_style = $this->responsiveImageStyleStorage
->load($this
->getSetting('responsive_image_style'));
if ($responsive_image_style) {
$summary[] = $this
->t('Responsive image style: @responsive_image_style', [
'@responsive_image_style' => $responsive_image_style
->label(),
]);
}
else {
$summary[] = $this
->t('Select a responsive image style.');
}
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$entity = $items
->getEntity();
$files = [];
foreach ($items
->getValue() as $item) {
$files[] = [
'file' => File::load($item['target_id']),
'item' => $item,
];
}
if (empty($files)) {
return $elements;
}
return $this
->buildElement($files, $entity);
}
protected function buildElement(array $files, EntityInterface $entity) {
$elements = [];
$css = "";
$image_link_setting = $this
->getSetting('image_link');
$cache_contexts = [];
if ($image_link_setting == 'file') {
$cache_contexts[] = 'url.site';
}
$responsive_image_style = $this->responsiveImageStyleStorage
->load($this
->getSetting('responsive_image_style'));
$image_styles_to_load = [];
$cache_tags = [];
if ($responsive_image_style) {
$cache_tags = Cache::mergeTags($cache_tags, $responsive_image_style
->getCacheTags());
$image_styles_to_load = $responsive_image_style
->getImageStyleIds();
}
$image_styles = $this->imageStyleStorage
->loadMultiple($image_styles_to_load);
foreach ($image_styles as $image_style) {
$cache_tags = Cache::mergeTags($cache_tags, $image_style
->getCacheTags());
}
foreach ($files as $file) {
$selector = $file['item']['css_selector'];
$selector = \Drupal::token()
->replace($selector, [
$entity
->getEntityTypeId() => $entity,
], [
'clear' => TRUE,
]);
$css .= $this
->generateBackgroundCss($file['file'], $responsive_image_style, $selector, $file['item']);
if (!empty($css)) {
$current_path = \Drupal::request()
->getRequestUri();
if (preg_match('/node\\/(\\d+)\\/layout/', $current_path, $matches)) {
$elements = [
'#theme' => 'background_style',
'#css' => $css,
'#cache' => [
'tags' => $cache_tags,
'contexts' => $cache_contexts,
],
];
}
else {
$id = 'picture-background-formatter-' . $selector;
$elements['#attached']['html_head'][] = [
[
'#tag' => 'style',
'#value' => new CSSSnippet($css),
'#cache' => [
'tags' => $cache_tags,
'contexts' => $cache_contexts,
],
],
$id,
];
}
}
}
return $elements;
}
protected function generateBackgroundCss($image, $responsive_image_style, $selector, array $options) {
$css = "";
$css .= $selector . '{';
$css .= "background-repeat: " . $options['css_repeat'] . ";";
$css .= "background-size: " . $options['css_background_size'] . ";";
$css .= "background-position: " . $options['css_background_position'] . ";";
$css .= '}';
if (!$responsive_image_style) {
$field_definition = $this->fieldDefinition
->getFieldStorageDefinition();
$this->logger
->error('
There is no responsive image style set for the {field_name} field on the {entity_type} entity. Please ensure
that the responsive image style is configured at <a href="{link}">{link}</a>. Then set the correct style on the
formatter for the entity display.
', [
'field_name' => $field_definition
->get('field_name'),
'entity_type' => $field_definition
->get('entity_type'),
'link' => Url::fromRoute('entity.responsive_image_style.collection')
->toString(),
]);
}
else {
$breakpoints = \Drupal::service('breakpoint.manager')
->getBreakpointsByGroup($responsive_image_style
->getBreakpointGroup());
foreach (array_reverse($responsive_image_style
->getKeyedImageStyleMappings()) as $breakpoint_id => $multipliers) {
if (isset($breakpoints[$breakpoint_id])) {
$multipliers = array_reverse($multipliers);
$query = $breakpoints[$breakpoint_id]
->getMediaQuery();
if ($query != "") {
$css .= ' @media ' . $query . ' {';
}
foreach ($multipliers as $multiplier => $mapping) {
$multiplier = rtrim($multiplier, "x");
if ($mapping['image_mapping_type'] != 'image_style') {
continue;
}
if ($mapping['image_mapping'] == "_original image_") {
$url = file_create_url($image
->getFileUri());
}
else {
$url = ImageStyle::load($mapping['image_mapping'])
->buildUrl($image
->getFileUri());
}
if ($multiplier != 1) {
$css .= ' @media (-webkit-min-device-pixel-ratio: ' . $multiplier . '), (min-resolution: ' . $multiplier * 96 . 'dpi), (min-resolution: ' . $multiplier . 'dppx) {';
}
$css .= $selector . ' {background-image: url(' . $url . ');}';
if ($multiplier != 1) {
$css .= '}';
}
}
if ($query != "") {
$css .= '}';
}
}
}
}
return $css;
}
}