View source
<?php
namespace Drupal\iek\Plugin\ImageEffect;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\image\ConfigurableImageEffectBase;
class ImageOverlayEffect extends ConfigurableImageEffectBase {
public function applyEffect(ImageInterface $image) {
if (!$image
->apply('iek_image_overlay', $this->configuration)) {
$this->logger
->error('IEK - Overlay failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', [
'%toolkit' => $image
->getToolkitId(),
'%path' => $image
->getSource(),
'%mimetype' => $image
->getMimeType(),
'%dimensions' => $image
->getWidth() . 'x' . $image
->getHeight(),
]);
return FALSE;
}
return TRUE;
}
public function transformDimensions(array &$dimensions, $uri) {
$dimensions['overlay_name'] = $this->configuration['overlay_name'];
$dimensions['overlay_offset'] = $this->configuration['overlay_offset'];
$dimensions['bg_offset'] = $this->configuration['bg_offset'];
}
public function getSummary() {
$data = $this->configuration;
$iek_overlays = iek_get_overlays();
foreach ($iek_overlays as $item1) {
foreach ($item1['children'] as $item2) {
if ($item2['name'] == $data['overlay_name']) {
$data['overlay_name'] = $item2['title'];
}
}
}
$summary = [
'#theme' => 'iek_image_overlay_summary',
'#data' => $data,
];
$summary += parent::getSummary();
return $summary;
}
public function defaultConfiguration() {
return [
'overlay_name' => '',
'overlay_offset' => 0,
'bg_offset' => 0,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$iek_overlays = iek_get_overlays();
$overlay_names = [];
foreach ($iek_overlays as $item1) {
foreach ($item1['children'] as $item2) {
$overlay_names[$item1['name']][$item2['name']] = $item2['title'];
}
}
$form['overlay_name'] = [
'#type' => 'select',
'#title' => $this
->t('Overlay name'),
'#default_value' => $this->configuration['overlay_name'],
'#options' => $overlay_names,
'#required' => TRUE,
];
$form['overlay_offset'] = [
'#type' => 'number',
'#title' => $this
->t('Overlay offset'),
'#default_value' => $this->configuration['overlay_offset'],
'#field_suffix' => ' ' . $this
->t('pixels'),
'#required' => TRUE,
];
$form['bg_offset'] = [
'#type' => 'number',
'#title' => $this
->t('Background offset'),
'#default_value' => $this->configuration['bg_offset'],
'#field_suffix' => ' ' . $this
->t('pixels'),
'#required' => TRUE,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['overlay_name'] = $form_state
->getValue('overlay_name');
$this->configuration['overlay_offset'] = $form_state
->getValue('overlay_offset');
$this->configuration['bg_offset'] = $form_state
->getValue('bg_offset');
}
}