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

namespace Drupal\node;

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\user\PrivateTempStoreFactory;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12 13

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

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

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager'),
48 49 50
      $container->get('user.private_tempstore'),
      $container->get('entity_type.bundle.info'),
      $container->get('datetime.time')
51 52 53
    );
  }

54
  /**
55
   * {@inheritdoc}
56
   */
57
  public function form(array $form, FormStateInterface $form_state) {
58 59
    // Try to restore from temp store, this must be done before calling
    // parent::form().
60 61
    $store = $this->tempStoreFactory->get('node_preview');

62 63 64 65
    // Attempt to load from preview when the uuid is present unless we are
    // rebuilding the form.
    $request_uuid = \Drupal::request()->query->get('uuid');
    if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
66
      /** @var $preview \Drupal\Core\Form\FormStateInterface */
67

68 69
      $form_state->setStorage($preview->getStorage());
      $form_state->setUserInput($preview->getUserInput());
70 71

      // Rebuild the form.
72
      $form_state->setRebuild();
73 74 75 76 77 78

      // The combination of having user input and rebuilding the form means
      // that it will attempt to cache the form state which will fail if it is
      // a GET request.
      $form_state->setRequestMethod('POST');

79
      $this->entity = $preview->getFormObject()->getEntity();
80
      $this->entity->in_preview = NULL;
81

82
      $form_state->set('has_been_previewed', TRUE);
83 84
    }

85
    /** @var \Drupal\node\NodeInterface $node */
86
    $node = $this->entity;
87

88
    if ($this->operation == 'edit') {
89
      $form['#title'] = $this->t('<em>Edit @type</em> @title', ['@type' => node_get_type_label($node), '@title' => $node->label()]);
90 91
    }

92
    // Changed must be sent to the client, for later overwrite error checking.
93
    $form['changed'] = [
94
      '#type' => 'hidden',
95
      '#default_value' => $node->getChangedTime(),
96
    ];
97

98
    $form = parent::form($form, $form_state);
99

100
    $form['advanced']['#attributes']['class'][] = 'entity-meta';
101 102

    // Node author information for administrators.
103
    $form['author'] = [
104
      '#type' => 'details',
105
      '#title' => t('Authoring information'),
106
      '#group' => 'advanced',
107 108 109 110 111 112
      '#attributes' => [
        'class' => ['node-form-author'],
      ],
      '#attached' => [
        'library' => ['node/drupal.node'],
      ],
113
      '#weight' => 90,
114
      '#optional' => TRUE,
115
    ];
116

117 118 119 120 121 122 123
    if (isset($form['uid'])) {
      $form['uid']['#group'] = 'author';
    }

    if (isset($form['created'])) {
      $form['created']['#group'] = 'author';
    }
124 125

    // Node options for administrators.
126
    $form['options'] = [
127
      '#type' => 'details',
128
      '#title' => t('Promotion options'),
129
      '#group' => 'advanced',
130 131 132 133 134 135
      '#attributes' => [
        'class' => ['node-form-options'],
      ],
      '#attached' => [
        'library' => ['node/drupal.node'],
      ],
136
      '#weight' => 95,
137
      '#optional' => TRUE,
138
    ];
139

140 141 142
    if (isset($form['promote'])) {
      $form['promote']['#group'] = 'options';
    }
143

144 145 146
    if (isset($form['sticky'])) {
      $form['sticky']['#group'] = 'options';
    }
147

148 149
    $form['#attached']['library'][] = 'node/form';

150
    $form['#entity_builders']['update_status'] = '::updateStatus';
151

152
    return $form;
153 154
  }

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  /**
   * 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']);
    }
  }

176
  /**
177
   * {@inheritdoc}
178
   */
