View source
<?php
namespace Drupal\imageapi_optimize\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\imageapi_optimize\ConfigurableImageAPIOptimizeProcessorInterface;
use Drupal\imageapi_optimize\ImageAPIOptimizePipelineInterface;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class ImageAPIOptimizeProcessorFormBase extends FormBase {
protected $imageAPIOptimizePipeline;
protected $imageAPIOptimizeProcessor;
public function getFormId() {
return 'imageapi_optimize_processor_form';
}
public function buildForm(array $form, FormStateInterface $form_state, ImageAPIOptimizePipelineInterface $imageapi_optimize_pipeline = NULL, $imageapi_optimize_processor = NULL) {
$this->imageAPIOptimizePipeline = $imageapi_optimize_pipeline;
try {
$this->imageAPIOptimizeProcessor = $this
->prepareImageAPIOptimizeProcessor($imageapi_optimize_processor);
} catch (PluginNotFoundException $e) {
throw new NotFoundHttpException("Invalid processor id: '{$imageapi_optimize_processor}'.");
}
$request = $this
->getRequest();
if (!$this->imageAPIOptimizeProcessor instanceof ConfigurableImageAPIOptimizeProcessorInterface) {
throw new NotFoundHttpException();
}
$form['#attached']['library'][] = 'imageapi_optimize/admin';
$form['uuid'] = [
'#type' => 'value',
'#value' => $this->imageAPIOptimizeProcessor
->getUuid(),
];
$form['id'] = [
'#type' => 'value',
'#value' => $this->imageAPIOptimizeProcessor
->getPluginId(),
];
$form['data'] = [];
$subform_state = SubformState::createForSubform($form['data'], $form, $form_state);
$form['data'] = $this->imageAPIOptimizeProcessor
->buildConfigurationForm($form['data'], $subform_state);
$form['data']['#tree'] = TRUE;
$form['weight'] = [
'#type' => 'hidden',
'#value' => $request->query
->has('weight') ? (int) $request->query
->get('weight') : $this->imageAPIOptimizeProcessor
->getWeight(),
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => $this->imageAPIOptimizePipeline
->toUrl('edit-form'),
'#attributes' => [
'class' => [
'button',
],
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->imageAPIOptimizeProcessor
->validateConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
$this->imageAPIOptimizeProcessor
->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
$this->imageAPIOptimizeProcessor
->setWeight($form_state
->getValue('weight'));
if (!$this->imageAPIOptimizeProcessor
->getUuid()) {
$this->imageAPIOptimizePipeline
->addProcessor($this->imageAPIOptimizeProcessor
->getConfiguration());
}
$this->imageAPIOptimizePipeline
->save();
$this
->messenger()
->addMessage($this
->t('The Image Optimize processor was successfully applied.'));
$form_state
->setRedirectUrl($this->imageAPIOptimizePipeline
->toUrl('edit-form'));
}
protected abstract function prepareImageAPIOptimizeProcessor($imageapi_optimize_processor);
}