3 namespace Drupal\crop\Form;
5 use Drupal\Core\Entity\EntityForm;
6 use Drupal\Core\Form\FormStateInterface;
9 use Symfony\Component\Validator\ConstraintViolationListInterface;
12 * Form controller for crop type forms.
14 class CropTypeForm extends EntityForm {
19 public function form(array $form, FormStateInterface $form_state) {
20 $form = parent::form($form, $form_state);
22 $type = $this->entity;
23 $form['#title'] = $this->operation == 'add' ? $this->t('Add crop type')
25 $this->t('Edit %label crop type', array('%label' => $type->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.'),
37 '#type' => 'machine_name',
38 '#default_value' => $type->id(),
40 'exists' => ['\Drupal\crop\Entity\CropType', 'load'],
41 'source' => ['label'],
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.'),
48 $form['description'] = [
49 '#title' => $this->t('Description'),
50 '#type' => 'textarea',
51 '#default_value' => $type->description,
52 '#description' => $this->t('Describe this crop type.'),
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'),
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.'),
69 $form['soft_limit']['soft_limit_width'] = [
71 '#title' => $this->t('Width'),
72 '#default_value' => $type->soft_limit_width,
73 '#description' => $this->t('Limit for width.'),
75 '#field_suffix' => 'px',
78 $form['soft_limit']['soft_limit_height'] = [
80 '#title' => $this->t('Height'),
81 '#default_value' => $type->soft_limit_height,
82 '#description' => $this->t('Limit for height.'),
84 '#field_suffix' => 'px',
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.'),
94 $form['hard_limit']['hard_limit_width'] = [
96 '#title' => $this->t('Width'),
97 '#default_value' => $type->hard_limit_width,
98 '#description' => $this->t('Limit for width'),
100 '#field_suffix' => 'px',
103 $form['hard_limit']['hard_limit_height'] = [
105 '#title' => $this->t('Height'),
106 '#default_value' => $type->hard_limit_height,
107 '#description' => $this->t('Limit for height.'),
109 '#field_suffix' => 'px',
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');
129 public function validateForm(array &$form, FormStateInterface $form_state) {
130 parent::validateForm($form, $form_state);
132 /** @var \Drupal\crop\Entity\CropType $entity */
133 $entity = $this->buildEntity($form, $form_state);
134 $violations = $entity->validate();
136 $this->flagViolations($violations, $form, $form_state);
140 * Flags violations for the current form.
142 * @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations
143 * The violations to flag.
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.
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());
155 if ($violation->getPropertyPath() == 'id') {
156 $form_state->setErrorByName('id', $violation->getMessage());
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);
170 $status = $type->save();
172 $t_args = array('%name' => $type->label());
174 if ($status == SAVED_UPDATED) {
175 drupal_set_message($this->t('The crop type %name has been updated.', $t_args));
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);
183 $form_state->setRedirect('crop.overview_types');