You are here

class ImageStylesProvider in Consumer Image Styles 8.2

Same name and namespace in other branches
  1. 8.3 src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider
  2. 4.x src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider

Class ImageStylesProvider.

@package Drupal\consumer_image_styles

Hierarchy

Expanded class hierarchy of ImageStylesProvider

1 string reference to 'ImageStylesProvider'
consumer_image_styles.services.yml in ./consumer_image_styles.services.yml
consumer_image_styles.services.yml
1 service uses ImageStylesProvider
consumer_image_styles.image_styles_provider in ./consumer_image_styles.services.yml
Drupal\consumer_image_styles\ImageStylesProvider

File

src/ImageStylesProvider.php, line 14

Namespace

Drupal\consumer_image_styles
View source
class ImageStylesProvider implements ImageStylesProviderInterface {
  private $entityTypeManager;
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   *
   * @param Consumer $consumer
   *   Consumer entity to load image styles for.
   *
   * @return \Drupal\image\Entity\ImageStyle[]
   *   List of image styles keyed by image style id.
   */
  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 (InvalidPluginDefinitionException $e) {
      $image_styles = [];
    }
    return $image_styles;
  }

}

Members