Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / workspaces / src / Form / WorkspaceForm.php
1 <?php
2
3 namespace Drupal\workspaces\Form;
4
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityConstraintViolationListInterface;
8 use Drupal\Core\Entity\EntityRepositoryInterface;
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Messenger\MessengerInterface;
12 use Drupal\Core\Url;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Form controller for the workspace edit forms.
17  */
18 class WorkspaceForm extends ContentEntityForm implements WorkspaceFormInterface {
19
20   /**
21    * The workspace entity.
22    *
23    * @var \Drupal\workspaces\WorkspaceInterface
24    */
25   protected $entity;
26
27   /**
28    * The messenger service.
29    *
30    * @var \Drupal\Core\Messenger\MessengerInterface
31    */
32   protected $messenger;
33
34   /**
35    * Constructs a new WorkspaceForm.
36    *
37    * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
38    *   The entity repository service.
39    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
40    *   The messenger service.
41    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
42    *   The entity type bundle service.
43    * @param \Drupal\Component\Datetime\TimeInterface $time
44    *   The time service.
45    */
46   public function __construct(EntityRepositoryInterface $entity_repository, MessengerInterface $messenger, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
47     parent::__construct($entity_repository, $entity_type_bundle_info, $time);
48     $this->messenger = $messenger;
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container) {
55     return new static(
56       $container->get('entity.repository'),
57       $container->get('messenger'),
58       $container->get('entity_type.bundle.info'),
59       $container->get('datetime.time')
60     );
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function form(array $form, FormStateInterface $form_state) {
67     $workspace = $this->entity;
68
69     if ($this->operation == 'edit') {
70       $form['#title'] = $this->t('Edit workspace %label', ['%label' => $workspace->label()]);
71     }
72     $form['label'] = [
73       '#type' => 'textfield',
74       '#title' => $this->t('Label'),
75       '#maxlength' => 255,
76       '#default_value' => $workspace->label(),
77       '#required' => TRUE,
78     ];
79
80     $form['id'] = [
81       '#type' => 'machine_name',
82       '#title' => $this->t('Workspace ID'),
83       '#maxlength' => 255,
84       '#default_value' => $workspace->id(),
85       '#disabled' => !$workspace->isNew(),
86       '#machine_name' => [
87         'exists' => '\Drupal\workspaces\Entity\Workspace::load',
88       ],
89       '#element_validate' => [],
90     ];
91
92     return parent::form($form, $form_state);
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   protected function getEditedFieldNames(FormStateInterface $form_state) {
99     return array_merge([
100       'label',
101       'id',
102     ], parent::getEditedFieldNames($form_state));
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
109     // Manually flag violations of fields not handled by the form display. This
110     // is necessary as entity form displays only flag violations for fields
111     // contained in the display.
112     $field_names = [
113       'label',
114       'id',
115     ];
116     foreach ($violations->getByFields($field_names) as $violation) {
117       list($field_name) = explode('.', $violation->getPropertyPath(), 2);
118       $form_state->setErrorByName($field_name, $violation->getMessage());
119     }
120     parent::flagViolations($violations, $form, $form_state);
121   }
122
123   /**
124    * {@inheritdoc}
125    */
126   public function save(array $form, FormStateInterface $form_state) {
127     $workspace = $this->entity;
128     $workspace->setNewRevision(TRUE);
129     $status = $workspace->save();
130
131     $info = ['%info' => $workspace->label()];
132     $context = ['@type' => $workspace->bundle(), '%info' => $workspace->label()];
133     $logger = $this->logger('workspaces');
134
135     if ($status == SAVED_UPDATED) {
136       $logger->notice('@type: updated %info.', $context);
137       $this->messenger->addMessage($this->t('Workspace %info has been updated.', $info));
138     }
139     else {
140       $logger->notice('@type: added %info.', $context);
141       $this->messenger->addMessage($this->t('Workspace %info has been created.', $info));
142     }
143
144     if ($workspace->id()) {
145       $form_state->setValue('id', $workspace->id());
146       $form_state->set('id', $workspace->id());
147
148       $collection_url = $workspace->toUrl('collection');
149       $redirect = $collection_url->access() ? $collection_url : Url::fromRoute('<front>');
150       $form_state->setRedirectUrl($redirect);
151     }
152     else {
153       $this->messenger->addError($this->t('The workspace could not be saved.'));
154       $form_state->setRebuild();
155     }
156   }
157
158 }