You are here

protected function BackgroundImageCssController::getCssTemplate in Background Image 2.0.x

Same name and namespace in other branches
  1. 8 src/Controller/BackgroundImageCssController.php \Drupal\background_image\Controller\BackgroundImageCssController::getCssTemplate()
  2. 2.x src/Controller/BackgroundImageCssController.php \Drupal\background_image\Controller\BackgroundImageCssController::getCssTemplate()

Retrieves the Twig template filename used to generate the necessary CSS.

Return value

string

1 call to BackgroundImageCssController::getCssTemplate()
BackgroundImageCssController::buildCss in src/Controller/BackgroundImageCssController.php
Generates the necessary CSS for a background image.

File

src/Controller/BackgroundImageCssController.php, line 401

Class

BackgroundImageCssController
Defines a controller to serve image styles.

Namespace

Drupal\background_image\Controller

Code

protected function getCssTemplate() {
  if (!isset($this->cssTemplates)) {
    $cache = \Drupal::cache()
      ->get('background_image_css_templates');
    $this->cssTemplates = $cache && is_array($cache->data) ? $cache->data : [];
  }
  $activeTheme = $this->themeManager
    ->getActiveTheme();
  $activeThemeName = $activeTheme
    ->getName();
  if (!isset($this->cssTemplates[$activeThemeName])) {

    // Search for a theme based template file.
    $templatePaths = [
      $activeTheme
        ->getPath() . '/templates',
    ];
    foreach ($activeTheme
      ->getBaseThemeExtensions() as $name => $extension) {
      $templatePaths[$name] = $extension
        ->getPath() . '/templates';
    }
    $mask = '/' . preg_quote('background_image.css.twig', '/') . '$/';
    foreach (array_unique($templatePaths) as $path) {
      try {
        if (is_dir($path) && ($file = current($this->fileSystem
          ->scanDirectory($path, $mask)))) {
          $cssTemplate = str_replace(\Drupal::root() . '/', '', $file->uri);
          break;
        }
      } catch (FileException $e) {

        // Intentionally ignored, do nothing.
      }
    }

    // Default to this module's template if none was found.
    if (!isset($cssTemplate)) {
      $cssTemplate = drupal_get_path('module', 'background_image') . '/templates/background_image.css.twig';
    }
    $this->cssTemplates[$activeThemeName] = $cssTemplate;
    \Drupal::cache()
      ->set('background_image_css_templates', $this->cssTemplates);
  }
  return $this->cssTemplates[$activeThemeName];
}