annotate core/modules/node/src/NodeForm.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\node;
Chris@0 4
Chris@0 5 use Drupal\Component\Datetime\TimeInterface;
Chris@0 6 use Drupal\Core\Entity\ContentEntityForm;
Chris@4 7 use Drupal\Core\Entity\EntityRepositoryInterface;
Chris@0 8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
Chris@0 9 use Drupal\Core\Form\FormStateInterface;
Chris@0 10 use Drupal\Core\Session\AccountInterface;
Chris@0 11 use Drupal\Core\TempStore\PrivateTempStoreFactory;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Form handler for the node edit forms.
Chris@0 16 *
Chris@0 17 * @internal
Chris@0 18 */
Chris@0 19 class NodeForm extends ContentEntityForm {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The tempstore factory.
Chris@0 23 *
Chris@0 24 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
Chris@0 25 */
Chris@0 26 protected $tempStoreFactory;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The Current User object.
Chris@0 30 *
Chris@0 31 * @var \Drupal\Core\Session\AccountInterface
Chris@0 32 */
Chris@0 33 protected $currentUser;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Constructs a NodeForm object.
Chris@0 37 *
Chris@4 38 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
Chris@4 39 * The entity repository.
Chris@0 40 * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
Chris@0 41 * The factory for the temp store object.
Chris@0 42 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
Chris@0 43 * The entity type bundle service.
Chris@0 44 * @param \Drupal\Component\Datetime\TimeInterface $time
Chris@0 45 * The time service.
Chris@0 46 * @param \Drupal\Core\Session\AccountInterface $current_user
Chris@0 47 * The current user.
Chris@0 48 */
Chris@4 49 public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
Chris@4 50 parent::__construct($entity_repository, $entity_type_bundle_info, $time);
Chris@0 51 $this->tempStoreFactory = $temp_store_factory;
Chris@0 52 $this->currentUser = $current_user;
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * {@inheritdoc}
Chris@0 57 */
Chris@0 58 public static function create(ContainerInterface $container) {
Chris@0 59 return new static(
Chris@4 60 $container->get('entity.repository'),
Chris@0 61 $container->get('tempstore.private'),
Chris@0 62 $container->get('entity_type.bundle.info'),
Chris@0 63 $container->get('datetime.time'),
Chris@0 64 $container->get('current_user')
Chris@0 65 );
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function form(array $form, FormStateInterface $form_state) {
Chris@0 72 // Try to restore from temp store, this must be done before calling
Chris@0 73 // parent::form().
Chris@0 74 $store = $this->tempStoreFactory->get('node_preview');
Chris@0 75
Chris@0 76 // Attempt to load from preview when the uuid is present unless we are
Chris@0 77 // rebuilding the form.
Chris@0 78 $request_uuid = \Drupal::request()->query->get('uuid');
Chris@0 79 if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
Chris@0 80 /** @var $preview \Drupal\Core\Form\FormStateInterface */
Chris@0 81
Chris@0 82 $form_state->setStorage($preview->getStorage());
Chris@0 83 $form_state->setUserInput($preview->getUserInput());
Chris@0 84
Chris@0 85 // Rebuild the form.
Chris@0 86 $form_state->setRebuild();
Chris@0 87
Chris@0 88 // The combination of having user input and rebuilding the form means
Chris@0 89 // that it will attempt to cache the form state which will fail if it is
Chris@0 90 // a GET request.
Chris@0 91 $form_state->setRequestMethod('POST');
Chris@0 92
Chris@0 93 $this->entity = $preview->getFormObject()->getEntity();
Chris@0 94 $this->entity->in_preview = NULL;
Chris@0 95
Chris@0 96 $form_state->set('has_been_previewed', TRUE);
Chris@0 97 }
Chris@0 98
Chris@0 99 /** @var \Drupal\node\NodeInterface $node */
Chris@0 100 $node = $this->entity;
Chris@0 101
Chris@0 102 if ($this->operation == 'edit') {
Chris@0 103 $form['#title'] = $this->t('<em>Edit @type</em> @title', [
Chris@0 104 '@type' => node_get_type_label($node),
Chris@4 105 '@title' => $node->label(),
Chris@0 106 ]);
Chris@0 107 }
Chris@0 108
Chris@0 109 // Changed must be sent to the client, for later overwrite error checking.
Chris@0 110 $form['changed'] = [
Chris@0 111 '#type' => 'hidden',
Chris@0 112 '#default_value' => $node->getChangedTime(),
Chris@0 113 ];
Chris@0 114
Chris@0 115 $form = parent::form($form, $form_state);
Chris@0 116
Chris@0 117 $form['advanced']['#attributes']['class'][] = 'entity-meta';
Chris@0 118
Chris@0 119 $form['meta'] = [
Chris@0 120 '#type' => 'details',
Chris@0 121 '#group' => 'advanced',
Chris@0 122 '#weight' => -10,
Chris@0 123 '#title' => $this->t('Status'),
Chris@0 124 '#attributes' => ['class' => ['entity-meta__header']],
Chris@0 125 '#tree' => TRUE,
Chris@0 126 '#access' => $this->currentUser->hasPermission('administer nodes'),
Chris@0 127 ];
Chris@0 128 $form['meta']['published'] = [
Chris@0 129 '#type' => 'item',
Chris@0 130 '#markup' => $node->isPublished() ? $this->t('Published') : $this->t('Not published'),
Chris@0 131 '#access' => !$node->isNew(),
Chris@0 132 '#wrapper_attributes' => ['class' => ['entity-meta__title']],
Chris@0 133 ];
Chris@0 134 $form['meta']['changed'] = [
Chris@0 135 '#type' => 'item',
Chris@0 136 '#title' => $this->t('Last saved'),
Chris@0 137 '#markup' => !$node->isNew() ? format_date($node->getChangedTime(), 'short') : $this->t('Not saved yet'),
Chris@0 138 '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']],
Chris@0 139 ];
Chris@0 140 $form['meta']['author'] = [
Chris@0 141 '#type' => 'item',
Chris@0 142 '#title' => $this->t('Author'),
Chris@0 143 '#markup' => $node->getOwner()->getUsername(),
Chris@0 144 '#wrapper_attributes' => ['class' => ['entity-meta__author']],
Chris@0 145 ];
Chris@0 146
Chris@0 147 $form['status']['#group'] = 'footer';
Chris@0 148
Chris@0 149 // Node author information for administrators.
Chris@0 150 $form['author'] = [
Chris@0 151 '#type' => 'details',
Chris@0 152 '#title' => t('Authoring information'),
Chris@0 153 '#group' => 'advanced',
Chris@0 154 '#attributes' => [
Chris@0 155 'class' => ['node-form-author'],
Chris@0 156 ],
Chris@0 157 '#attached' => [
Chris@0 158 'library' => ['node/drupal.node'],
Chris@0 159 ],
Chris@0 160 '#weight' => 90,
Chris@0 161 '#optional' => TRUE,
Chris@0 162 ];
Chris@0 163
Chris@0 164 if (isset($form['uid'])) {
Chris@0 165 $form['uid']['#group'] = 'author';
Chris@0 166 }
Chris@0 167
Chris@0 168 if (isset($form['created'])) {
Chris@0 169 $form['created']['#group'] = 'author';
Chris@0 170 }
Chris@0 171
Chris@0 172 // Node options for administrators.
Chris@0 173 $form['options'] = [
Chris@0 174 '#type' => 'details',
Chris@0 175 '#title' => t('Promotion options'),
Chris@0 176 '#group' => 'advanced',
Chris@0 177 '#attributes' => [
Chris@0 178 'class' => ['node-form-options'],
Chris@0 179 ],
Chris@0 180 '#attached' => [
Chris@0 181 'library' => ['node/drupal.node'],
Chris@0 182 ],
Chris@0 183 '#weight' => 95,
Chris@0 184 '#optional' => TRUE,
Chris@0 185 ];
Chris@0 186
Chris@0 187 if (isset($form['promote'])) {
Chris@0 188 $form['promote']['#group'] = 'options';
Chris@0 189 }
Chris@0 190
Chris@0 191 if (isset($form['sticky'])) {
Chris@0 192 $form['sticky']['#group'] = 'options';
Chris@0 193 }
Chris@0 194
Chris@0 195 $form['#attached']['library'][] = 'node/form';
Chris@0 196
Chris@0 197 return $form;
Chris@0 198 }
Chris@0 199
Chris@0 200 /**
Chris@0 201 * Entity builder updating the node status with the submitted value.
Chris@0 202 *
Chris@0 203 * @param string $entity_type_id
Chris@0 204 * The entity type identifier.
Chris@0 205 * @param \Drupal\node\NodeInterface $node
Chris@0 206 * The node updated with the submitted values.
Chris@0 207 * @param array $form
Chris@0 208 * The complete form array.
Chris@0 209 * @param \Drupal\Core\Form\FormStateInterface $form_state
Chris@0 210 * The current state of the form.
Chris@0 211 *
Chris@0 212 * @see \Drupal\node\NodeForm::form()
Chris@0 213 *
Chris@0 214 * @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0.
Chris@0 215 * The "Publish" button was removed.
Chris@0 216 */
Chris@0 217 public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
Chris@0 218 $element = $form_state->getTriggeringElement();
Chris@0 219 if (isset($element['#published_status'])) {
Chris@4 220 $element['#published_status'] ? $node->setPublished() : $node->setUnpublished();
Chris@0 221 }
Chris@0 222 }
Chris@0 223
Chris@0 224 /**
Chris@0 225 * {@inheritdoc}
Chris@0 226 */
Chris@0 227 protected function actions(array $form, FormStateInterface $form_state) {
Chris@0 228 $element = parent::actions($form, $form_state);
Chris@0 229 $node = $this->entity;
Chris@0 230 $preview_mode = $node->type->entity->getPreviewMode();
Chris@0 231
Chris@0 232 $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
Chris@0 233
Chris@0 234 $element['preview'] = [
Chris@0 235 '#type' => 'submit',
Chris@0 236 '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
Chris@0 237 '#value' => t('Preview'),
Chris@0 238 '#weight' => 20,
Chris@0 239 '#submit' => ['::submitForm', '::preview'],
Chris@0 240 ];
Chris@0 241
Chris@0 242 $element['delete']['#access'] = $node->access('delete');
Chris@0 243 $element['delete']['#weight'] = 100;
Chris@0 244
Chris@0 245 return $element;
Chris@0 246 }
Chris@0 247
Chris@0 248 /**
Chris@0 249 * Form submission handler for the 'preview' action.
Chris@0 250 *
Chris@0 251 * @param $form
Chris@0 252 * An associative array containing the structure of the form.
Chris@0 253 * @param $form_state
Chris@0 254 * The current state of the form.
Chris@0 255 */
Chris@0 256 public function preview(array $form, FormStateInterface $form_state) {
Chris@0 257 $store = $this->tempStoreFactory->get('node_preview');
Chris@0 258 $this->entity->in_preview = TRUE;
Chris@0 259 $store->set($this->entity->uuid(), $form_state);
Chris@0 260
Chris@0 261 $route_parameters = [
Chris@0 262 'node_preview' => $this->entity->uuid(),
Chris@0 263 'view_mode_id' => 'full',
Chris@0 264 ];
Chris@0 265
Chris@0 266 $options = [];
Chris@0 267 $query = $this->getRequest()->query;
Chris@0 268 if ($query->has('destination')) {
Chris@0 269 $options['query']['destination'] = $query->get('destination');
Chris@0 270 $query->remove('destination');
Chris@0 271 }
Chris@0 272 $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
Chris@0 273 }
Chris@0 274
Chris@0 275 /**
Chris@0 276 * {@inheritdoc}
Chris@0 277 */
Chris@0 278 public function save(array $form, FormStateInterface $form_state) {
Chris@0 279 $node = $this->entity;
Chris@0 280 $insert = $node->isNew();
Chris@0 281 $node->save();
Chris@0 282 $node_link = $node->link($this->t('View'));
Chris@0 283 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
Chris@0 284 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())];
Chris@0 285
Chris@0 286 if ($insert) {
Chris@0 287 $this->logger('content')->notice('@type: added %title.', $context);
Chris@4 288 $this->messenger()->addStatus($this->t('@type %title has been created.', $t_args));
Chris@0 289 }
Chris@0 290 else {
Chris@0 291 $this->logger('content')->notice('@type: updated %title.', $context);
Chris@4 292 $this->messenger()->addStatus($this->t('@type %title has been updated.', $t_args));
Chris@0 293 }
Chris@0 294
Chris@0 295 if ($node->id()) {
Chris@0 296 $form_state->setValue('nid', $node->id());
Chris@0 297 $form_state->set('nid', $node->id());
Chris@0 298 if ($node->access('view')) {
Chris@0 299 $form_state->setRedirect(
Chris@0 300 'entity.node.canonical',
Chris@0 301 ['node' => $node->id()]
Chris@0 302 );
Chris@0 303 }
Chris@0 304 else {
Chris@0 305 $form_state->setRedirect('<front>');
Chris@0 306 }
Chris@0 307
Chris@0 308 // Remove the preview entry from the temp store, if any.
Chris@0 309 $store = $this->tempStoreFactory->get('node_preview');
Chris@0 310 $store->delete($node->uuid());
Chris@0 311 }
Chris@0 312 else {
Chris@0 313 // In the unlikely case something went wrong on save, the node will be
Chris@0 314 // rebuilt and node form redisplayed the same way as in preview.
Chris@4 315 $this->messenger()->addError($this->t('The post could not be saved.'));
Chris@0 316 $form_state->setRebuild();
Chris@0 317 }
Chris@0 318 }
Chris@0 319
Chris@0 320 }