Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\node;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Datetime\TimeInterface;
|
Chris@18
|
6 use Drupal\Core\Datetime\DateFormatterInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\ContentEntityForm;
|
Chris@17
|
8 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
Chris@0
|
10 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
11 use Drupal\Core\Session\AccountInterface;
|
Chris@14
|
12 use Drupal\Core\TempStore\PrivateTempStoreFactory;
|
Chris@0
|
13 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Form handler for the node edit forms.
|
Chris@14
|
17 *
|
Chris@14
|
18 * @internal
|
Chris@0
|
19 */
|
Chris@0
|
20 class NodeForm extends ContentEntityForm {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The tempstore factory.
|
Chris@0
|
24 *
|
Chris@14
|
25 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $tempStoreFactory;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The Current User object.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $currentUser;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@18
|
37 * The date formatter service.
|
Chris@18
|
38 *
|
Chris@18
|
39 * @var \Drupal\Core\Datetime\DateFormatterInterface
|
Chris@18
|
40 */
|
Chris@18
|
41 protected $dateFormatter;
|
Chris@18
|
42
|
Chris@18
|
43 /**
|
Chris@0
|
44 * Constructs a NodeForm object.
|
Chris@0
|
45 *
|
Chris@17
|
46 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@17
|
47 * The entity repository.
|
Chris@14
|
48 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
|
Chris@0
|
49 * The factory for the temp store object.
|
Chris@0
|
50 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
|
Chris@0
|
51 * The entity type bundle service.
|
Chris@0
|
52 * @param \Drupal\Component\Datetime\TimeInterface $time
|
Chris@0
|
53 * The time service.
|
Chris@0
|
54 * @param \Drupal\Core\Session\AccountInterface $current_user
|
Chris@0
|
55 * The current user.
|
Chris@18
|
56 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
Chris@18
|
57 * The date formatter service.
|
Chris@0
|
58 */
|
Chris@18
|
59 public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
|
Chris@17
|
60 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
|
Chris@0
|
61 $this->tempStoreFactory = $temp_store_factory;
|
Chris@0
|
62 $this->currentUser = $current_user;
|
Chris@18
|
63 $this->dateFormatter = $date_formatter;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public static function create(ContainerInterface $container) {
|
Chris@0
|
70 return new static(
|
Chris@17
|
71 $container->get('entity.repository'),
|
Chris@14
|
72 $container->get('tempstore.private'),
|
Chris@0
|
73 $container->get('entity_type.bundle.info'),
|
Chris@0
|
74 $container->get('datetime.time'),
|
Chris@18
|
75 $container->get('current_user'),
|
Chris@18
|
76 $container->get('date.formatter')
|
Chris@0
|
77 );
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * {@inheritdoc}
|
Chris@0
|
82 */
|
Chris@0
|
83 public function form(array $form, FormStateInterface $form_state) {
|
Chris@0
|
84 // Try to restore from temp store, this must be done before calling
|
Chris@0
|
85 // parent::form().
|
Chris@0
|
86 $store = $this->tempStoreFactory->get('node_preview');
|
Chris@0
|
87
|
Chris@0
|
88 // Attempt to load from preview when the uuid is present unless we are
|
Chris@0
|
89 // rebuilding the form.
|
Chris@0
|
90 $request_uuid = \Drupal::request()->query->get('uuid');
|
Chris@0
|
91 if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
|
Chris@0
|
92 /** @var $preview \Drupal\Core\Form\FormStateInterface */
|
Chris@0
|
93
|
Chris@0
|
94 $form_state->setStorage($preview->getStorage());
|
Chris@0
|
95 $form_state->setUserInput($preview->getUserInput());
|
Chris@0
|
96
|
Chris@0
|
97 // Rebuild the form.
|
Chris@0
|
98 $form_state->setRebuild();
|
Chris@0
|
99
|
Chris@0
|
100 // The combination of having user input and rebuilding the form means
|
Chris@0
|
101 // that it will attempt to cache the form state which will fail if it is
|
Chris@0
|
102 // a GET request.
|
Chris@0
|
103 $form_state->setRequestMethod('POST');
|
Chris@0
|
104
|
Chris@0
|
105 $this->entity = $preview->getFormObject()->getEntity();
|
Chris@0
|
106 $this->entity->in_preview = NULL;
|
Chris@0
|
107
|
Chris@0
|
108 $form_state->set('has_been_previewed', TRUE);
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 /** @var \Drupal\node\NodeInterface $node */
|
Chris@0
|
112 $node = $this->entity;
|
Chris@0
|
113
|
Chris@0
|
114 if ($this->operation == 'edit') {
|
Chris@0
|
115 $form['#title'] = $this->t('<em>Edit @type</em> @title', [
|
Chris@0
|
116 '@type' => node_get_type_label($node),
|
Chris@17
|
117 '@title' => $node->label(),
|
Chris@0
|
118 ]);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 // Changed must be sent to the client, for later overwrite error checking.
|
Chris@0
|
122 $form['changed'] = [
|
Chris@0
|
123 '#type' => 'hidden',
|
Chris@0
|
124 '#default_value' => $node->getChangedTime(),
|
Chris@0
|
125 ];
|
Chris@0
|
126
|
Chris@0
|
127 $form = parent::form($form, $form_state);
|
Chris@0
|
128
|
Chris@0
|
129 $form['advanced']['#attributes']['class'][] = 'entity-meta';
|
Chris@0
|
130
|
Chris@0
|
131 $form['meta'] = [
|
Chris@0
|
132 '#type' => 'details',
|
Chris@0
|
133 '#group' => 'advanced',
|
Chris@0
|
134 '#weight' => -10,
|
Chris@0
|
135 '#title' => $this->t('Status'),
|
Chris@0
|
136 '#attributes' => ['class' => ['entity-meta__header']],
|
Chris@0
|
137 '#tree' => TRUE,
|
Chris@0
|
138 '#access' => $this->currentUser->hasPermission('administer nodes'),
|
Chris@0
|
139 ];
|
Chris@0
|
140 $form['meta']['published'] = [
|
Chris@0
|
141 '#type' => 'item',
|
Chris@0
|
142 '#markup' => $node->isPublished() ? $this->t('Published') : $this->t('Not published'),
|
Chris@0
|
143 '#access' => !$node->isNew(),
|
Chris@0
|
144 '#wrapper_attributes' => ['class' => ['entity-meta__title']],
|
Chris@0
|
145 ];
|
Chris@0
|
146 $form['meta']['changed'] = [
|
Chris@0
|
147 '#type' => 'item',
|
Chris@0
|
148 '#title' => $this->t('Last saved'),
|
Chris@18
|
149 '#markup' => !$node->isNew() ? $this->dateFormatter->format($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
|
Chris@0
|
150 '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
|
Chris@0
|
151 ];
|
Chris@0
|
152 $form['meta']['author'] = [
|
Chris@0
|
153 '#type' => 'item',
|
Chris@0
|
154 '#title' => $this->t('Author'),
|
Chris@18
|
155 '#markup' => $node->getOwner()->getAccountName(),
|
Chris@0
|
156 '#wrapper_attributes' => ['class' => ['entity-meta__author']],
|
Chris@0
|
157 ];
|
Chris@0
|
158
|
Chris@0
|
159 $form['status']['#group'] = 'footer';
|
Chris@0
|
160
|
Chris@0
|
161 // Node author information for administrators.
|
Chris@0
|
162 $form['author'] = [
|
Chris@0
|
163 '#type' => 'details',
|
Chris@0
|
164 '#title' => t('Authoring information'),
|
Chris@0
|
165 '#group' => 'advanced',
|
Chris@0
|
166 '#attributes' => [
|
Chris@0
|
167 'class' => ['node-form-author'],
|
Chris@0
|
168 ],
|
Chris@0
|
169 '#attached' => [
|
Chris@0
|
170 'library' => ['node/drupal.node'],
|
Chris@0
|
171 ],
|
Chris@0
|
172 '#weight' => 90,
|
Chris@0
|
173 '#optional' => TRUE,
|
Chris@0
|
174 ];
|
Chris@0
|
175
|
Chris@0
|
176 if (isset($form['uid'])) {
|
Chris@0
|
177 $form['uid']['#group'] = 'author';
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 if (isset($form['created'])) {
|
Chris@0
|
181 $form['created']['#group'] = 'author';
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 // Node options for administrators.
|
Chris@0
|
185 $form['options'] = [
|
Chris@0
|
186 '#type' => 'details',
|
Chris@0
|
187 '#title' => t('Promotion options'),
|
Chris@0
|
188 '#group' => 'advanced',
|
Chris@0
|
189 '#attributes' => [
|
Chris@0
|
190 'class' => ['node-form-options'],
|
Chris@0
|
191 ],
|
Chris@0
|
192 '#attached' => [
|
Chris@0
|
193 'library' => ['node/drupal.node'],
|
Chris@0
|
194 ],
|
Chris@0
|
195 '#weight' => 95,
|
Chris@0
|
196 '#optional' => TRUE,
|
Chris@0
|
197 ];
|
Chris@0
|
198
|
Chris@0
|
199 if (isset($form['promote'])) {
|
Chris@0
|
200 $form['promote']['#group'] = 'options';
|
Chris@0
|
201 }
|
Chris@0
|
202
|
Chris@0
|
203 if (isset($form['sticky'])) {
|
Chris@0
|
204 $form['sticky']['#group'] = 'options';
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 $form['#attached']['library'][] = 'node/form';
|
Chris@0
|
208
|
Chris@0
|
209 return $form;
|
Chris@0
|
210 }
|
Chris@0
|
211
|
Chris@0
|
212 /**
|
Chris@0
|
213 * Entity builder updating the node status with the submitted value.
|
Chris@0
|
214 *
|
Chris@0
|
215 * @param string $entity_type_id
|
Chris@0
|
216 * The entity type identifier.
|
Chris@0
|
217 * @param \Drupal\node\NodeInterface $node
|
Chris@0
|
218 * The node updated with the submitted values.
|
Chris@0
|
219 * @param array $form
|
Chris@0
|
220 * The complete form array.
|
Chris@0
|
221 * @param \Drupal\Core\Form\FormStateInterface $form_state
|
Chris@0
|
222 * The current state of the form.
|
Chris@0
|
223 *
|
Chris@0
|
224 * @see \Drupal\node\NodeForm::form()
|
Chris@0
|
225 *
|
Chris@0
|
226 * @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0.
|
Chris@0
|
227 * The "Publish" button was removed.
|
Chris@0
|
228 */
|
Chris@0
|
229 public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
|
Chris@0
|
230 $element = $form_state->getTriggeringElement();
|
Chris@0
|
231 if (isset($element['#published_status'])) {
|
Chris@17
|
232 $element['#published_status'] ? $node->setPublished() : $node->setUnpublished();
|
Chris@0
|
233 }
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 /**
|
Chris@0
|
237 * {@inheritdoc}
|
Chris@0
|
238 */
|
Chris@0
|
239 protected function actions(array $form, FormStateInterface $form_state) {
|
Chris@0
|
240 $element = parent::actions($form, $form_state);
|
Chris@0
|
241 $node = $this->entity;
|
Chris@0
|
242 $preview_mode = $node->type->entity->getPreviewMode();
|
Chris@0
|
243
|
Chris@0
|
244 $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
|
Chris@0
|
245
|
Chris@0
|
246 $element['preview'] = [
|
Chris@0
|
247 '#type' => 'submit',
|
Chris@0
|
248 '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
|
Chris@0
|
249 '#value' => t('Preview'),
|
Chris@0
|
250 '#weight' => 20,
|
Chris@0
|
251 '#submit' => ['::submitForm', '::preview'],
|
Chris@0
|
252 ];
|
Chris@0
|
253
|
Chris@0
|
254 $element['delete']['#access'] = $node->access('delete');
|
Chris@0
|
255 $element['delete']['#weight'] = 100;
|
Chris@0
|
256
|
Chris@0
|
257 return $element;
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@0
|
260 /**
|
Chris@0
|
261 * Form submission handler for the 'preview' action.
|
Chris@0
|
262 *
|
Chris@0
|
263 * @param $form
|
Chris@0
|
264 * An associative array containing the structure of the form.
|
Chris@0
|
265 * @param $form_state
|
Chris@0
|
266 * The current state of the form.
|
Chris@0
|
267 */
|
Chris@0
|
268 public function preview(array $form, FormStateInterface $form_state) {
|
Chris@0
|
269 $store = $this->tempStoreFactory->get('node_preview');
|
Chris@0
|
270 $this->entity->in_preview = TRUE;
|
Chris@0
|
271 $store->set($this->entity->uuid(), $form_state);
|
Chris@0
|
272
|
Chris@0
|
273 $route_parameters = [
|
Chris@0
|
274 'node_preview' => $this->entity->uuid(),
|
Chris@0
|
275 'view_mode_id' => 'full',
|
Chris@0
|
276 ];
|
Chris@0
|
277
|
Chris@0
|
278 $options = [];
|
Chris@0
|
279 $query = $this->getRequest()->query;
|
Chris@0
|
280 if ($query->has('destination')) {
|
Chris@0
|
281 $options['query']['destination'] = $query->get('destination');
|
Chris@0
|
282 $query->remove('destination');
|
Chris@0
|
283 }
|
Chris@0
|
284 $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * {@inheritdoc}
|
Chris@0
|
289 */
|
Chris@0
|
290 public function save(array $form, FormStateInterface $form_state) {
|
Chris@0
|
291 $node = $this->entity;
|
Chris@0
|
292 $insert = $node->isNew();
|
Chris@0
|
293 $node->save();
|
Chris@18
|
294 $node_link = $node->toLink($this->t('View'))->toString();
|
Chris@0
|
295 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
|
Chris@18
|
296 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->toLink()->toString()];
|
Chris@0
|
297
|
Chris@0
|
298 if ($insert) {
|
Chris@0
|
299 $this->logger('content')->notice('@type: added %title.', $context);
|
Chris@17
|
300 $this->messenger()->addStatus($this->t('@type %title has been created.', $t_args));
|
Chris@0
|
301 }
|
Chris@0
|
302 else {
|
Chris@0
|
303 $this->logger('content')->notice('@type: updated %title.', $context);
|
Chris@17
|
304 $this->messenger()->addStatus($this->t('@type %title has been updated.', $t_args));
|
Chris@0
|
305 }
|
Chris@0
|
306
|
Chris@0
|
307 if ($node->id()) {
|
Chris@0
|
308 $form_state->setValue('nid', $node->id());
|
Chris@0
|
309 $form_state->set('nid', $node->id());
|
Chris@0
|
310 if ($node->access('view')) {
|
Chris@0
|
311 $form_state->setRedirect(
|
Chris@0
|
312 'entity.node.canonical',
|
Chris@0
|
313 ['node' => $node->id()]
|
Chris@0
|
314 );
|
Chris@0
|
315 }
|
Chris@0
|
316 else {
|
Chris@0
|
317 $form_state->setRedirect('<front>');
|
Chris@0
|
318 }
|
Chris@0
|
319
|
Chris@0
|
320 // Remove the preview entry from the temp store, if any.
|
Chris@0
|
321 $store = $this->tempStoreFactory->get('node_preview');
|
Chris@0
|
322 $store->delete($node->uuid());
|
Chris@0
|
323 }
|
Chris@0
|
324 else {
|
Chris@0
|
325 // In the unlikely case something went wrong on save, the node will be
|
Chris@0
|
326 // rebuilt and node form redisplayed the same way as in preview.
|
Chris@17
|
327 $this->messenger()->addError($this->t('The post could not be saved.'));
|
Chris@0
|
328 $form_state->setRebuild();
|
Chris@0
|
329 }
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@0
|
332 }
|