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

/**
 * @file
5
 * Definition of Drupal\node\NodeForm.
6 7 8 9
 */

namespace Drupal\node;

10
use Drupal\Component\Utility\Html;
11
use Drupal\Core\Entity\ContentEntityForm;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Language\LanguageInterface;
14 15 16
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\user\TempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Drupal\user\Entity\User;
18 19 20 21

/**
 * Form controller for the node edit forms.
 */
22
class NodeForm extends ContentEntityForm {
23

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  /**
   * The tempstore factory.
   *
   * @var \Drupal\user\TempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * Constructs a ContentEntityForm object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   * @param \Drupal\user\TempStoreFactory $temp_store_factory
   *   The factory for the temp store object.
   */
  public function __construct(EntityManagerInterface $entity_manager, TempStoreFactory $temp_store_factory) {
    parent::__construct($entity_manager);
    $this->tempStoreFactory = $temp_store_factory;
  }

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

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

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

  /**
68
   * {@inheritdoc}
69
   */
70
  public function form(array $form, FormStateInterface $form_state) {
71 72
    // Try to restore from temp store, this must be done before calling
    // parent::form().
73 74 75 76 77 78 79 80 81
    $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)) {
82
      /** @var $preview \Drupal\Core\Form\FormStateInterface */
83 84 85
      $form_state = $preview;

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

91
    /** @var \Drupal\node\NodeInterface $node */
92
    $node = $this->entity;
93

94
    if ($this->operation == 'edit') {
95
      $form['#title'] = $this->t('<em>Edit @type</em> @title', array('@type' => node_get_type_label($node), '@title' => $node->label()));
96 97
    }

98
    $current_user = $this->currentUser();
99 100 101 102

    // Override the default CSS class name, since the user-defined node type
    // name in 'TYPE-node-form' potentially clashes with third-party class
    // names.
103
    $form['#attributes']['class'][0] = Html::getClass('node-' . $node->getType() . '-form');
104 105 106 107

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

111
    $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('node', $node->getType()));
112 113 114
    $form['langcode'] = array(
      '#title' => t('Language'),
      '#type' => 'language_select',
115
      '#default_value' => $node->getUntranslated()->language()->getId(),
116
      '#languages' => LanguageInterface::STATE_ALL,
117
      '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
118
    );
119

120
    $form['advanced'] = array(
121
      '#type' => 'vertical_tabs',
122
      '#attributes' => array('class' => array('entity-meta')),
123 124
      '#weight' => 99,
    );
125
    $form = parent::form($form, $form_state);
126

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

145
    $form['revision'] = array(
146 147
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
148
      '#default_value' => $node->type->entity->isNewRevision(),
149
      '#access' => $current_user->hasPermission('administer nodes'),
150
      '#group' => 'revision_information',
151 152
    );

153
    $form['revision_log'] += array(
154 155 156 157 158
      '#states' => array(
        'visible' => array(
          ':input[name="revision"]' => array('checked' => TRUE),
        ),
      ),
159
      '#group' => 'revision_information',
160 161 162 163
    );

    // Node author information for administrators.
    $form['author'] = array(
164
      '#type' => 'details',
165
      '#title' => t('Authoring information'),
166
      '#group' => 'advanced',
167 168 169 170
      '#attributes' => array(
        'class' => array('node-form-author'),
      ),
      '#attached' => array(
171
        'library' => array('node/drupal.node'),
172 173
      ),
      '#weight' => 90,
174
      '#optional' => TRUE,
175 176
    );

177 178 179 180 181 182 183
    if (isset($form['uid'])) {
      $form['uid']['#group'] = 'author';
    }

    if (isset($form['created'])) {
      $form['created']['#group'] = 'author';
    }
184 185 186

    // Node options for administrators.
    $form['options'] = array(
187
      '#type' => 'details',
188
      '#title' => t('Promotion options'),
189
      '#group' => 'advanced',
190 191 192 193
      '#attributes' => array(
        'class' => array('node-form-options'),
      ),
      '#attached' => array(
194
        'library' => array('node/drupal.node'),
195 196
      ),
      '#weight' => 95,
197
      '#optional' => TRUE,
198 199
    );

200 201 202
    if (isset($form['promote'])) {
      $form['promote']['#group'] = 'options';
    }
203

204 205 206
    if (isset($form['sticky'])) {
      $form['sticky']['#group'] = 'options';
    }
207

208 209
    $form['#attached']['library'][] = 'node/form';

210
    return $form;
211 212 213
  }

  /**
214
   * {@inheritdoc}
215
   */
