Chris@0: image_style_mappings as &$mapping) { Chris@0: if ($mapping['breakpoint_id'] === $breakpoint_id && $mapping['multiplier'] === $multiplier) { Chris@0: $mapping = [ Chris@0: 'breakpoint_id' => $breakpoint_id, Chris@0: 'multiplier' => $multiplier, Chris@0: ] + $image_style_mapping; Chris@0: $this->keyedImageStyleMappings = NULL; Chris@0: return $this; Chris@0: } Chris@0: } Chris@0: $this->image_style_mappings[] = [ Chris@0: 'breakpoint_id' => $breakpoint_id, Chris@0: 'multiplier' => $multiplier, Chris@0: ] + $image_style_mapping; Chris@0: $this->keyedImageStyleMappings = NULL; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function hasImageStyleMappings() { Chris@0: $mappings = $this->getKeyedImageStyleMappings(); Chris@0: return !empty($mappings); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getKeyedImageStyleMappings() { Chris@0: if (!$this->keyedImageStyleMappings) { Chris@0: $this->keyedImageStyleMappings = []; Chris@0: foreach ($this->image_style_mappings as $mapping) { Chris@0: if (!static::isEmptyImageStyleMapping($mapping)) { Chris@0: $this->keyedImageStyleMappings[$mapping['breakpoint_id']][$mapping['multiplier']] = $mapping; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $this->keyedImageStyleMappings; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getImageStyleMappings() { Chris@0: return $this->image_style_mappings; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setBreakpointGroup($breakpoint_group) { Chris@0: // If the breakpoint group is changed then the image style mappings are Chris@0: // invalid. Chris@0: if ($breakpoint_group !== $this->breakpoint_group) { Chris@0: $this->removeImageStyleMappings(); Chris@0: } Chris@0: $this->breakpoint_group = $breakpoint_group; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getBreakpointGroup() { Chris@0: return $this->breakpoint_group; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setFallbackImageStyle($fallback_image_style) { Chris@0: $this->fallback_image_style = $fallback_image_style; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFallbackImageStyle() { Chris@0: return $this->fallback_image_style; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeImageStyleMappings() { Chris@0: $this->image_style_mappings = []; Chris@0: $this->keyedImageStyleMappings = NULL; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function calculateDependencies() { Chris@0: parent::calculateDependencies(); Chris@0: $providers = \Drupal::service('breakpoint.manager')->getGroupProviders($this->breakpoint_group); Chris@0: foreach ($providers as $provider => $type) { Chris@0: $this->addDependency($type, $provider); Chris@0: } Chris@0: // Extract all the styles from the image style mappings. Chris@0: $styles = ImageStyle::loadMultiple($this->getImageStyleIds()); Chris@0: array_walk($styles, function ($style) { Chris@0: $this->addDependency('config', $style->getConfigDependencyName()); Chris@0: }); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function isEmptyImageStyleMapping(array $image_style_mapping) { Chris@0: if (!empty($image_style_mapping)) { Chris@0: switch ($image_style_mapping['image_mapping_type']) { Chris@0: case 'sizes': Chris@0: // The image style mapping must have a sizes attribute defined and one Chris@0: // or more image styles selected. Chris@0: if ($image_style_mapping['image_mapping']['sizes'] && $image_style_mapping['image_mapping']['sizes_image_styles']) { Chris@0: return FALSE; Chris@0: } Chris@0: break; Chris@0: case 'image_style': Chris@0: // The image style mapping must have an image style selected. Chris@0: if ($image_style_mapping['image_mapping']) { Chris@0: return FALSE; Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: return TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getImageStyleMapping($breakpoint_id, $multiplier) { Chris@0: $map = $this->getKeyedImageStyleMappings(); Chris@0: if (isset($map[$breakpoint_id][$multiplier])) { Chris@0: return $map[$breakpoint_id][$multiplier]; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getImageStyleIds() { Chris@0: $image_styles = [$this->getFallbackImageStyle()]; Chris@0: foreach ($this->getImageStyleMappings() as $image_style_mapping) { Chris@0: // Only image styles of non-empty mappings should be loaded. Chris@0: if (!$this::isEmptyImageStyleMapping($image_style_mapping)) { Chris@0: switch ($image_style_mapping['image_mapping_type']) { Chris@0: case 'image_style': Chris@0: $image_styles[] = $image_style_mapping['image_mapping']; Chris@0: break; Chris@0: case 'sizes': Chris@0: $image_styles = array_merge($image_styles, $image_style_mapping['image_mapping']['sizes_image_styles']); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: return array_values(array_filter(array_unique($image_styles))); Chris@0: } Chris@0: Chris@0: }