Completed
Push — 8.x-1.x ( 02125d...d2ee20 )
by Alexandre
02:11
created

CropEffect::getSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\crop\Plugin\ImageEffect;
4
5
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\Core\Image\ImageFactory;
8
use Drupal\Core\Image\ImageInterface;
9
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
10
use Drupal\crop\CropInterface;
11
use Drupal\crop\CropStorageInterface;
12
use Drupal\crop\Entity\Crop;
13
use Drupal\image\ConfigurableImageEffectBase;
14
use Psr\Log\LoggerInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * Crops an image resource.
19
 *
20
 * @ImageEffect(
21
 *   id = "crop_crop",
22
 *   label = @Translation("Manual crop"),
23
 *   description = @Translation("Applies manually provided crop to the image.")
24
 * )
25
 */
26
class CropEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
27
28
  /**
29
   * Crop entity storage.
30
   *
31
   * @var \Drupal\crop\CropStorageInterface
32
   */
33
  protected $storage;
34
35
  /**
36
   * Crop type entity storage.
37
   *
38
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
39
   */
40
  protected $typeStorage;
41
42
  /**
43
   * Crop entity.
44
   *
45
   * @var \Drupal\crop\CropInterface
46
   */
47
  protected $crop;
48
49
  /**
50
   * The image factory service.
51
   *
52
   * @var \Drupal\Core\Image\ImageFactory
53
   */
54
  protected $imageFactory;
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, CropStorageInterface $crop_storage, ConfigEntityStorageInterface $crop_type_storage, ImageFactory $image_factory) {
60
    parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
61
    $this->storage = $crop_storage;
62
    $this->typeStorage = $crop_type_storage;
63
    $this->imageFactory = $image_factory;
64
  }
65
66
  /**
67
   * {@inheritdoc}
68
   */
69
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
70
    return new static(
71
      $configuration,
72
      $plugin_id,
73
      $plugin_definition,
74
      $container->get('logger.factory')->get('image'),
75
      $container->get('entity_type.manager')->getStorage('crop'),
76
      $container->get('entity_type.manager')->getStorage('crop_type'),
77
      $container->get('image.factory')
78
    );
79
  }
80
81
  /**
82
   * {@inheritdoc}
83
   */
84
  public function applyEffect(ImageInterface $image) {
85
    if (empty($this->configuration['crop_type']) || !$this->typeStorage->load($this->configuration['crop_type'])) {
86
      $this->logger->error('Manual image crop failed due to misconfigured crop type on %path.', ['%path' => $image->getSource()]);
87
      return FALSE;
88
    }
89
90
    if ($crop = $this->getCrop($image)) {
91
      $anchor = $crop->anchor();
92
      $size = $crop->size();
93
94
      if (!$image->crop($anchor['x'], $anchor['y'], $size['width'], $size['height'])) {
95
        $this->logger->error('Manual image crop failed using the %toolkit toolkit on %path (%mimetype, %width x %height)', [
96
          '%toolkit' => $image->getToolkitId(),
97
          '%path' => $image->getSource(),
98
          '%mimetype' => $image->getMimeType(),
99
          '%width' => $image->getWidth(),
100
          '%height' => $image->getHeight(),
101
        ]
102
        );
103
        return FALSE;
104
      }
105
    }
106
107
    return TRUE;
108
  }
109
110
  /**
111
   * {@inheritdoc}
112
   */
113
  public function getSummary() {
114
    $summary = [
115
      '#theme' => 'crop_crop_summary',
116
      '#data' => $this->configuration,
117
    ];
118
    $summary += parent::getSummary();
119
120
    return $summary;
121
  }
122
123
  /**
124
   * {@inheritdoc}
125
   */
126
  public function defaultConfiguration() {
127
    return parent::defaultConfiguration() + [
128
      'crop_type' => NULL,
129
    ];
130
  }
131
132
  /**
133
   * {@inheritdoc}
134
   */
135
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
136
    $options = [];
137
    foreach ($this->typeStorage->loadMultiple() as $type) {
138
      $options[$type->id()] = $type->label();
139
    }
140
141
    $form['crop_type'] = [
142
      '#type' => 'select',
143
      '#title' => t('Crop type'),
144
      '#default_value' => $this->configuration['crop_type'],
145
      '#options' => $options,
146
      '#description' => t('Crop type to be used for the image style.'),
147
    ];
148
149
    return $form;
150
  }
151
152
  /**
153
   * {@inheritdoc}
154
   */
155
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
156
    parent::submitConfigurationForm($form, $form_state);
157
    $this->configuration['crop_type'] = $form_state->getValue('crop_type');
158
  }
159
160
  /**
161
   * Gets crop entity for the image.
162
   *
163
   * @param ImageInterface $image
164
   *   Image object.
165
   *
166
   * @return \Drupal\Core\Entity\EntityInterface|\Drupal\crop\CropInterface|false
167
   *   Crop entity or FALSE if crop doesn't exist.
168
   */
169
  protected function getCrop(ImageInterface $image) {
170
    if (!isset($this->crop)) {
171
      $this->crop = FALSE;
0 ignored issues
show
Documentation Bug introduced by
It seems like FALSE of type false is incompatible with the declared type object<Drupal\crop\CropInterface> of property $crop.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
172
      if ($crop = Crop::findCrop($image->getSource(), $this->configuration['crop_type'])) {
173
        $this->crop = $crop;
174
      }
175
    }
176
177
    return $this->crop;
178
  }
179
180
  /**
181
   * {@inheritdoc}
182
   */
183
  public function transformDimensions(array &$dimensions, $uri) {
184
    /** @var \Drupal\Core\Image\Image $image */
185
    $image = $this->imageFactory->get($uri);
186
187
    /** @var \Drupal\crop\CropInterface $crop */
188
    $crop = $this->getCrop($image);
189
    if (!$crop instanceof CropInterface) {
190
      return;
191
    }
192
    $size = $crop->size();
193
194
    // The new image will have the exact dimensions defined for the crop effect.
195
    $dimensions['width'] = $size['width'];
196
    $dimensions['height'] = $size['height'];
197
  }
198
199
}
200