ResponsiveImageStyle.php in Zircon Profile 8.0
File
core/modules/responsive_image/src/Entity/ResponsiveImageStyle.php
View source
<?php
namespace Drupal\responsive_image\Entity;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\image\Entity\ImageStyle;
use Drupal\responsive_image\ResponsiveImageStyleInterface;
class ResponsiveImageStyle extends ConfigEntityBase implements ResponsiveImageStyleInterface {
protected $id;
protected $label;
protected $image_style_mappings = array();
protected $keyedImageStyleMappings;
protected $breakpoint_group = '';
protected $fallback_image_style = '';
public function __construct(array $values, $entity_type_id = 'responsive_image_style') {
parent::__construct($values, $entity_type_id);
}
public function addImageStyleMapping($breakpoint_id, $multiplier, array $image_style_mapping) {
foreach ($this->image_style_mappings as &$mapping) {
if ($mapping['breakpoint_id'] === $breakpoint_id && $mapping['multiplier'] === $multiplier) {
$mapping = array(
'breakpoint_id' => $breakpoint_id,
'multiplier' => $multiplier,
) + $image_style_mapping;
$this->keyedImageStyleMappings = NULL;
return $this;
}
}
$this->image_style_mappings[] = array(
'breakpoint_id' => $breakpoint_id,
'multiplier' => $multiplier,
) + $image_style_mapping;
$this->keyedImageStyleMappings = NULL;
return $this;
}
public function hasImageStyleMappings() {
$mappings = $this
->getKeyedImageStyleMappings();
return !empty($mappings);
}
public function getKeyedImageStyleMappings() {
if (!$this->keyedImageStyleMappings) {
$this->keyedImageStyleMappings = array();
foreach ($this->image_style_mappings as $mapping) {
if (!static::isEmptyImageStyleMapping($mapping)) {
$this->keyedImageStyleMappings[$mapping['breakpoint_id']][$mapping['multiplier']] = $mapping;
}
}
}
return $this->keyedImageStyleMappings;
}
public function getImageStyleMappings() {
return $this->image_style_mappings;
}
public function setBreakpointGroup($breakpoint_group) {
if ($breakpoint_group !== $this->breakpoint_group) {
$this
->removeImageStyleMappings();
}
$this->breakpoint_group = $breakpoint_group;
return $this;
}
public function getBreakpointGroup() {
return $this->breakpoint_group;
}
public function setFallbackImageStyle($fallback_image_style) {
$this->fallback_image_style = $fallback_image_style;
return $this;
}
public function getFallbackImageStyle() {
return $this->fallback_image_style;
}
public function removeImageStyleMappings() {
$this->image_style_mappings = array();
$this->keyedImageStyleMappings = NULL;
return $this;
}
public function calculateDependencies() {
parent::calculateDependencies();
$providers = \Drupal::service('breakpoint.manager')
->getGroupProviders($this->breakpoint_group);
foreach ($providers as $provider => $type) {
$this
->addDependency($type, $provider);
}
$styles = ImageStyle::loadMultiple($this
->getImageStyleIds());
array_walk($styles, function ($style) {
$this
->addDependency('config', $style
->getConfigDependencyName());
});
return $this;
}
public static function isEmptyImageStyleMapping(array $image_style_mapping) {
if (!empty($image_style_mapping)) {
switch ($image_style_mapping['image_mapping_type']) {
case 'sizes':
if ($image_style_mapping['image_mapping']['sizes'] && $image_style_mapping['image_mapping']['sizes_image_styles']) {
return FALSE;
}
break;
case 'image_style':
if ($image_style_mapping['image_mapping']) {
return FALSE;
}
break;
}
}
return TRUE;
}
public function getImageStyleMapping($breakpoint_id, $multiplier) {
$map = $this
->getKeyedImageStyleMappings();
if (isset($map[$breakpoint_id][$multiplier])) {
return $map[$breakpoint_id][$multiplier];
}
}
public function getImageStyleIds() {
$image_styles = [
$this
->getFallbackImageStyle(),
];
foreach ($this
->getImageStyleMappings() as $image_style_mapping) {
if (!$this::isEmptyImageStyleMapping($image_style_mapping)) {
switch ($image_style_mapping['image_mapping_type']) {
case 'image_style':
$image_styles[] = $image_style_mapping['image_mapping'];
break;
case 'sizes':
$image_styles = array_merge($image_styles, $image_style_mapping['image_mapping']['sizes_image_styles']);
break;
}
}
}
return array_values(array_filter(array_unique($image_styles)));
}
}