179
  protected function actions(array $form, FormStateInterface $form_state) {
180
    $element = parent::actions($form, $form_state);
181
    $node = $this->entity;
182
    $preview_mode = $node->type->entity->getPreviewMode();
183

184
    $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
185

186 187 188 189 190 191
    // 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.
192
    if ($element['submit']['#access'] && \Drupal::currentUser()->hasPermission('administer nodes')) {
193 194 195 196 197 198 199 200
      // 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'];
201 202
      // If the "Publish" button is clicked, we want to update the status to "published".
      $element['publish']['#published_status'] = TRUE;
203 204 205
      $element['publish']['#dropbutton'] = 'save';
      if ($node->isNew()) {
        $element['publish']['#value'] = t('Save and publish');
206
      }
207
      else {
208
        $element['publish']['#value'] = $node->isPublished() ? t('Save and keep published') : t('Save and publish');
209 210 211 212 213
      }
      $element['publish']['#weight'] = 0;

      // Add a "Unpublish" button.
      $element['unpublish'] = $element['submit'];
214 215
      // If the "Unpublish" button is clicked, we want to update the status to "unpublished".
      $element['unpublish']['#published_status'] = FALSE;
216 217 218 219 220
      $element['unpublish']['#dropbutton'] = 'save';
      if ($node->isNew()) {
        $element['unpublish']['#value'] = t('Save as unpublished');
      }
      else {
221
        $element['unpublish']['#value'] = !$node->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
222 223
      }
      $element['unpublish']['#weight'] = 10;
224

225
      // If already published, the 'publish' button is primary.
226
      if ($node->isPublished()) {
227 228 229 230 231 232 233
        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;
      }
234

235 236 237
      // Remove the "Save" button.
      $element['submit']['#access'] = FALSE;
    }
238

239
    $element['preview'] = [
240
      '#type' => 'submit',
241
      '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
242
      '#value' => t('Preview'),
243
      '#weight' => 20,
244 245
      '#submit' => ['::submitForm', '::preview'],
    ];
246

247
    $element['delete']['#access'] = $node->access('delete');
248
    $element['delete']['#weight'] = 100;
249 250 251 252 253 254 255 256 257 258

    return $element;
  }

  /**
   * Form submission handler for the 'preview' action.
   *
   * @param $form
   *   An associative array containing the structure of the form.
   * @param $form_state
259
   *   The current state of the form.
260
   */
261
  public function preview(array $form, FormStateInterface $form_state) {
262 263 264
    $store = $this->tempStoreFactory->get('node_preview');
    $this->entity->in_preview = TRUE;
    $store->set($this->entity->uuid(), $form_state);
265 266

    $route_parameters = [
267
      'node_preview' => $this->entity->uuid(),
268
      'view_mode_id' => 'full',
269 270 271 272 273 274 275 276 277
    ];

    $options = [];
    $query = $this->getRequest()->query;
    if ($query->has('destination')) {
      $options['query']['destination'] = $query->get('destination');
      $query->remove('destination');
    }
    $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
278 279 280
  }

  /**
281
   * {@inheritdoc}
282
   */
283
  public function save(array $form, FormStateInterface $form_state) {
284
    $node = $this->entity;
285
    $insert = $node->isNew();
286
    $node->save();
287
    $node_link = $node->link($this->t('View'));
288 289
    $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
    $t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())];
290 291

    if ($insert) {
292
      $this->logger('content')->notice('@type: added %title.', $context);
293 294 295
      drupal_set_message(t('@type %title has been created.', $t_args));
    }
    else {
296
      $this->logger('content')->notice('@type: updated %title.', $context);
297 298 299
      drupal_set_message(t('@type %title has been updated.', $t_args));
    }

300
    if ($node->id()) {
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()]
307 308 309
        );
      }
      else {
310
        $form_state->setRedirect('<front>');
311
      }
312 313 314

      // Remove the preview entry from the temp store, if any.
      $store = $this->tempStoreFactory->get('node_preview');
315
      $store->delete($node->uuid());
316 317 318 319 320
    }
    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');
321
      $form_state->setRebuild();
322 323 324 325
    }
  }

}