You are here

public function BlazyDependenciesTrait::calculateDependencies in Blazy 8.2

File

src/Dejavu/BlazyDependenciesTrait.php, line 13

Class

BlazyDependenciesTrait
A Trait common for file, image or media to handle dependencies.

Namespace

Drupal\blazy\Dejavu

Code

public function calculateDependencies() {
  $dependencies = parent::calculateDependencies();
  $style_ids = [];
  foreach ([
    'box',
    'box_media',
    'image',
    'thumbnail',
  ] as $key) {
    if (!empty($this
      ->getSetting($key . '_style'))) {
      $style_ids[] = $this
        ->getSetting($key . '_style');
    }
  }

  /** @var \Drupal\image\ImageStyleInterface $style */
  foreach ($style_ids as $style_id) {
    if ($style_id && ($style = $this->formatter
      ->entityLoad($style_id, 'image_style'))) {

      // If this formatter uses a valid image style to display the image, add
      // the image style configuration entity as dependency of this formatter.
      $dependencies[$style
        ->getConfigDependencyKey()][] = $style
        ->getConfigDependencyName();
    }
  }
  if ($this->formatter
    ->getModuleHandler()
    ->moduleExists('responsive_image')) {
    foreach ([
      'box',
      'responsive_image',
    ] as $key) {
      $style_id = $this
        ->getSetting($key . '_style');

      /** @var \Drupal\responsive_image\ResponsiveImageStyleInterface $style */
      if ($style_id && ($style = $this->formatter
        ->entityLoad($style_id, 'responsive_image_style'))) {

        // Add the responsive image style as dependency.
        $dependencies[$style
          ->getConfigDependencyKey()][] = $style
          ->getConfigDependencyName();
      }
    }
  }
  return $dependencies;
}