216
  protected function actions(array $form, FormStateInterface $form_state) {
217
    $element = parent::actions($form, $form_state);
218
    $node = $this->entity;
219
    $preview_mode = $node->type->entity->getPreviewMode();
220

221
    $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || (!$form_state->getErrors() && $form_state->get('node_preview'));
222

223 224 225 226 227 228
    // 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.
229
    if ($element['submit']['#access'] && \Drupal::currentUser()->hasPermission('administer nodes')) {
230 231 232 233 234 235 236 237 238 239 240
      // 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'];
      $element['publish']['#dropbutton'] = 'save';
      if ($node->isNew()) {
        $element['publish']['#value'] = t('Save and publish');
241
      }
242
      else {
243
        $element['publish']['#value'] = $node->isPublished() ? t('Save and keep published') : t('Save and publish');
244 245
      }
      $element['publish']['#weight'] = 0;
246
      array_unshift($element['publish']['#submit'], '::publish');
247 248 249 250 251 252 253 254

      // Add a "Unpublish" button.
      $element['unpublish'] = $element['submit'];
      $element['unpublish']['#dropbutton'] = 'save';
      if ($node->isNew()) {
        $element['unpublish']['#value'] = t('Save as unpublished');
      }
      else {
255
        $element['unpublish']['#value'] = !$node->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
256 257
      }
      $element['unpublish']['#weight'] = 10;
258
      array_unshift($element['unpublish']['#submit'], '::unpublish');
259

260
      // If already published, the 'publish' button is primary.
261
      if ($node->isPublished()) {
262 263 264 265 266 267 268
        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;
      }
269

270 271 272
      // Remove the "Save" button.
      $element['submit']['#access'] = FALSE;
    }
273 274

    $element['preview'] = array(
275
      '#type' => 'submit',
276
      '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
277
      '#value' => t('Preview'),
278
      '#weight' => 20,
279
      '#validate' => array('::validate'),
280
      '#submit' => array('::submitForm', '::preview'),
281 282
    );

283
    $element['delete']['#access'] = $node->access('delete');
284
    $element['delete']['#weight'] = 100;
285 286 287 288 289

    return $element;
  }

  /**
290
   * {@inheritdoc}
291
   */
292
  public function validate(array $form, FormStateInterface $form_state) {
293 294
    $node = $this->buildEntity($form, $form_state);

295
    if ($node->id() && (node_last_changed($node->id(), $this->getFormLangcode($form_state)) > $node->getChangedTime())) {
296
      $form_state->setErrorByName('changed', $this->t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
297 298
    }

299
    // Invoke hook_node_validate() for validation needed by modules.
300
    // Can't use \Drupal::moduleHandler()->invokeAll(), because $form_state must
301
    // be receivable by reference.
302
    foreach (\Drupal::moduleHandler()->getImplementations('node_validate') as $module) {
303 304 305 306 307 308 309 310
      $function = $module . '_node_validate';
      $function($node, $form, $form_state);
    }

    parent::validate($form, $form_state);
  }

  /**
311 312
   * {@inheritdoc}
   *
313 314 315 316 317 318
   * 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.
   */
319
  public function submitForm(array &$form, FormStateInterface $form_state) {
320
    // Build the node object from the submitted values.
321 322
    parent::submitForm($form, $form_state);
    $node = $this->entity;
323

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

335
    $node->validated = TRUE;
336
    foreach (\Drupal::moduleHandler()->getImplementations('node_submit') as $module) {
337 338 339 340 341 342 343 344 345 346 347
      $function = $module . '_node_submit';
      $function($node, $form, $form_state);
    }
  }

  /**
   * Form submission handler for the 'preview' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
348
   *   The current state of the form.
349
   */
350
  public function preview(array $form, FormStateInterface $form_state) {
351 352 353 354 355 356 357
    $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',
    ));
358 359
  }

360 361 362 363 364 365
  /**
   * Form submission handler for the 'publish' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
366
   *   The current state of the form.
367
   */
368
  public function publish(array $form, FormStateInterface $form_state) {
369
    $node = $this->entity;
370
    $node->setPublished(TRUE);
371 372 373 374 375 376 377 378 379
    return $node;
  }

  /**
   * Form submission handler for the 'unpublish' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
380
   *   The current state of the form.
381
   */
382
  public function unpublish(array $form, FormStateInterface $form_state) {
383
    $node = $this->entity;
384
    $node->setPublished(FALSE);
385 386 387
    return $node;
  }

388 389 390
  /**
   * {@inheritdoc}
   */
391
  public function buildEntity(array $form, FormStateInterface $form_state) {
392
    /** @var \Drupal\node\NodeInterface $entity */
393 394 395
    $entity = parent::buildEntity($form, $form_state);
    // A user might assign the node author by entering a user name in the node
    // form, which we then need to translate to a user ID.
396 397
    // @todo: Remove it when https://www.drupal.org/node/2322525 is pushed.
    if (!empty($form_state->getValue('uid')[0]['target_id']) && $account = User::load($form_state->getValue('uid')[0]['target_id'])) {
398
      $entity->setOwnerId($account->id());
399 400
    }
    else {
401
      $entity->setOwnerId(0);
402 403 404 405
    }
    return $entity;
  }

406
  /**
407
   * {@inheritdoc}
408
   */
409
  public function save(array $form, FormStateInterface $form_state) {
410
    $node = $this->entity;
411
    $insert = $node->isNew();
412
    $node->save();
413
    $node_link = $node->link($this->t('View'));
414
    $context = array('@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link);
415
    $t_args = array('@type' => node_get_type_label($node), '%title' => $node->label());
416 417

    if ($insert) {
418
      $this->logger('content')->notice('@type: added %title.', $context);
419 420 421
      drupal_set_message(t('@type %title has been created.', $t_args));
    }
    else {
422
      $this->logger('content')->notice('@type: updated %title.', $context);
423 424 425
      drupal_set_message(t('@type %title has been updated.', $t_args));
    }

426
    if ($node->id()) {
427
      $form_state->setValue('nid', $node->id());
428
      $form_state->set('nid', $node->id());
429
      if ($node->access('view')) {
430
        $form_state->setRedirect(
431
          'entity.node.canonical',
432
          array('node' => $node->id())
433 434 435
        );
      }
      else {
436
        $form_state->setRedirect('<front>');
437
      }
438 439 440 441 442 443

      // Remove the preview entry from the temp store, if any.
      $store = $this->tempStoreFactory->get('node_preview');
      if ($store->get($node->uuid())) {
        $store->delete($node->uuid());
      }
444 445 446 447 448
    }
    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');
449
      $form_state->setRebuild();
450 451 452 453
    }
  }

}