Skip to content
Snippets Groups Projects
Commit 935cc085 authored by cchoe1's avatar cchoe1
Browse files

add logger, check if responsive_image style is set

parent 05e8008f
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,13 @@ 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;
/**
* Plugin implementation of the 'image' formatter.
......@@ -27,6 +34,49 @@ use Drupal\bg_img_field\Component\Render\CSSSnippet;
*/
class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerFactoryPluginInterface {
/**
* @var Drupal\Core\Logger\LoggerChannelTrait
*/
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;
}
/**
* {@inheritdoc}
*/
......@@ -179,47 +229,75 @@ class BgImgFieldFormatter extends ResponsiveImageFormatter implements ContainerF
$css .= "background-position: " . $options['css_background_position'] .";";
$css .= '}';
$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])) {
// $responsive_image_style holds the configuration from the responsive_image module for a given responsive style
// We need to check that this exists or else we get a WSOD
if (!$responsive_image_style) {
$field_definition = $this->fieldDefinition->getFieldStorageDefinition();
$multipliers = array_reverse($multipliers);
$query = $breakpoints[$breakpoint_id]->getMediaQuery();
if ($query != "") {
$css .= ' @media ' . $query . ' {';
}
$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])) {
foreach ($multipliers as $multiplier => $mapping) {
$multiplier = rtrim($multiplier, "x");
$multipliers = array_reverse($multipliers);
if($mapping['image_mapping_type'] != 'image_style') {
continue;
$query = $breakpoints[$breakpoint_id]->getMediaQuery();
if ($query != "") {
$css .= ' @media ' . $query . ' {';
}
if ($mapping['image_mapping'] == "_original image_") {
$url = file_create_url($image->getFileUri());
}
else {
$url = ImageStyle::load($mapping['image_mapping'])->buildUrl($image->getFileUri());
}
foreach ($multipliers as $multiplier => $mapping) {
$multiplier = rtrim($multiplier, "x");
if($mapping['image_mapping_type'] != 'image_style') {
continue;
}
if ($multiplier != 1) {
$css .= ' @media (-webkit-min-device-pixel-ratio: ' . $multiplier . '), (min-resolution: ' . $multiplier * 96 . 'dpi), (min-resolution: ' . $multiplier . 'dppx) {';
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 .= '}';
}
}
$css .= $selector . ' {background-image: url(' . $url . ');}';
if ($multiplier != 1) {
if ($query != "") {
$css .= '}';
}
}
if ($query != "") {
$css .= '}';
}
}
}
return $css;
}
/**
* Implement abstract method
*
* @param $level
* @param $message
* @param array $context
*/
public function log($level, $message, array $context = []) {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment