public function BackgroundImageCssController::buildCss in Background Image 2.0.x
Same name and namespace in other branches
- 8 src/Controller/BackgroundImageCssController.php \Drupal\background_image\Controller\BackgroundImageCssController::buildCss()
- 2.x src/Controller/BackgroundImageCssController.php \Drupal\background_image\Controller\BackgroundImageCssController::buildCss()
Generates the necessary CSS for a background image.
Parameters
\Drupal\background_image\BackgroundImageInterface $background_image: A background image entity.
string $uri: The URI path on where to store the generated CSS.
Return value
bool TRUE or FALSE
1 call to BackgroundImageCssController::buildCss()
- BackgroundImageCssController::deliver in src/
Controller/ BackgroundImageCssController.php - Generates a background CSS file.
File
- src/
Controller/ BackgroundImageCssController.php, line 145
Class
- BackgroundImageCssController
- Defines a controller to serve image styles.
Namespace
Drupal\background_image\ControllerCode
public function buildCss(BackgroundImageInterface $background_image, $uri) {
// Immediately return if there is is no image file.
if (!$background_image
->getImageFile()) {
$this->logger
->error('Background image does not have a valid image file: background_image:@id', [
'@id' => $background_image
->id(),
]);
return FALSE;
}
// Build the destination folder tree if it doesn't already exist.
$directory = $this->fileSystem
->dirname($uri);
if (!$this->fileSystem
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
$this->logger
->error('Failed to create background image directory: %directory', [
'%directory' => $directory,
]);
return FALSE;
}
$variables = [
'base_class' => $this->backgroundImageManager
->getBaseClass(),
'background_image_class' => $background_image
->getCssClass(),
'settings' => $background_image
->getSettings()
->get(),
'preload_url' => $background_image
->getImageUrl($this->backgroundImageManager
->getPreloadImageStyle()),
'fallback_url' => $background_image
->getImageUrl($this->backgroundImageManager
->getFallbackImageStyle()),
'media_queries' => $this
->buildMediaQueries($background_image),
];
$cssTemplate = $this
->getCssTemplate();
$this
->moduleHandler()
->alter('background_image_css_template', $variables, $cssTemplate, $background_image);
$this->themeManager
->alter('background_image_css_template', $variables, $cssTemplate, $background_image);
// Render the template.
try {
$data = $this->twig
->loadTemplate($cssTemplate)
->render($variables);
// Minify the CSS if necessary.
if (preg_match('/\\.min\\.css$/', $uri) && ($css_minifier = $this->backgroundImageManager
->getCssMinifier())) {
$data = $css_minifier
->optimize($data, [], []);
$css_minifier
->addLicense($data, preg_replace('/\\.min\\.css$/', '.css', file_create_url($uri)));
}
if (!$this
->dump($data, $uri)) {
return FALSE;
}
} catch (\Exception $e) {
$previous_exception = $e
->getPrevious();
$this->logger
->error($previous_exception ? $previous_exception
->getMessage() : $e
->getMessage());
return FALSE;
}
return TRUE;
}