Images not generated when used in CSS

Created on 23 October 2020, over 3 years ago
Updated 14 June 2023, about 1 year ago

Problem/Motivation

After installing Focal Points and updating image styles to use it, images that are used in CSS background-image: url(path); no longer get generated. Images referenced in img tags render. And if I pass the image path first through file_exists() in PHP, that seems to be enough to trigger the generation. But something about CSS doesn't do this.

Steps to reproduce

  /**
   * Code to get file path.
   */
  protected function getFilePath(MediaInterface $entity, string $image_style) {
    $fid = $entity->getSource()->getSourceFieldValue($entity);
    /** @var \Drupal\file\FileInterface $file */
    $file = $this->entityTypeManager->getStorage('file')->load($fid);
    /** @var \Drupal\image\ImageStyleInterface $style */
    $style = $this->entityTypeManager->getStorage('image_style')->load($image_style);
    $file_path = $style->buildUrl($file->getFileUri());

    return $file_path;
  }

  /**
   * Code to create CSS.
   */
  protected function generateStyles(string $selector, string $file_path) {
    // @todo It's possible to pass this off to Twig which would make writing
    // the CSS easier.
    $css = sprintf('%s {', $selector);
    $css .= sprintf('background-image: url(\'%s\');', file_url_transform_relative($file_path));
    $css .= '}';

    return $css;
  }

I'm not sure yet why CSS doesn't trigger the image derivatives.

πŸ› Bug report
Status

Active

Version

1.0

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States jnettik Denver, CO

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    Is the problem happening because the image URL is missing the "itok" security token, thus the system blocks generation of the new image variation? This seems more like a core issue than something specific to Focal Point.

Production build 0.69.0 2024