protected function BackgroundImageCssController::buildMediaQueries in Background Image 2.x
Same name and namespace in other branches
- 8 src/Controller/BackgroundImageCssController.php \Drupal\background_image\Controller\BackgroundImageCssController::buildMediaQueries()
- 2.0.x src/Controller/BackgroundImageCssController.php \Drupal\background_image\Controller\BackgroundImageCssController::buildMediaQueries()
Build a list of media queries.
Parameters
\Drupal\background_image\BackgroundImageInterface $background_image: The background image entity.
Return value
array An sorted indexed array of associative arrays.
1 call to BackgroundImageCssController::buildMediaQueries()
- BackgroundImageCssController::buildCss in src/
Controller/ BackgroundImageCssController.php - Generates the necessary CSS for a background image.
File
- src/
Controller/ BackgroundImageCssController.php, line 202
Class
- BackgroundImageCssController
- Defines a controller to serve image styles.
Namespace
Drupal\background_image\ControllerCode
protected function buildMediaQueries(BackgroundImageInterface $background_image) {
$responsive_image_style = $this->backgroundImageManager
->getResponsiveImageStyle();
// Immediately return if there is no responsive image style.
if (!$this->breakpointManager || !$responsive_image_style) {
return [];
}
$mediaQueries = [];
// Get the necessary variables.
$breakpoints = $this->breakpointManager
->getBreakpointsByGroup($responsive_image_style
->getBreakpointGroup());
$keyed_image_style_mappings = $responsive_image_style
->getKeyedImageStyleMappings();
$retinaRules = $this->backgroundImageManager
->getRetinaRules();
// Retrieve the responsive image sources.
$i = 0;
foreach ($breakpoints as $breakpoint_id => $breakpoint) {
if (isset($keyed_image_style_mappings[$breakpoint_id])) {
$mediaQuery = trim($breakpoint
->getMediaQuery());
foreach ($keyed_image_style_mappings[$breakpoint_id] as $multiplier => $image_style_mapping) {
if ($image_style_mapping['image_mapping_type'] !== 'image_style') {
continue;
}
// Use multiplier as a key so it can be sorted in the array later.
$key = intval(mb_substr($multiplier, 0, -1) * 100) + $i++;
$image_style = $image_style_mapping['image_mapping'];
// Merge the multiplier with retina rules.
if ($multiplier === "2x") {
$rules = [];
foreach ($retinaRules as $retinaRule) {
$rules[] = trim($retinaRule) . ' and ' . trim(preg_replace('/^\\s*(only )?(all|print|screen)\\s?(and)?/', '', $mediaQuery));
}
$mediaQueries[$key] = [
'image_style' => $image_style,
'multiplier' => $multiplier,
'query' => implode(',', $rules),
'url' => $background_image
->getImageUrl($image_style),
];
}
else {
$mediaQueries[$key] = [
'image_style' => $image_style,
'multiplier' => $multiplier,
'query' => $mediaQuery,
'url' => $background_image
->getImageUrl($image_style),
];
}
}
}
}
// Sort the the media queries so the multipliers are aft
ksort($mediaQueries);
return $mediaQueries;
}