Version 1
[yaffs-website] / web / core / modules / image / src / Form / ImageStyleFormBase.php
1 <?php
2
3 namespace Drupal\image\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Base form for image style add and edit forms.
12  */
13 abstract class ImageStyleFormBase extends EntityForm {
14
15   /**
16    * The entity being used by this form.
17    *
18    * @var \Drupal\image\ImageStyleInterface
19    */
20   protected $entity;
21
22   /**
23    * The image style entity storage.
24    *
25    * @var \Drupal\Core\Entity\EntityStorageInterface
26    */
27   protected $imageStyleStorage;
28
29   /**
30    * Constructs a base class for image style add and edit forms.
31    *
32    * @param \Drupal\Core\Entity\EntityStorageInterface $image_style_storage
33    *   The image style entity storage.
34    */
35   public function __construct(EntityStorageInterface $image_style_storage) {
36     $this->imageStyleStorage = $image_style_storage;
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function create(ContainerInterface $container) {
43     return new static(
44       $container->get('entity.manager')->getStorage('image_style')
45     );
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function form(array $form, FormStateInterface $form_state) {
52
53     $form['label'] = [
54       '#type' => 'textfield',
55       '#title' => $this->t('Image style name'),
56       '#default_value' => $this->entity->label(),
57       '#required' => TRUE,
58     ];
59     $form['name'] = [
60       '#type' => 'machine_name',
61       '#machine_name' => [
62         'exists' => [$this->imageStyleStorage, 'load'],
63       ],
64       '#default_value' => $this->entity->id(),
65       '#required' => TRUE,
66     ];
67
68     return parent::form($form, $form_state);
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function save(array $form, FormStateInterface $form_state) {
75     parent::save($form, $form_state);
76     $form_state->setRedirectUrl($this->entity->urlInfo('edit-form'));
77   }
78
79 }