NodeForm.php 12.6 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\node;

5
use Drupal\Core\Entity\ContentEntityForm;
6
use Drupal\Core\Entity\EntityManagerInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\user\PrivateTempStoreFactory;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10 11

/**
12
 * Form handler for the node edit forms.
13
 */
14
class NodeForm extends ContentEntityForm {
15

16 17 18
  /**
   * The tempstore factory.
   *
19
   * @var \Drupal\user\PrivateTempStoreFactory
20 21 22
   */
  protected $tempStoreFactory;

23 24 25 26 27
  /**
   * Whether this node has been previewed or not.
   */
  protected $hasBeenPreviewed = FALSE;

28 29 30 31 32
  /**
   * Constructs a ContentEntityForm object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
33
   * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
34 35
   *   The factory for the temp store object.
   */
36
  public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory) {
37 38 39 40 41 42 43 44 45 46
    parent::__construct($entity_manager);
    $this->tempStoreFactory = $temp_store_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
47
      $container->get('user.private_tempstore')
48 49 50
    );
  }

51
  /**
52
   * {@inheritdoc}
53
   */
54
  protected function prepareEntity() {
55
    /** @var \Drupal\node\NodeInterface $node */
56
    $node = $this->entity;
57

58
    if (!$node->isNew()) {
59 60
      // Remove the revision log message from the original node entity.
      $node->revision_log = NULL;
61 62 63 64
    }
  }

  /**
65
   * {@inheritdoc}
66
   */
67
  public function form(array $form, FormStateInterface $form_state) {
68 69
    // Try to restore from temp store, this must be done before calling
    // parent::form().
70 71 72 73 74 75 76 77 78
    $uuid = $this->entity->uuid();
    $store = $this->tempStoreFactory->get('node_preview');

    // If the user is creating a new node, the UUID is passed in the request.
    if ($request_uuid = \Drupal::request()->query->get('uuid')) {
      $uuid = $request_uuid;
    }

    if ($preview = $store->get($uuid)) {
79
      /** @var $preview \Drupal\Core\Form\FormStateInterface */
80 81 82 83

      foreach ($preview->getValues() as $name => $value) {
        $form_state->setValue($name, $value);
      }
84 85

      // Rebuild the form.
86
      $form_state->setRebuild();
87
      $this->entity = $preview->getFormObject()->getEntity();
88
      $this->entity->in_preview = NULL;
89

90 91 92 93 94
      // Remove the stale temp store entry for existing nodes.
      if (!$this->entity->isNew()) {
        $store->delete($uuid);
      }

95
      $this->hasBeenPreviewed = TRUE;
96 97
    }

98
    /** @var \Drupal\node\NodeInterface $node */
99
    $node = $this->entity;
100

101
    if ($this->operation == 'edit') {
102
      $form['#title'] = $this->t('<em>Edit @type</em> @title', array('@type' => node_get_type_label($node), '@title' => $node->label()));
103 104
    }

105
    $current_user = $this->currentUser();
106 107 108 109

    // Changed must be sent to the client, for later overwrite error checking.
    $form['changed'] = array(
      '#type' => 'hidden',
110
      '#default_value' => $node->getChangedTime(),
111 112
    );

113
    $form['advanced'] = array(
114
      '#type' => 'vertical_tabs',
115
      '#attributes' => array('class' => array('entity-meta')),
116 117
      '#weight' => 99,
    );
118
    $form = parent::form($form, $form_state);
119

120
    // Add a revision_log field if the "Create new revision" option is checked,
121
    // or if the current user has the ability to check that option.
122
    $form['revision_information'] = array(
123 124
      '#type' => 'details',
      '#group' => 'advanced',
125
      '#title' => t('Revision information'),
126 127
      // Open by default when "Create new revision" is checked.
      '#open' => $node->isNewRevision(),
128 129 130 131
      '#attributes' => array(
        'class' => array('node-form-revision-information'),
      ),
      '#attached' => array(
132
        'library' => array('node/drupal.node'),
133 134
      ),
      '#weight' => 20,
135
      '#optional' => TRUE,
136 137
    );

138
    $form['revision'] = array(
139 140
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
141
      '#default_value' => $node->type->entity->isNewRevision(),
142
      '#access' => $current_user->hasPermission('administer nodes') && !$node->isNew(),
143
      '#group' => 'revision_information',
144 145
    );

146
    $form['revision_log'] += array(
147 148 149 150 151
      '#states' => array(
        'visible' => array(
          ':input[name="revision"]' => array('checked' => TRUE),
        ),
      ),
152
      '#group' => 'revision_information',
153 154 155 156
    );

    // Node author information for administrators.
    $form['author'] = array(
157
      '#type' => 'details',
158
      '#title' => t('Authoring information'),
159
      '#group' => 'advanced',
160 161 162 163
      '#attributes' => array(
        'class' => array('node-form-author'),
      ),
      '#attached' => array(
164
        'library' => array('node/drupal.node'),
165 166
      ),
      '#weight' => 90,
167
      '#optional' => TRUE,
168 169
    );

170 171 172 173 174 175 176
    if (isset($form['uid'])) {
      $form['uid']['#group'] = 'author';
    }

    if (isset($form['created'])) {
      $form['created']['#group'] = 'author';
    }
177 178 179

    // Node options for administrators.
    $form['options'] = array(
180
      '#type' => 'details',
181
      '#title' => t('Promotion options'),
182
      '#group' => 'advanced',
183 184 185 186
      '#attributes' => array(
        'class' => array('node-form-options'),
      ),
      '#attached' => array(
187
        'library' => array('node/drupal.node'),
188 189
      ),
      '#weight' => 95,
190
      '#optional' => TRUE,
191 192
    );

193 194 195
    if (isset($form['promote'])) {
      $form['promote']['#group'] = 'options';
    }
196

197 198 199
    if (isset($form['sticky'])) {
      $form['sticky']['#group'] = 'options';
    }
200

201 202
    $form['#attached']['library'][] = 'node/form';

203
    $form['#entity_builders']['update_status'] = '::updateStatus';
204

205
    return $form;
206 207
  }

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  /**
   * Entity builder updating the node status with the submitted value.
   *
   * @param string $entity_type_id
   *   The entity type identifier.
   * @param \Drupal\node\NodeInterface $node
   *   The node updated with the submitted values.
   * @param array $form
   *   The complete form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @see \Drupal\node\NodeForm::form()
   */
  function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
    $element = $form_state->getTriggeringElement();
    if (isset($element['#published_status'])) {
      $node->setPublished($element['#published_status']);
    }
  }

