You are here

consumer_image_styles.module in Consumer Image Styles 4.x

Module implementation file.

File

consumer_image_styles.module
View source
<?php

/**
 * @file
 * Module implementation file.
 */
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Implements hook_entity_base_field_info().
 */
function consumer_image_styles_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() == 'consumer') {
    $fields['image_styles'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(new TranslatableMarkup('Image Styles'))
      ->setDescription(new TranslatableMarkup('Image styles this consumer will need. All images will provide all the variants selected here.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'image_style')
      ->setSetting('handler', 'default')
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->setTranslatable(FALSE)
      ->setDisplayOptions('view', [
      'label' => 'inline',
      'type' => 'entity_reference_label',
      'weight' => 5,
    ])
      ->setDisplayOptions('form', [
      'type' => 'options_buttons',
      'weight' => 5,
    ]);
  }
  return $fields;
}

/**
 * Implements hook_consumers_list_alter().
 */
function consumer_image_styles_consumers_list_alter(&$data, $context) {
  if ($context['type'] === 'header') {
    $data['image_styles'] = t('Image Styles');
  }
  elseif ($context['type'] === 'row') {
    $entity = $context['entity'];
    $data['image_styles'] = [
      'data' => [
        '#theme' => 'item_list',
        '#items' => [],
      ],
    ];

    // Add all image style labels.

    /* @var $image_styles_provider \Drupal\consumer_image_styles\ImageStylesProvider */
    $image_styles_provider = \Drupal::service('consumer_image_styles.image_styles_provider');
    $image_styles = $image_styles_provider
      ->loadStyles($entity);
    foreach ($image_styles as $image_style) {
      $data['image_styles']['data']['#items'][] = $image_style
        ->label();
    }
  }
}

Functions