Completed
Push — 8.x-1.x ( faacfb...55150c )
by Janez
02:54
created

ImageThumbnail::isApplicable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Contains \Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay\ImageThumbnail.
5
 */
6
7
namespace Drupal\entity_browser\Plugin\EntityBrowser\FieldWidgetDisplay;
8
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Entity\EntityManagerInterface;
11
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
14
use Drupal\file\FileInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Drupal\entity_browser\FieldWidgetDisplayBase;
17
18
/**
19
 * Displays image thumbnail
20
 *
21
 * @EntityBrowserFieldWidgetDisplay(
22
 *   id = "thumbnail",
23
 *   label = @Translation("Image thumbnail"),
24
 *   description = @Translation("Displays image files as thumbnails")
25
 * )
26
 */
27
class ImageThumbnail extends FieldWidgetDisplayBase implements ContainerFactoryPluginInterface {
28
29
  /**
30
   * Entity manager service.
31
   *
32
   * @var \Drupal\Core\Entity\EntityManagerInterface
33
   */
34
  protected $entityManager;
35
36
  /**
37
   * Constructs widget plugin.
38
   *
39
   * @param array $configuration
40
   *   A configuration array containing information about the plugin instance.
41
   * @param string $plugin_id
42
   *   The plugin_id for the plugin instance.
43
   * @param mixed $plugin_definition
44
   *   The plugin implementation definition.
45
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
46
   *   Entity manager service.
47
   */
48
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager) {
49
    parent::__construct($configuration, $plugin_id, $plugin_definition);
50
    $this->entityManager = $entity_manager;
51
  }
52
53
  /**
54
   * {@inheritdoc}
55
   */
56
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
57
    return new static(
58
      $configuration,
59
      $plugin_id,
60
      $plugin_definition,
61
      $container->get('entity.manager')
62
    );
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public function view(EntityInterface $entity) {
69
    return [
70
      '#theme' => 'image_style',
71
      '#style_name' => $this->configuration['image_style'],
72
      '#title' => $entity->label(),
73
      '#alt' => $entity->label(),
74
      '#uri' => $entity->getFileUri(),
75
    ];
76
  }
77
78
  /**
79
   * {@inheritdoc}
80
   */
81
  public function settingsForm(array $form, FormStateInterface $form_state) {
82
    $options = [];
83
    foreach ($this->entityManager->getStorage('image_style')->loadMultiple() as $id => $image_style) {
84
      $options[$id] = $image_style->label();
85
    }
86
87
    return [
88
      'image_style' => [
89
        '#type' => 'select',
90
        '#title' => t('Image style'),
91
        '#description' => t('Select image style to be used to display thumbnails.'),
92
        '#default_value' => $this->configuration['image_style'],
93
        '#options' => $options,
94
      ],
95
    ];
96
  }
97
98
  /**
99
   * {@inheritdoc}
100
   */
101
  public function isApplicable(EntityTypeInterface $entity_type) {
102
    return $entity_type->isSubclassOf(FileInterface::class);
103
  }
104
105
}
106