NodeForm.php 16.9 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\NestedArray;
11
use Drupal\Core\Cache\Cache;
12
use Drupal\Core\Datetime\DrupalDateTime;
13
use Drupal\Core\Entity\ContentEntityForm;
14
use Drupal\Core\Language\Language;
15
use Drupal\Component\Utility\String;
16 17 18 19

/**
 * Form controller for the node edit forms.
 */
20
class NodeForm extends ContentEntityForm {
21

22 23 24 25 26 27 28
  /**
   * Default settings for this content/node type.
   *
   * @var array
   */
  protected $settings;

29
  /**
30
   * {@inheritdoc}
31
   */
32
  protected function prepareEntity() {
33
    /** @var \Drupal\node\NodeInterface $node */
34
    $node = $this->entity;
35
    // Set up default values, if required.
36 37 38
    $type = entity_load('node_type', $node->bundle());
    $this->settings = $type->getModuleSettings('node');

39
    if (!$node->isNew()) {
40
      $node->date = format_date($node->getCreatedTime(), 'custom', 'Y-m-d H:i:s O');
41 42
      // Remove the revision log message from the original node entity.
      $node->revision_log = NULL;
43 44 45 46
    }
  }

  /**
47
   * Overrides Drupal\Core\Entity\EntityForm::form().
48
   */
49
  public function form(array $form, array &$form_state) {
50
    /** @var \Drupal\node\NodeInterface $node */
51
    $node = $this->entity;
52

53
    if ($this->operation == 'edit') {
54
      $form['#title'] = $this->t('<em>Edit @type</em> @title', array('@type' => node_get_type_label($node), '@title' => $node->label()));
55 56
    }

57
    $user_config = \Drupal::config('user.settings');
58 59 60 61
    // Some special stuff when previewing a node.
    if (isset($form_state['node_preview'])) {
      $form['#prefix'] = $form_state['node_preview'];
      $node->in_preview = TRUE;
62
      $form['#title'] = $this->t('Preview');
63 64 65 66 67 68 69 70
    }
    else {
      unset($node->in_preview);
    }

    // Override the default CSS class name, since the user-defined node type
    // name in 'TYPE-node-form' potentially clashes with third-party class
    // names.
71
    $form['#attributes']['class'][0] = drupal_html_class('node-' . $node->getType() . '-form');
72 73 74 75

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

79
    $language_configuration = \Drupal::moduleHandler()->invoke('language', 'get_default_configuration', array('node', $node->getType()));
80 81 82
    $form['langcode'] = array(
      '#title' => t('Language'),
      '#type' => 'language_select',
83
      '#default_value' => $node->getUntranslated()->language()->id,
84
      '#languages' => Language::STATE_ALL,
85
      '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
86
    );
87

88
    $form['advanced'] = array(
89
      '#type' => 'vertical_tabs',
90
      '#attributes' => array('class' => array('entity-meta')),
91 92 93
      '#weight' => 99,
    );

94 95
    // Add a revision log field if the "Create new revision" option is checked,
    // or if the current user has the ability to check that option.
96
    $form['revision_information'] = array(
97 98
      '#type' => 'details',
      '#group' => 'advanced',
99
      '#title' => t('Revision information'),
100 101
      // Open by default when "Create new revision" is checked.
      '#open' => $node->isNewRevision(),
102 103 104 105
      '#attributes' => array(
        'class' => array('node-form-revision-information'),
      ),
      '#attached' => array(
106
        'library' => array('node/drupal.node'),
107 108
      ),
      '#weight' => 20,
109
      '#optional' => TRUE,
110 111
    );

112
    $form['revision'] = array(
113 114
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
115
      '#default_value' => !empty($this->settings['options']['revision']),
116 117
      '#access' => $node->isNewRevision() || user_access('administer nodes'),
      '#group' => 'revision_information',
118 119
    );

120
    $form['revision_log'] = array(
121 122 123
      '#type' => 'textarea',
      '#title' => t('Revision log message'),
      '#rows' => 4,
124
      '#default_value' => !empty($node->revision_log->value) ? $node->revision_log->value : '',
125
      '#description' => t('Briefly describe the changes you have made.'),
126 127 128 129 130
      '#states' => array(
        'visible' => array(
          ':input[name="revision"]' => array('checked' => TRUE),
        ),
      ),
131 132
      '#group' => 'revision_information',
      '#access' => $node->isNewRevision() || user_access('administer nodes'),
133 134 135 136
    );

    // Node author information for administrators.
    $form['author'] = array(
137
      '#type' => 'details',
138
      '#title' => t('Authoring information'),
139
      '#group' => 'advanced',
140 141 142 143
      '#attributes' => array(
        'class' => array('node-form-author'),
      ),
      '#attached' => array(
144
        'library' => array('node/drupal.node'),
145 146 147
        'js' => array(
          array(
            'type' => 'setting',
148
            'data' => array('anonymous' => $user_config->get('anonymous')),
149 150 151 152
          ),
        ),
      ),
      '#weight' => 90,
153
      '#optional' => TRUE,
154 155
    );

156
    $form['uid'] = array(
157 158 159
      '#type' => 'textfield',
      '#title' => t('Authored by'),
      '#maxlength' => 60,
160
      '#autocomplete_route_name' => 'user.autocomplete',
161
      '#default_value' => $node->getOwnerId()? $node->getOwner()->getUsername() : '',
162
      '#weight' => -1,
163
      '#description' => t('Leave blank for %anonymous.', array('%anonymous' => $user_config->get('anonymous'))),
164 165
      '#group' => 'author',
      '#access' => user_access('administer nodes'),
166
    );
167
    $form['created'] = array(
168
      '#type' => 'textfield',
169
      '#title' => t('Authored on'),
170
      '#maxlength' => 25,
171
      '#description' => t('Format: %time. The date format is YYYY-MM-DD and %timezone is the time zone offset from UTC. Leave blank to use the time of form submission.', array('%time' => !empty($node->date) ? date_format(date_create($node->date), 'Y-m-d H:i:s O') : format_date($node->getCreatedTime(), 'custom', 'Y-m-d H:i:s O'), '%timezone' => !empty($node->date) ? date_format(date_create($node->date), 'O') : format_date($node->getCreatedTime(), 'custom', 'O'))),
172
      '#default_value' => !empty($node->date) ? $node->date : '',
173 174
      '#group' => 'author',
      '#access' => user_access('administer nodes'),
175 176 177 178
    );

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

192
    $form['promote'] = array(
193 194
      '#type' => 'checkbox',
      '#title' => t('Promoted to front page'),
195
      '#default_value' => $node->isPromoted(),
196 197
      '#group' => 'options',
      '#access' => user_access('administer nodes'),
198 199
    );

200
    $form['sticky'] = array(
201 202
      '#type' => 'checkbox',
      '#title' => t('Sticky at top of lists'),
203
      '#default_value' => $node->isSticky(),
204 205
      '#group' => 'options',
      '#access' => user_access('administer nodes'),
206 207 208 209 210 211
    );

    return parent::form($form, $form_state, $node);
  }

