ImageAPIOptimizePipelineDeleteForm.php in Image Optimize (or ImageAPI Optimize) 8.2
File
src/Form/ImageAPIOptimizePipelineDeleteForm.php
View source
<?php
namespace Drupal\imageapi_optimize\Form;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
class ImageAPIOptimizePipelineDeleteForm extends EntityDeleteForm {
protected $replacementOptions;
public function getQuestion() {
return $this
->t('Optionally select a pipeline before deleting %pipeline', [
'%pipeline' => $this->entity
->label(),
]);
}
public function getDescription() {
if (count($this
->getReplacementOptions()) > 1) {
return $this
->t('If this pipeline is in use on the site, you may select another pipeline to replace it. If no replacement pipeline is selected, the dependent configurations might need manual reconfiguration.');
}
return $this
->t('The dependent configurations might need manual reconfiguration.');
}
public function form(array $form, FormStateInterface $form_state) {
$replacement_pipelines = $this
->getReplacementOptions();
if (count($replacement_pipelines) > 1) {
$form['replacement'] = [
'#type' => 'select',
'#title' => $this
->t('Replacement pipeline'),
'#options' => $replacement_pipelines,
'#empty_option' => $this
->t('- No replacement -'),
'#weight' => -5,
];
}
return parent::form($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($replacement = $form_state
->getValue('replacement')) {
$storage = $this->entityTypeManager
->getStorage($this->entity
->getEntityTypeId());
$storage
->setReplacementId($this->entity
->id(), $replacement);
}
parent::submitForm($form, $form_state);
}
protected function getReplacementOptions() {
if (!isset($this->replacementOptions)) {
$this->replacementOptions = array_diff_key(imageapi_optimize_pipeline_options(), [
$this
->getEntity()
->id() => '',
]);
}
return $this->replacementOptions;
}
}