Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / image / src / Form / ImageStyleDeleteForm.php
1 <?php
2
3 namespace Drupal\image\Form;
4
5 use Drupal\Core\Entity\EntityDeleteForm;
6 use Drupal\Core\Form\FormStateInterface;
7
8 /**
9  * Creates a form to delete an image style.
10  */
11 class ImageStyleDeleteForm extends EntityDeleteForm {
12
13   /**
14    * Replacement options.
15    *
16    * @var array
17    */
18   protected $replacementOptions;
19
20   /**
21    * {@inheritdoc}
22    */
23   public function getQuestion() {
24     return $this->t('Optionally select a style before deleting %style', ['%style' => $this->entity->label()]);
25   }
26   /**
27    * {@inheritdoc}
28    */
29   public function getDescription() {
30     if (count($this->getReplacementOptions()) > 1) {
31       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.');
32     }
33     return $this->t('All images that have been generated for this style will be permanently deleted. The dependent configurations might need manual reconfiguration.');
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function form(array $form, FormStateInterface $form_state) {
40     $replacement_styles = $this->getReplacementOptions();
41     // If there are non-empty options in the list, allow the user to optionally
42     // pick up a replacement.
43     if (count($replacement_styles) > 1) {
44       $form['replacement'] = [
45         '#type' => 'select',
46         '#title' => $this->t('Replacement style'),
47         '#options' => $replacement_styles,
48         '#empty_option' => $this->t('- No replacement -'),
49         '#weight' => -5,
50       ];
51     }
52
53     return parent::form($form, $form_state);
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function submitForm(array &$form, FormStateInterface $form_state) {
60     // Save a selected replacement in the image style storage. It will be used
61     // later, in the same request, when resolving dependencies.
62     if ($replacement = $form_state->getValue('replacement')) {
63       /** @var \Drupal\image\ImageStyleStorageInterface $storage */
64       $storage = $this->entityTypeManager->getStorage($this->entity->getEntityTypeId());
65       $storage->setReplacementId($this->entity->id(), $replacement);
66     }
67     parent::submitForm($form, $form_state);
68   }
69
70   /**
71    * Returns a list of image style replacement options.
72    *
73    * @return array
74    *   An option list suitable for the form select '#options'.
75    */
76   protected function getReplacementOptions() {
77     if (!isset($this->replacementOptions)) {
78       $this->replacementOptions = array_diff_key(image_style_options(), [$this->getEntity()->id() => '']);
79     }
80     return $this->replacementOptions;
81   }
82
83 }