NodeFormController.php 14.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\node\NodeFormController.
 */

namespace Drupal\node;

10
use Drupal\Core\Datetime\DrupalDateTime;
11
12
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityFormController;
13
14
15
16
17
18
19
20
21
22
23
24

/**
 * Form controller for the node edit forms.
 */
class NodeFormController extends EntityFormController {

  /**
   * Prepares the node object.
   *
   * Fills in a few default values, and then invokes hook_prepare() on the node
   * type module, and hook_node_prepare() on all modules.
   *
25
   * Overrides Drupal\Core\Entity\EntityFormController::prepareEntity().
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
   */
  protected function prepareEntity(EntityInterface $node) {
    // Set up default values, if required.
    $node_options = variable_get('node_options_' . $node->type, array('status', 'promote'));
    // If this is a new node, fill in the default values.
    if (!isset($node->nid) || isset($node->is_new)) {
      foreach (array('status', 'promote', 'sticky') as $key) {
        // Multistep node forms might have filled in something already.
        if (!isset($node->$key)) {
          $node->$key = (int) in_array($key, $node_options);
        }
      }
      global $user;
      $node->uid = $user->uid;
      $node->created = REQUEST_TIME;
    }
    else {
      $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
      // Remove the log message from the original node entity.
      $node->log = NULL;
    }
    // Always use the default revision setting.
48
    $node->setNewRevision(in_array('revision', $node_options));
49
50
51
52
53
54

    node_invoke($node, 'prepare');
    module_invoke_all('node_prepare', $node);
  }

  /**
55
   * Overrides Drupal\Core\Entity\EntityFormController::form().
56
57
   */
  public function form(array $form, array &$form_state, EntityInterface $node) {
58
    $user_config = config('user.settings');
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    // Some special stuff when previewing a node.
    if (isset($form_state['node_preview'])) {
      $form['#prefix'] = $form_state['node_preview'];
      $node->in_preview = TRUE;
    }
    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.
    $form['#attributes']['class'][0] = drupal_html_class('node-' . $node->type . '-form');

    // Basic node information.
    // These elements are just values so they are not even sent to the client.
    foreach (array('nid', 'vid', 'uid', 'created', 'type') as $key) {
      $form[$key] = array(
        '#type' => 'value',
        '#value' => isset($node->$key) ? $node->$key : NULL,
      );
    }

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

    // Invoke hook_form() to get the node-specific bits. Can't use node_invoke()
    // because hook_form() needs to be able to receive $form_state by reference.
    // @todo hook_form() implementations are unable to add #validate or #submit
    //   handlers to the form buttons below. Remove hook_form() entirely.
92
93
    $function = node_hook($node->type, 'form');
    if ($function && ($extra = $function($node, $form_state))) {
94
95
96
97
98
99
100
101
      $form = array_merge_recursive($form, $extra);
    }
    // If the node type has a title, and the node type form defined no special
    // weight for it, we default to a weight of -5 for consistency.
    if (isset($form['title']) && !isset($form['title']['#weight'])) {
      $form['title']['#weight'] = -5;
    }

102
    $language_configuration = module_invoke('language', 'get_default_configuration', 'node', $node->type);
103
104
105
106
107
    $form['langcode'] = array(
      '#title' => t('Language'),
      '#type' => 'language_select',
      '#default_value' => $node->langcode,
      '#languages' => LANGUAGE_ALL,
108
      '#access' => isset($language_configuration['language_hidden']) && !$language_configuration['language_hidden'],
109
    );
110
111
112
113
114
115
116
117
118

    $form['additional_settings'] = array(
      '#type' => 'vertical_tabs',
      '#weight' => 99,
    );

    // Add a log field if the "Create new revision" option is checked, or if the
    // current user has the ability to check that option.
    $form['revision_information'] = array(
119
      '#type' => 'details',
120
121
      '#title' => t('Revision information'),
      '#collapsible' => TRUE,
122
123
      // Collapsed by default when "Create new revision" is unchecked.
      '#collapsed' => !$node->isNewRevision(),
124
125
126
127
128
129
130
131
      '#group' => 'additional_settings',
      '#attributes' => array(
        'class' => array('node-form-revision-information'),
      ),
      '#attached' => array(
        'js' => array(drupal_get_path('module', 'node') . '/node.js'),
      ),
      '#weight' => 20,
132
      '#access' => $node->isNewRevision() || user_access('administer nodes'),
133
134
135
136
137
    );

    $form['revision_information']['revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
138
      '#default_value' => $node->isNewRevision(),
139
140
141
142
143
144
      '#access' => user_access('administer nodes'),
    );

    // Check the revision log checkbox when the log textarea is filled in.
    // This must not happen if "Create new revision" is enabled by default,
    // since the state would auto-disable the checkbox otherwise.
145
    if (!$node->isNewRevision()) {
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
      $form['revision_information']['revision']['#states'] = array(
        'checked' => array(
          'textarea[name="log"]' => array('empty' => FALSE),
        ),
      );
    }

    $form['revision_information']['log'] = array(
      '#type' => 'textarea',
      '#title' => t('Revision log message'),
      '#rows' => 4,
      '#default_value' => !empty($node->log) ? $node->log : '',
      '#description' => t('Briefly describe the changes you have made.'),
    );

    // Node author information for administrators.
    $form['author'] = array(
163
      '#type' => 'details',
164
165
166
167
168
169
170
171
172
173
174
175
176
      '#access' => user_access('administer nodes'),
      '#title' => t('Authoring information'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'additional_settings',
      '#attributes' => array(
        'class' => array('node-form-author'),
      ),
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'node') . '/node.js',
          array(
            'type' => 'setting',
177
            'data' => array('anonymous' => $user_config->get('anonymous')),
178
179
180
181
182
183
184
185
186
187
188
189
190
          ),
        ),
      ),
      '#weight' => 90,
    );

    $form['author']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Authored by'),
      '#maxlength' => 60,
      '#autocomplete_path' => 'user/autocomplete',
      '#default_value' => !empty($node->name) ? $node->name : '',
      '#weight' => -1,
