ImageStylesProvider.php in Consumer Image Styles 8.2
File
src/ImageStylesProvider.php
View source
<?php
namespace Drupal\consumer_image_styles;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\consumers\Entity\Consumer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class ImageStylesProvider implements ImageStylesProviderInterface {
private $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function loadStyles(Consumer $consumer) {
$consumer_config = $consumer
->get('image_styles')
->getValue();
$image_style_ids = array_map(function ($field_value) {
return $field_value['target_id'];
}, $consumer_config);
try {
$image_styles = $this->entityTypeManager
->getStorage('image_style')
->loadMultiple($image_style_ids);
} catch (InvalidPluginDefinitionException $e) {
$image_styles = [];
}
return $image_styles;
}
}