229
  /**
230
   * {@inheritdoc}
231
   */
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();
236

237
    $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $this->hasBeenPreviewed;
238

239 240 241 242 243 244
    // If saving is an option, privileged users get dedicated form submit
    // buttons to adjust the publishing status while saving in one go.
    // @todo This adjustment makes it close to impossible for contributed
    //   modules to integrate with "the Save operation" of this form. Modules
    //   need a way to plug themselves into 1) the ::submit() step, and
    //   2) the ::save() step, both decoupled from the pressed form button.
245
    if ($element['submit']['#access'] && \Drupal::currentUser()->hasPermission('administer nodes')) {
246 247 248 249 250 251 252 253
      // isNew | prev status » default   & publish label             & unpublish label
      // 1     | 1           » publish   & Save and publish          & Save as unpublished
      // 1     | 0           » unpublish & Save and publish          & Save as unpublished
      // 0     | 1           » publish   & Save and keep published   & Save and unpublish
      // 0     | 0           » unpublish & Save and keep unpublished & Save and publish

      // Add a "Publish" button.
      $element['publish'] = $element['submit'];
254 255
      // If the "Publish" button is clicked, we want to update the status to "published".
      $element['publish']['#published_status'] = TRUE;
256 257 258
      $element['publish']['#dropbutton'] = 'save';
      if ($node->isNew()) {
        $element['publish']['#value'] = t('Save and publish');
259
      }
260
      else {
261
        $element['publish']['#value'] = $node->isPublished() ? t('Save and keep published') : t('Save and publish');
262 263 264 265 266
      }
      $element['publish']['#weight'] = 0;

      // Add a "Unpublish" button.
      $element['unpublish'] = $element['submit'];
267 268
      // If the "Unpublish" button is clicked, we want to update the status to "unpublished".
      $element['unpublish']['#published_status'] = FALSE;
269 270 271 272 273
      $element['unpublish']['#dropbutton'] = 'save';
      if ($node->isNew()) {
        $element['unpublish']['#value'] = t('Save as unpublished');
      }
      else {
274
        $element['unpublish']['#value'] = !$node->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
275 276
      }
      $element['unpublish']['#weight'] = 10;
277

278
      // If already published, the 'publish' button is primary.
279
      if ($node->isPublished()) {
280 281 282 283 284 285 286
        unset($element['unpublish']['#button_type']);
      }
      // Otherwise, the 'unpublish' button is primary and should come first.
      else {
        unset($element['publish']['#button_type']);
        $element['unpublish']['#weight'] = -10;
      }
287

288 289 290
      // Remove the "Save" button.
      $element['submit']['#access'] = FALSE;
    }
