View source
<?php
namespace Drupal\image_effects\Plugin\image_effects\ImageSelector;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Url;
use Drupal\image_effects\Plugin\ImageEffectsPluginBase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Dropdown extends ImageEffectsPluginBase {
protected $imageFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, LoggerInterface $logger, ImageFactory $image_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $config_factory, $logger);
$this->imageFactory = $image_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('logger.channel.image_effects'), $container
->get('image.factory'));
}
public function defaultConfiguration() {
return [
'path' => '',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state, array $ajax_settings = []) {
$element['path'] = [
'#type' => 'textfield',
'#title' => $this
->t('Path'),
'#default_value' => $this->configuration['path'],
'#element_validate' => [
[
$this,
'validatePath',
],
],
'#maxlength' => 255,
'#description' => $this
->t('Location of the directory where the background images are stored.') . ' ' . $this
->t('Relative paths will be resolved relative to the Drupal installation directory.'),
];
return $element;
}
public function validatePath($element, FormStateInterface $form_state, $form) {
if (!is_dir($element['#value'])) {
$form_state
->setErrorByName(implode('][', $element['#parents']), $this
->t('Invalid directory specified.'));
}
}
public function selectionElement(array $options = []) {
$image_files = $this
->getList();
if (empty($image_files)) {
drupal_set_message($this
->t('No images available. Make sure at least one image is available in the directory specified in the <a href=":url">configuration page</a>.', [
':url' => Url::fromRoute('image_effects.settings')
->toString(),
]), 'warning');
}
$options['#default_value'] = isset($options['#default_value']) ? pathinfo($options['#default_value'], PATHINFO_BASENAME) : '';
return array_merge([
'#type' => 'select',
'#title' => $this
->t('Image'),
'#description' => $this
->t('Select an image.'),
'#options' => array_combine($image_files, $image_files),
'#element_validate' => [
[
$this,
'validateSelectorUri',
],
],
], $options);
}
public function validateSelectorUri($element, FormStateInterface $form_state, $form) {
if (!empty($element['#value'])) {
if (file_exists($file_path = $this->configuration['path'] . '/' . $element['#value'])) {
$form_state
->setValueForElement($element, $file_path);
}
else {
$form_state
->setErrorByName(implode('][', $element['#parents']), $this
->t('The selected file does not exist.'));
}
}
}
protected function getList() {
$filelist = [];
if (is_dir($this->configuration['path']) && ($handle = opendir($this->configuration['path']))) {
while ($file = readdir($handle)) {
$extension = pathinfo($file, PATHINFO_EXTENSION);
if (in_array($extension, $this->imageFactory
->getSupportedExtensions())) {
$filelist[] = $file;
}
}
closedir($handle);
}
return $filelist;
}
}