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