  /**
212
   * Overrides Drupal\Core\Entity\EntityForm::actions().
213
   */
214 215
  protected function actions(array $form, array &$form_state) {
    $element = parent::actions($form, $form_state);
216
    $node = $this->entity;
217
    $preview_mode = $this->settings['preview'];
218

219
    $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || (!form_get_errors($form_state) && isset($form_state['node_preview']));
220

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

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

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

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

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

286
    $element['delete']['#access'] = $node->access('delete');
287
    $element['delete']['#weight'] = 100;
288 289 290 291 292

    return $element;
  }

  /**
293
   * Overrides Drupal\Core\Entity\EntityForm::validate().
294 295 296 297
   */
  public function validate(array $form, array &$form_state) {
    $node = $this->buildEntity($form, $form_state);

298
    if ($node->id() && (node_last_changed($node->id(), $this->getFormLangcode($form_state)) > $node->getChangedTime())) {
299
      $this->setFormError('changed', $form_state, $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.'));
300 301 302
    }

    // Validate the "authored by" field.
303
    if (!empty($form_state['values']['uid']) && !user_load_by_name($form_state['values']['uid'])) {
304 305 306
      // The use of empty() is mandatory in the context of usernames
      // as the empty string denotes the anonymous user. In case we
      // are dealing with an anonymous user we set the user ID to 0.
307
      $this->setFormError('uid', $form_state, $this->t('The username %name does not exist.', array('%name' => $form_state['values']['uid'])));
308 309 310
    }

    // Validate the "authored on" field.
311
    // The date element contains the date object.
312 313
    $date = $node->date instanceof DrupalDateTime ? $node->date : new DrupalDateTime($node->date);
    if ($date->hasErrors()) {
314
      $this->setFormError('date', $form_state, $this->t('You have to specify a valid date.'));
315 316
    }

317
    // Invoke hook_node_validate() for validation needed by modules.
318
    // Can't use \Drupal::moduleHandler()->invokeAll(), because $form_state must
319
    // be receivable by reference.
320
    foreach (\Drupal::moduleHandler()->getImplementations('node_validate') as $module) {
321 322 323 324 325 326 327 328 329 330 331 332 333 334
      $function = $module . '_node_validate';
      $function($node, $form, $form_state);
    }

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

  /**
   * 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.
   *
335
   * Overrides Drupal\Core\Entity\EntityForm::submit().
336 337 338 339 340
   */
  public function submit(array $form, array &$form_state) {
    // Build the node object from the submitted values.
    $node = parent::submit($form, $form_state);

341
    // Save as a new revision if requested to do so.
342
    if (!empty($form_state['values']['revision']) && $form_state['values']['revision'] != FALSE) {
343
      $node->setNewRevision();
344 345
      // If a new revision is created, save the current user as revision author.
      $node->setRevisionCreationTime(REQUEST_TIME);
346
      $node->setRevisionAuthorId(\Drupal::currentUser()->id());
347
    }
348 349 350
    else {
      $node->setNewRevision(FALSE);
    }
351

352
    $node->validated = TRUE;
353
    foreach (\Drupal::moduleHandler()->getImplementations('node_submit') as $module) {
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
      $function = $module . '_node_submit';
      $function($node, $form, $form_state);
    }

    return $node;
  }

  /**
   * Form submission handler for the 'preview' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function preview(array $form, array &$form_state) {
370 371 372
    // @todo Remove this: we should not have explicit includes in autoloaded
    //   classes.
    module_load_include('inc', 'node', 'node.pages');
373
    $form_state['node_preview'] = node_preview($this->entity, $form_state);
374 375 376
    $form_state['rebuild'] = TRUE;
  }

377 378 379 380 381 382 383 384 385
  /**
   * Form submission handler for the 'publish' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function publish(array $form, array &$form_state) {
386
    $node = $this->entity;
387
    $node->setPublished(TRUE);
388 389 390 391 392 393 394 395 396 397 398 399
    return $node;
  }

  /**
   * Form submission handler for the 'unpublish' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
   *   A reference to a keyed array containing the current state of the form.
   */
  public function unpublish(array $form, array &$form_state) {
400
    $node = $this->entity;
401
    $node->setPublished(FALSE);
402 403 404
    return $node;
  }

405 406 407 408
  /**
   * {@inheritdoc}
   */
  public function buildEntity(array $form, array &$form_state) {
409
    /** @var \Drupal\node\NodeInterface $entity */
410 411 412
    $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.
413
    if (!empty($form_state['values']['uid']) && $account = user_load_by_name($form_state['values']['uid'])) {
414
      $entity->setOwnerId($account->id());
415 416
    }
    else {
417
      $entity->setOwnerId(0);
418 419
    }

420 421
    if (!empty($form_state['values']['created']) && $form_state['values']['created'] instanceOf DrupalDateTime) {
      $entity->setCreatedTime($form_state['values']['created']->getTimestamp());
422 423 424 425 426 427 428 429
    }
    else {
      $entity->setCreatedTime(REQUEST_TIME);
    }
    return $entity;
  }


430
  /**
431
   * Overrides Drupal\Core\Entity\EntityForm::save().
432 433
   */
  public function save(array $form, array &$form_state) {
434
    $node = $this->entity;
435
    $insert = $node->isNew();
436
    $node->save();
437
    $node_link = l(t('View'), 'node/' . $node->id());
438
    $watchdog_args = array('@type' => $node->getType(), '%title' => $node->label());
439
    $t_args = array('@type' => node_get_type_label($node), '%title' => $node->label());
440 441 442 443 444 445 446 447 448 449

    if ($insert) {
      watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
      drupal_set_message(t('@type %title has been created.', $t_args));
    }
    else {
      watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
      drupal_set_message(t('@type %title has been updated.', $t_args));
    }

450 451 452
    if ($node->id()) {
      $form_state['values']['nid'] = $node->id();
      $form_state['nid'] = $node->id();
453 454 455 456 457 458 459 460 461 462 463
      if ($node->access('view')) {
        $form_state['redirect_route'] = array(
          'route_name' => 'node.view',
          'route_parameters' => array(
            'node' => $node->id(),
          ),
        );
      }
      else {
        $form_state['redirect_route']['route_name'] = '<front>';
      }
464 465 466 467 468 469 470 471 472
    }
    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');
      $form_state['rebuild'] = TRUE;
    }

    // Clear the page and block caches.
473
    Cache::invalidateTags(array('content' => TRUE));
474 475 476
  }

}