Skip to content
Snippets Groups Projects
Commit c9fcb5cc authored by Edouard Cunibil's avatar Edouard Cunibil
Browse files

Issue #3216397 by tbenice, duaelfr: Allow all preprocess functions to modify...

parent d00508ef
No related branches found
No related tags found
1 merge request!15Issue #3216397 by tbenice, duaelfr: Allow all preprocess functions to modify...
Pipeline #462421 passed
......@@ -8,8 +8,10 @@ use Drupal\bg_image_formatter\Plugin\Field\FieldFormatter\BgImageFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Theme\Registry;
use Drupal\Core\Url;
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Responsive Background Image formatter.
......@@ -22,6 +24,22 @@ use Drupal\responsive_image\Entity\ResponsiveImageStyle;
*/
class ResponsiveBgImageFormatter extends BgImageFormatter {
/**
* The theme registry service.
*
* @var \Drupal\Core\Theme\Registry
*/
protected Registry $registry;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->registry = $container->get('theme.registry');
return $instance;
}
/**
* {@inheritdoc}
*/
......@@ -102,23 +120,32 @@ class ResponsiveBgImageFormatter extends BgImageFormatter {
static::$selectorIndex[$field_id]++;
$uri = $file->getFileUri();
$vars = [
$variables = [
'uri' => $file->getFileUri(),
'responsive_image_style_id' => $settings['image_style'],
'width' => 1,
'height' => 1,
];
if (file_exists($uri)) {
[$vars['width'], $vars['height']] = getimagesize($uri);
[$variables['width'], $variables['height']] = getimagesize($uri);
}
// Call preprocess functions to let other modules/themes react before
// rendering.
$info = $this->registry->get();
$hook = 'responsive_image__responsive_bg_image_formatter';
foreach ($info['responsive_image']['preprocess functions'] as $preprocessor_function) {
if (is_callable($preprocessor_function)) {
call_user_func_array($preprocessor_function, [&$variables, $hook, $info]);
}
}
template_preprocess_responsive_image($vars);
if (empty($vars['sources'])) {
if (empty($variables['sources'])) {
continue;
}
// Split each source into multiple rules.
foreach (array_reverse($vars['sources']) as $source_i => $source) {
foreach (array_reverse($variables['sources']) as $source_i => $source) {
$attr = $source->toArray();
$srcset = explode(', ', $attr['srcset']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment