View source
<?php
namespace Drupal\imageapi_optimize\Form;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\imageapi_optimize\ConfigurableImageAPIOptimizeProcessorInterface;
use Drupal\imageapi_optimize\ImageAPIOptimizeProcessorManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ImageAPIOptimizePipelineEditForm extends ImageAPIOptimizePipelineFormBase {
protected $imageAPIOptimizeProcessorManager;
public function __construct(EntityStorageInterface $imageapi_optimize_pipeline_storage, ImageAPIOptimizeProcessorManager $imageapi_optimize_processor_manager) {
parent::__construct($imageapi_optimize_pipeline_storage);
$this->imageAPIOptimizeProcessorManager = $imageapi_optimize_processor_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('imageapi_optimize_pipeline'), $container
->get('plugin.manager.imageapi_optimize.processor'));
}
public function form(array $form, FormStateInterface $form_state) {
$user_input = $form_state
->getUserInput();
$form['#title'] = $this
->t('Edit pipeline %name', [
'%name' => $this->entity
->label(),
]);
$form['#tree'] = TRUE;
$form['#attached']['library'][] = 'imageapi_optimize/admin';
$form['processors'] = [
'#type' => 'table',
'#header' => [
$this
->t('Processor'),
$this
->t('Weight'),
$this
->t('Operations'),
],
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'image-processor-order-weight',
],
],
'#attributes' => [
'id' => 'image-pipeline-processors',
],
'#empty' => t('There are currently no processors in this pipeline. Add one by selecting an option below.'),
'#weight' => 5,
];
foreach ($this->entity
->getProcessors() as $processor) {
$key = $processor
->getUuid();
$form['processors'][$key]['#attributes']['class'][] = 'draggable';
$form['processors'][$key]['#weight'] = isset($user_input['processors']) ? $user_input['processors'][$key]['weight'] : NULL;
$form['processors'][$key]['processor'] = [
'#tree' => FALSE,
'data' => [
'label' => [
'#plain_text' => $processor
->label(),
],
],
];
$summary = $processor
->getSummary();
if (!empty($summary)) {
$summary['#prefix'] = ' ';
$form['processors'][$key]['processor']['data']['summary'] = $summary;
}
$form['processors'][$key]['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for @title', [
'@title' => $processor
->label(),
]),
'#title_display' => 'invisible',
'#default_value' => $processor
->getWeight(),
'#attributes' => [
'class' => [
'image-processor-order-weight',
],
],
];
$links = [];
$is_configurable = $processor instanceof ConfigurableImageAPIOptimizeProcessorInterface;
if ($is_configurable) {
$links['edit'] = [
'title' => $this
->t('Edit'),
'url' => Url::fromRoute('imageapi_optimize.processor_edit_form', [
'imageapi_optimize_pipeline' => $this->entity
->id(),
'imageapi_optimize_processor' => $key,
]),
];
}
$links['delete'] = [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('imageapi_optimize.processor_delete', [
'imageapi_optimize_pipeline' => $this->entity
->id(),
'imageapi_optimize_processor' => $key,
]),
];
$form['processors'][$key]['operations'] = [
'#type' => 'operations',
'#links' => $links,
];
}
$new_processor_options = [];
$processors = $this->imageAPIOptimizeProcessorManager
->getDefinitions();
uasort($processors, function ($a, $b) {
return strcasecmp($a['id'], $b['id']);
});
foreach ($processors as $processor => $definition) {
$new_processor_options[$processor] = $definition['label'];
}
$form['processors']['new'] = [
'#tree' => FALSE,
'#weight' => isset($user_input['weight']) ? $user_input['weight'] : NULL,
'#attributes' => [
'class' => [
'draggable',
],
],
];
$form['processors']['new']['processor'] = [
'data' => [
'new' => [
'#type' => 'select',
'#title' => $this
->t('Processor'),
'#title_display' => 'invisible',
'#options' => $new_processor_options,
'#empty_option' => $this
->t('Select a new processor'),
],
[
'add' => [
'#type' => 'submit',
'#value' => $this
->t('Add'),
'#validate' => [
'::processorValidate',
],
'#submit' => [
'::submitForm',
'::processorSave',
],
],
],
],
'#prefix' => '<div class="image-pipeline-new">',
'#suffix' => '</div>',
];
$form['processors']['new']['weight'] = [
'#type' => 'weight',
'#title' => $this
->t('Weight for new processor'),
'#title_display' => 'invisible',
'#default_value' => count($this->entity
->getProcessors()) + 1,
'#attributes' => [
'class' => [
'image-processor-order-weight',
],
],
];
$form['processors']['new']['operations'] = [
'data' => [],
];
return parent::form($form, $form_state);
}
public function processorValidate($form, FormStateInterface $form_state) {
if (!$form_state
->getValue('new')) {
$form_state
->setErrorByName('new', $this
->t('Select a processor to add.'));
}
}
public function processorSave($form, FormStateInterface $form_state) {
$this
->save($form, $form_state);
$processor = $this->imageAPIOptimizeProcessorManager
->getDefinition($form_state
->getValue('new'));
if (is_subclass_of($processor['class'], '\\Drupal\\imageapi_optimize\\ConfigurableImageAPIOptimizeProcessorInterface')) {
$form_state
->setRedirect('imageapi_optimize.processor_add_form', [
'imageapi_optimize_pipeline' => $this->entity
->id(),
'imageapi_optimize_processor' => $form_state
->getValue('new'),
], [
'query' => [
'weight' => $form_state
->getValue('weight'),
],
]);
}
else {
$processor = [
'id' => $processor['id'],
'data' => [],
'weight' => $form_state
->getValue('weight'),
];
$processor_id = $this->entity
->addProcessor($processor);
$this->entity
->save();
if (!empty($processor_id)) {
$this
->messenger()
->addMessage($this
->t('The Image Optimize processor was successfully applied.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->isValueEmpty('processors')) {
$this
->updateProcessorWeights($form_state
->getValue('processors'));
}
parent::submitForm($form, $form_state);
}
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$this
->messenger()
->addMessage($this
->t('Changes to the pipeline have been saved.'));
}
public function actions(array $form, FormStateInterface $form_state) {
$actions = parent::actions($form, $form_state);
$actions['submit']['#value'] = $this
->t('Update pipeline');
return $actions;
}
protected function updateProcessorWeights(array $processors) {
foreach ($processors as $uuid => $processor_data) {
if ($this->entity
->getProcessors()
->has($uuid)) {
$this->entity
->getProcessor($uuid)
->setWeight($processor_data['weight']);
}
}
}
}