You are here

consumer_image_styles.module in Consumer Image Styles 8

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');
  }
  else {
    if ($context['type'] === 'row') {
      $entity = $context['entity'];
      $data['image_styles'] = [
        'data' => [
          '#theme' => 'item_list',
          '#items' => [],
        ],
      ];
      foreach ($entity
        ->get('image_styles') as $image_style) {
        $data['image_styles']['data']['#items'][] = $image_style->entity
          ->label();
      }
    }
  }
}

Functions