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

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