291 292

    $element['preview'] = array(
293
      '#type' => 'submit',
294
      '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
295
      '#value' => t('Preview'),
296
      '#weight' => 20,
297
      '#submit' => array('::submitForm', '::preview'),
298 299
    );

300
    $element['delete']['#access'] = $node->access('delete');
301
    $element['delete']['#weight'] = 100;
302 303 304 305 306

    return $element;
  }

  /**
307 308
   * {@inheritdoc}
   *
309 310 311 312 313 314
   * Updates the node object by processing the submitted values.
   *
   * This function can be called by a "Next" button of a wizard to update the
   * form state's entity with the current step's values before proceeding to the
   * next step.
   */
315
  public function submitForm(array &$form, FormStateInterface $form_state) {
316
    // Build the node object from the submitted values.
317 318
    parent::submitForm($form, $form_state);
    $node = $this->entity;
319

320
    // Save as a new revision if requested to do so.
321
    if (!$form_state->isValueEmpty('revision') && $form_state->getValue('revision') != FALSE) {
322
      $node->setNewRevision();
323 324
      // If a new revision is created, save the current user as revision author.
      $node->setRevisionCreationTime(REQUEST_TIME);
325
      $node->setRevisionUserId(\Drupal::currentUser()->id());
326
    }
327 328 329
    else {
      $node->setNewRevision(FALSE);
    }
330 331 332 333 334 335 336 337
  }

  /**
   * Form submission handler for the 'preview' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
338
   *   The current state of the form.
339
   */
340
  public function preview(array $form, FormStateInterface $form_state) {
341 342 343 344 345 346 347
    $store = $this->tempStoreFactory->get('node_preview');
    $this->entity->in_preview = TRUE;
    $store->set($this->entity->uuid(), $form_state);
    $form_state->setRedirect('entity.node.preview', array(
      'node_preview' => $this->entity->uuid(),
      'view_mode_id' => 'default',
    ));
348 349 350
  }

  /**
351
   * {@inheritdoc}
352
   */
353
  public function save(array $form, FormStateInterface $form_state) {
354
    $node = $this->entity;
355
    $insert = $node->isNew();
356
    $node->save();
357
    $node_link = $node->link($this->t('View'));
358
    $context = array('@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link);
359
    $t_args = array('@type' => node_get_type_label($node), '%title' => $node->link($node->label()));
360 361

    if ($insert) {
362
      $this->logger('content')->notice('@type: added %title.', $context);
363 364 365
      drupal_set_message(t('@type %title has been created.', $t_args));
    }
    else {
366
      $this->logger('content')->notice('@type: updated %title.', $context);
367 368 369
      drupal_set_message(t('@type %title has been updated.', $t_args));
    }

370
    if ($node->id()) {
371
      $form_state->setValue('nid', $node->id());
372
      $form_state->set('nid', $node->id());
373
      if ($node->access('view')) {
374
        $form_state->setRedirect(
375
          'entity.node.canonical',
376
          array('node' => $node->id())
377 378 379
        );
      }
      else {
380
        $form_state->setRedirect('<front>');
381
      }
382 383 384

      // Remove the preview entry from the temp store, if any.
      $store = $this->tempStoreFactory->get('node_preview');
385
      $store->delete($node->uuid());
386 387 388 389 390
    }
    else {
      // In the unlikely case something went wrong on save, the node will be
      // rebuilt and node form redisplayed the same way as in preview.
      drupal_set_message(t('The post could not be saved.'), 'error');
391
      $form_state->setRebuild();
392 393 394 395
    }
  }

}