ImageStyleDeleteForm.php in Drupal 10
File
core/modules/image/src/Form/ImageStyleDeleteForm.php
View source
<?php
namespace Drupal\image\Form;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
class ImageStyleDeleteForm extends EntityDeleteForm {
protected $replacementOptions;
public function getQuestion() {
return $this
->t('Optionally select a style before deleting %style', [
'%style' => $this->entity
->label(),
]);
}
public function getDescription() {
if (count($this
->getReplacementOptions()) > 1) {
return $this
->t('If this style is in use on the site, you may select another style to replace it. All images that have been generated for this style will be permanently deleted. If no replacement style is selected, the dependent configurations might need manual reconfiguration.');
}
return $this
->t('All images that have been generated for this style will be permanently deleted. The dependent configurations might need manual reconfiguration.');
}
public function form(array $form, FormStateInterface $form_state) {
$replacement_styles = $this
->getReplacementOptions();
if (count($replacement_styles) > 1) {
$form['replacement'] = [
'#type' => 'select',
'#title' => $this
->t('Replacement style'),
'#options' => $replacement_styles,
'#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(image_style_options(), [
$this
->getEntity()
->id() => '',
]);
}
return $this->replacementOptions;
}
}