NodeForm.php 10 KB

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