class ImageStylesProvider in Consumer Image Styles 4.x
Same name and namespace in other branches
- 8.3 src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider
- 8.2 src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider
Class ImageStylesProvider.
@package Drupal\consumer_image_styles
Hierarchy
- class \Drupal\consumer_image_styles\ImageStylesProvider implements ImageStylesProviderInterface uses StringTranslationTrait
Expanded class hierarchy of ImageStylesProvider
2 files declare their use of ImageStylesProvider
- ConsumerImageSylesFunctionalTest.php in tests/
src/ Functional/ ConsumerImageSylesFunctionalTest.php - LinkCollectionNormalizer.php in src/
Normalizer/ LinkCollectionNormalizer.php
1 string reference to 'ImageStylesProvider'
1 service uses ImageStylesProvider
File
- src/
ImageStylesProvider.php, line 19
Namespace
Drupal\consumer_image_stylesView source
class ImageStylesProvider implements ImageStylesProviderInterface {
use StringTranslationTrait;
const DERIVATIVE_LINK_REL = 'drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative';
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
private $entityTypeManager;
/**
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
private $imageFactory;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ImageFactory $image_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->imageFactory = $image_factory;
}
/**
* {@inheritdoc}
*/
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);
// Load image style entities in bulk.
try {
$image_styles = $this->entityTypeManager
->getStorage('image_style')
->loadMultiple($image_style_ids);
} catch (PluginException $e) {
$image_styles = [];
}
return $image_styles;
}
/**
* {@inheritdoc}
*/
public function buildDerivativeLink($uri, ImageStyleInterface $image_style) {
$image = $this->imageFactory
->get($uri);
$dimensions = [
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
];
$image_style
->transformDimensions($dimensions, $uri);
return [
'href' => file_create_url($image_style
->buildUrl($uri)),
'title' => $this
->t('Image Style: @name', [
'@name' => $image_style
->label(),
]),
'type' => $image
->getMimeType(),
'meta' => [
'rel' => static::DERIVATIVE_LINK_REL,
] + $dimensions,
];
}
/**
* {inheritdoc}
*/
public function entityIsImage(EntityInterface $entity) {
if (!$entity instanceof File) {
return FALSE;
}
return in_array(mb_strtolower(pathinfo($entity
->getFileUri(), PATHINFO_EXTENSION)), $this->imageFactory
->getSupportedExtensions());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ImageStylesProvider:: |
private | property | The entity type manager. | |
ImageStylesProvider:: |
private | property | The image factory. | |
ImageStylesProvider:: |
public | function |
Builds a derivative link based on the image URI and the image style. Overrides ImageStylesProviderInterface:: |
|
ImageStylesProvider:: |
constant | |||
ImageStylesProvider:: |
public | function |
{inheritdoc} Overrides ImageStylesProviderInterface:: |
|
ImageStylesProvider:: |
public | function |
Load the image styles for a given consumer. Overrides ImageStylesProviderInterface:: |
|
ImageStylesProvider:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |