Version 1
[yaffs-website] / web / modules / contrib / crop / src / Form / CropTypeForm.php
1 <?php
2
3 namespace Drupal\crop\Form;
4
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Link;
8 use Drupal\Core\Url;
9 use Symfony\Component\Validator\ConstraintViolationListInterface;
10
11 /**
12  * Form controller for crop type forms.
13  */
14 class CropTypeForm extends EntityForm {
15
16   /**
17    * {@inheritdoc}
18    */
19   public function form(array $form, FormStateInterface $form_state) {
20     $form = parent::form($form, $form_state);
21
22     $type = $this->entity;
23     $form['#title'] = $this->operation == 'add' ? $this->t('Add crop type')
24         :
25         $this->t('Edit %label crop type', array('%label' => $type->label()));
26
27     $form['label'] = [
28       '#title' => $this->t('Name'),
29       '#type' => 'textfield',
30       '#default_value' => $type->label,
31       '#description' => $this->t('The human-readable name of this crop type. This name must be unique.'),
32       '#required' => TRUE,
33       '#size' => 30,
34     ];
35
36     $form['id'] = [
37       '#type' => 'machine_name',
38       '#default_value' => $type->id(),
39       '#machine_name' => [
40         'exists' => ['\Drupal\crop\Entity\CropType', 'load'],
41         'source' => ['label'],
42       ],
43       // A crop type's machine name cannot be changed.
44       '#disabled' => !$type->isNew(),
45       '#description' => $this->t('A unique machine-readable name for this crop type. It must only contain lowercase letters, numbers, and underscores.'),
46     ];
47
48     $form['description'] = [
49       '#title' => $this->t('Description'),
50       '#type' => 'textarea',
51       '#default_value' => $type->description,
52       '#description' => $this->t('Describe this crop type.'),
53     ];
54
55     $form['aspect_ratio'] = [
56       '#title' => $this->t('Aspect Ratio'),
57       '#type' => 'textfield',
58       '#default_value' => $type->aspect_ratio,
59       '#attributes' => ['placeholder' => 'W:H'],
60       '#description' => $this->t('Set an aspect ratio <b>eg: 16:9</b> or leave this empty for arbitrary aspect ratio'),
61     ];
62
63     $form['soft_limit'] = [
64       '#type' => 'fieldset',
65       '#title' => $this->t('Soft limit'),
66       '#description' => $this->t('Define crop size soft limit. Warning will be displayed if crop smaller than that is selected.'),
67     ];
68
69     $form['soft_limit']['soft_limit_width'] = [
70       '#type' => 'number',
71       '#title' => $this->t('Width'),
72       '#default_value' => $type->soft_limit_width,
73       '#description' => $this->t('Limit for width.'),
74       '#size' => 60,
75       '#field_suffix' => 'px',
76       '#min' => 0,
77     ];
78     $form['soft_limit']['soft_limit_height'] = [
79       '#type' => 'number',
80       '#title' => $this->t('Height'),
81       '#default_value' => $type->soft_limit_height,
82       '#description' => $this->t('Limit for height.'),
83       '#size' => 60,
84       '#field_suffix' => 'px',
85       '#min' => 0,
86     ];
87
88     $form['hard_limit'] = [
89       '#type' => 'fieldset',
90       '#title' => $this->t('Hard limit'),
91       '#description' => $this->t('Define crop size hard limit. User is not allowed to make a smaller selection than defined here.'),
92     ];
93
94     $form['hard_limit']['hard_limit_width'] = [
95       '#type' => 'number',
96       '#title' => $this->t('Width'),
97       '#default_value' => $type->hard_limit_width,
98       '#description' => $this->t('Limit for width'),
99       '#size' => 60,
100       '#field_suffix' => 'px',
101       '#min' => 0,
102     ];
103     $form['hard_limit']['hard_limit_height'] = [
104       '#type' => 'number',
105       '#title' => $this->t('Height'),
106       '#default_value' => $type->hard_limit_height,
107       '#description' => $this->t('Limit for height.'),
108       '#size' => 60,
109       '#field_suffix' => 'px',
110       '#min' => 0,
111     ];
112
113     return $form;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   protected function actions(array $form, FormStateInterface $form_state) {
120     $actions = parent::actions($form, $form_state);
121     $actions['submit']['#value'] = $this->t('Save crop type');
122     $actions['delete']['#value'] = $this->t('Delete crop type');
123     return $actions;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function validateForm(array &$form, FormStateInterface $form_state) {
130     parent::validateForm($form, $form_state);
131
132     /** @var \Drupal\crop\Entity\CropType $entity */
133     $entity = $this->buildEntity($form, $form_state);
134     $violations = $entity->validate();
135
136     $this->flagViolations($violations, $form, $form_state);
137   }
138
139   /**
140    * Flags violations for the current form.
141    *
142    * @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations
143    *   The violations to flag.
144    * @param array $form
145    *   A nested array of form elements comprising the form.
146    * @param \Drupal\Core\Form\FormStateInterface $form_state
147    *   The current state of the form.
148    */
149   protected function flagViolations(ConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
150     // Manually flag violations of fields not handled by the form display.
151     foreach ($violations->getIterator() as $violation) {
152       if ($violation->getPropertyPath() == 'aspect_ratio') {
153         $form_state->setErrorByName('aspect_ratio', $violation->getMessage());
154       }
155       if ($violation->getPropertyPath() == 'id') {
156         $form_state->setErrorByName('id', $violation->getMessage());
157       }
158     }
159   }
160
161   /**
162    * {@inheritdoc}
163    */
164   public function save(array $form, FormStateInterface $form_state) {
165     $type = $this->entity;
166     $type->id = trim($type->id());
167     $type->label = trim($type->label);
168     $type->aspect_ratio = trim($type->aspect_ratio);
169
170     $status = $type->save();
171
172     $t_args = array('%name' => $type->label());
173
174     if ($status == SAVED_UPDATED) {
175       drupal_set_message($this->t('The crop type %name has been updated.', $t_args));
176     }
177     elseif ($status == SAVED_NEW) {
178       drupal_set_message($this->t('The crop type %name has been added.', $t_args));
179       $context = array_merge($t_args, array('link' => Link::createFromRoute($this->t('View'), 'crop.overview_types')->toString()));
180       $this->logger('crop')->notice('Added crop type %name.', $context);
181     }
182
183     $form_state->setRedirect('crop.overview_types');
184   }
185
186 }