191
      '#description' => t('Leave blank for %anonymous.', array('%anonymous' => $user_config->get('anonymous'))),
192
193
194
195
196
197
198
199
200
201
202
203
    );

    $form['author']['date'] = array(
      '#type' => 'textfield',
      '#title' => t('Authored on'),
      '#maxlength' => 25,
      '#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->created, 'custom', 'Y-m-d H:i:s O'), '%timezone' => !empty($node->date) ? date_format(date_create($node->date), 'O') : format_date($node->created, 'custom', 'O'))),
      '#default_value' => !empty($node->date) ? $node->date : '',
    );

    // Node options for administrators.
    $form['options'] = array(
204
      '#type' => 'details',
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
      '#access' => user_access('administer nodes'),
      '#title' => t('Publishing options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#group' => 'additional_settings',
      '#attributes' => array(
        'class' => array('node-form-options'),
      ),
      '#attached' => array(
        'js' => array(drupal_get_path('module', 'node') . '/node.js'),
      ),
      '#weight' => 95,
    );

    $form['options']['status'] = array(
      '#type' => 'checkbox',
      '#title' => t('Published'),
      '#default_value' => $node->status,
    );

    $form['options']['promote'] = array(
      '#type' => 'checkbox',
      '#title' => t('Promoted to front page'),
      '#default_value' => $node->promote,
    );

    $form['options']['sticky'] = array(
      '#type' => 'checkbox',
      '#title' => t('Sticky at top of lists'),
      '#default_value' => $node->sticky,
    );

    // This form uses a button-level #submit handler for the form's main submit
    // action. node_form_submit() manually invokes all form-level #submit
    // handlers of the form. Without explicitly setting #submit, Form API would
    // auto-detect node_form_submit() as submit handler, but that is the
    // button-level #submit handler for the 'Save' action.
    $form += array('#submit' => array());

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

  /**
248
   * Overrides Drupal\Core\Entity\EntityFormController::actions().
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
   */
  protected function actions(array $form, array &$form_state) {
    $element = parent::actions($form, $form_state);
    $node = $this->getEntity($form_state);
    $preview_mode = variable_get('node_preview_' . $node->type, DRUPAL_OPTIONAL);

    $element['preview'] = array(
      '#access' => $preview_mode != DRUPAL_DISABLED,
      '#value' => t('Preview'),
      '#validate' => array(
        array($this, 'validate'),
      ),
      '#submit' => array(
        array($this, 'submit'),
        array($this, 'preview'),
      ),
    );

    $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || (!form_get_errors() && isset($form_state['node_preview']));
    $element['delete']['#access'] = node_access('delete', $node);

    return $element;
  }

  /**
274
   * Overrides Drupal\Core\Entity\EntityFormController::validate().
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
   */
  public function validate(array $form, array &$form_state) {
    $node = $this->buildEntity($form, $form_state);

    if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
      form_set_error('changed', 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.'));
    }

    // Validate the "authored by" field.
    if (!empty($node->name) && !($account = user_load_by_name($node->name))) {
      // 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.
      form_set_error('name', t('The username %name does not exist.', array('%name' => $node->name)));
    }

    // Validate the "authored on" field.
292
293
    $date = new DrupalDateTime($node->date);
    if ($date->hasErrors()) {
294
295
296
297
298
299
300
      form_set_error('date', t('You have to specify a valid date.'));
    }

    // Invoke hook_validate() for node type specific validation and
    // hook_node_validate() for miscellaneous validation needed by modules.
    // Can't use node_invoke() or module_invoke_all(), because $form_state must
    // be receivable by reference.
301
    if ($function = node_hook($node->type, 'validate')) {
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
      $function($node, $form, $form_state);
    }
    foreach (module_implements('node_validate') as $module) {
      $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.
   *
319
   * Overrides Drupal\Core\Entity\EntityFormController::submit().
320
321
322
323
324
   */
  public function submit(array $form, array &$form_state) {
    // Build the node object from the submitted values.
    $node = parent::submit($form, $form_state);

325
326
327
328
329
    // Save as a new revision if requested to do so.
    if (!empty($form_state['values']['revision'])) {
      $node->setNewRevision();
    }

330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
    node_submit($node);
    foreach (module_implements('node_submit') as $module) {
      $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) {
    drupal_set_title(t('Preview'), PASS_THROUGH);
    $form_state['node_preview'] = node_preview($this->getEntity($form_state));
    $form_state['rebuild'] = TRUE;
  }

  /**
354
   * Overrides Drupal\Core\Entity\EntityFormController::save().
355
356
357
358
359
360
361
   */
  public function save(array $form, array &$form_state) {
    $node = $this->getEntity($form_state);
    $insert = empty($node->nid);
    $node->save();
    $node_link = l(t('view'), 'node/' . $node->nid);
    $watchdog_args = array('@type' => $node->type, '%title' => $node->label());
362
    $t_args = array('@type' => node_get_type_label($node), '%title' => $node->label());
363
364
365
366
367
368
369
370
371
372
373
374
375

    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));
    }

    if ($node->nid) {
      $form_state['values']['nid'] = $node->nid;
      $form_state['nid'] = $node->nid;
376
      $form_state['redirect'] = node_access('view', $node) ? 'node/' . $node->nid : '<front>';
377
378
379
380
381
382
383
384
385
    }
    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.
386
    cache_invalidate_tags(array('content' => TRUE));
387
388
389
  }

  /**
390
   * Overrides Drupal\Core\Entity\EntityFormController::delete().
391
392
393
394
395
396
397
398
399
400
   */
  public function delete(array $form, array &$form_state) {
    $destination = array();
    if (isset($_GET['destination'])) {
      $destination = drupal_get_destination();
      unset($_GET['destination']);
    }
    $node = $this->getEntity($form_state);
    $form_state['redirect'] = array('node/' . $node->nid . '/delete', array('query' => $destination));
  }
401

402
}