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\Core\Session\AccountInterface;
11 use Drupal\user\PrivateTempStoreFactory;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Form handler for the node edit forms.
17 class NodeForm extends ContentEntityForm {
20 * The tempstore factory.
22 * @var \Drupal\user\PrivateTempStoreFactory
24 protected $tempStoreFactory;
27 * The Current User object.
29 * @var \Drupal\Core\Session\AccountInterface
31 protected $currentUser;
34 * Constructs a NodeForm object.
36 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
38 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
39 * The factory for the temp store object.
40 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
41 * The entity type bundle service.
42 * @param \Drupal\Component\Datetime\TimeInterface $time
44 * @param \Drupal\Core\Session\AccountInterface $current_user
47 public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
48 parent::__construct($entity_manager, $entity_type_bundle_info, $time);
49 $this->tempStoreFactory = $temp_store_factory;
50 $this->currentUser = $current_user;
56 public static function create(ContainerInterface $container) {
58 $container->get('entity.manager'),
59 $container->get('user.private_tempstore'),
60 $container->get('entity_type.bundle.info'),
61 $container->get('datetime.time'),
62 $container->get('current_user')
69 public function form(array $form, FormStateInterface $form_state) {
70 // Try to restore from temp store, this must be done before calling
72 $store = $this->tempStoreFactory->get('node_preview');
74 // Attempt to load from preview when the uuid is present unless we are
75 // rebuilding the form.
76 $request_uuid = \Drupal::request()->query->get('uuid');
77 if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
78 /** @var $preview \Drupal\Core\Form\FormStateInterface */
80 $form_state->setStorage($preview->getStorage());
81 $form_state->setUserInput($preview->getUserInput());
84 $form_state->setRebuild();
86 // The combination of having user input and rebuilding the form means
87 // that it will attempt to cache the form state which will fail if it is
89 $form_state->setRequestMethod('POST');
91 $this->entity = $preview->getFormObject()->getEntity();
92 $this->entity->in_preview = NULL;
94 $form_state->set('has_been_previewed', TRUE);
97 /** @var \Drupal\node\NodeInterface $node */
98 $node = $this->entity;
100 if ($this->operation == 'edit') {
101 $form['#title'] = $this->t('<em>Edit @type</em> @title', [
102 '@type' => node_get_type_label($node),
103 '@title' => $node->label()
107 // Changed must be sent to the client, for later overwrite error checking.
110 '#default_value' => $node->getChangedTime(),
113 $form = parent::form($form, $form_state);
115 $form['advanced']['#attributes']['class'][] = 'entity-meta';
118 '#type' => 'details',
119 '#group' => 'advanced',
121 '#title' => $this->t('Status'),
122 '#attributes' => ['class' => ['entity-meta__header']],
124 '#access' => $this->currentUser->hasPermission('administer nodes'),
126 $form['meta']['published'] = [
128 '#markup' => $node->isPublished() ? $this->t('Published') : $this->t('Not published'),
129 '#access' => !$node->isNew(),
130 '#wrapper_attributes' => ['class' => ['entity-meta__title']],
132 $form['meta']['changed'] = [
134 '#title' => $this->t('Last saved'),
135 '#markup' => !$node->isNew() ? format_date($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
136 '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
138 $form['meta']['author'] = [
140 '#title' => $this->t('Author'),
141 '#markup' => $node->getOwner()->getUsername(),
142 '#wrapper_attributes' => ['class' => ['entity-meta__author']],
146 '#type' => 'container',
149 'class' => ['node-form-footer']
152 $form['status']['#group'] = 'footer';
154 // Node author information for administrators.
156 '#type' => 'details',
157 '#title' => t('Authoring information'),
158 '#group' => 'advanced',
160 'class' => ['node-form-author'],
163 'library' => ['node/drupal.node'],
169 if (isset($form['uid'])) {
170 $form['uid']['#group'] = 'author';
173 if (isset($form['created'])) {
174 $form['created']['#group'] = 'author';
177 // Node options for administrators.
179 '#type' => 'details',
180 '#title' => t('Promotion options'),
181 '#group' => 'advanced',
183 'class' => ['node-form-options'],
186 'library' => ['node/drupal.node'],
192 if (isset($form['promote'])) {
193 $form['promote']['#group'] = 'options';
196 if (isset($form['sticky'])) {
197 $form['sticky']['#group'] = 'options';
200 $form['#attached']['library'][] = 'node/form';
206 * Entity builder updating the node status with the submitted value.
208 * @param string $entity_type_id
209 * The entity type identifier.
210 * @param \Drupal\node\NodeInterface $node
211 * The node updated with the submitted values.
213 * The complete form array.
214 * @param \Drupal\Core\Form\FormStateInterface $form_state
215 * The current state of the form.
217 * @see \Drupal\node\NodeForm::form()
219 * @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0.
220 * The "Publish" button was removed.
222 public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
223 $element = $form_state->getTriggeringElement();
224 if (isset($element['#published_status'])) {
225 $node->setPublished($element['#published_status']);
232 protected function actions(array $form, FormStateInterface $form_state) {
233 $element = parent::actions($form, $form_state);
234 $node = $this->entity;
235 $preview_mode = $node->type->entity->getPreviewMode();
237 $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
239 $element['preview'] = [
241 '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
242 '#value' => t('Preview'),
244 '#submit' => ['::submitForm', '::preview'],
247 $element['delete']['#access'] = $node->access('delete');
248 $element['delete']['#weight'] = 100;
254 * Form submission handler for the 'preview' action.
257 * An associative array containing the structure of the form.
259 * The current state of the form.
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);
266 $route_parameters = [
267 'node_preview' => $this->entity->uuid(),
268 'view_mode_id' => 'full',
272 $query = $this->getRequest()->query;
273 if ($query->has('destination')) {
274 $options['query']['destination'] = $query->get('destination');
275 $query->remove('destination');
277 $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
283 public function save(array $form, FormStateInterface $form_state) {
284 $node = $this->entity;
285 $insert = $node->isNew();
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())];
292 $this->logger('content')->notice('@type: added %title.', $context);
293 drupal_set_message(t('@type %title has been created.', $t_args));
296 $this->logger('content')->notice('@type: updated %title.', $context);
297 drupal_set_message(t('@type %title has been updated.', $t_args));
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()]
310 $form_state->setRedirect('<front>');
313 // Remove the preview entry from the temp store, if any.
314 $store = $this->tempStoreFactory->get('node_preview');
315 $store->delete($node->uuid());
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();