Chris@0: tempStoreFactory = $temp_store_factory; Chris@0: $this->currentUser = $current_user; Chris@18: $this->dateFormatter = $date_formatter; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@17: $container->get('entity.repository'), Chris@14: $container->get('tempstore.private'), Chris@0: $container->get('entity_type.bundle.info'), Chris@0: $container->get('datetime.time'), Chris@18: $container->get('current_user'), Chris@18: $container->get('date.formatter') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function form(array $form, FormStateInterface $form_state) { Chris@0: // Try to restore from temp store, this must be done before calling Chris@0: // parent::form(). Chris@0: $store = $this->tempStoreFactory->get('node_preview'); Chris@0: Chris@0: // Attempt to load from preview when the uuid is present unless we are Chris@0: // rebuilding the form. Chris@0: $request_uuid = \Drupal::request()->query->get('uuid'); Chris@0: if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) { Chris@0: /** @var $preview \Drupal\Core\Form\FormStateInterface */ Chris@0: Chris@0: $form_state->setStorage($preview->getStorage()); Chris@0: $form_state->setUserInput($preview->getUserInput()); Chris@0: Chris@0: // Rebuild the form. Chris@0: $form_state->setRebuild(); Chris@0: Chris@0: // The combination of having user input and rebuilding the form means Chris@0: // that it will attempt to cache the form state which will fail if it is Chris@0: // a GET request. Chris@0: $form_state->setRequestMethod('POST'); Chris@0: Chris@0: $this->entity = $preview->getFormObject()->getEntity(); Chris@0: $this->entity->in_preview = NULL; Chris@0: Chris@0: $form_state->set('has_been_previewed', TRUE); Chris@0: } Chris@0: Chris@0: /** @var \Drupal\node\NodeInterface $node */ Chris@0: $node = $this->entity; Chris@0: Chris@0: if ($this->operation == 'edit') { Chris@0: $form['#title'] = $this->t('Edit @type @title', [ Chris@0: '@type' => node_get_type_label($node), Chris@17: '@title' => $node->label(), Chris@0: ]); Chris@0: } Chris@0: Chris@0: // Changed must be sent to the client, for later overwrite error checking. Chris@0: $form['changed'] = [ Chris@0: '#type' => 'hidden', Chris@0: '#default_value' => $node->getChangedTime(), Chris@0: ]; Chris@0: Chris@0: $form = parent::form($form, $form_state); Chris@0: Chris@0: $form['advanced']['#attributes']['class'][] = 'entity-meta'; Chris@0: Chris@0: $form['meta'] = [ Chris@0: '#type' => 'details', Chris@0: '#group' => 'advanced', Chris@0: '#weight' => -10, Chris@0: '#title' => $this->t('Status'), Chris@0: '#attributes' => ['class' => ['entity-meta__header']], Chris@0: '#tree' => TRUE, Chris@0: '#access' => $this->currentUser->hasPermission('administer nodes'), Chris@0: ]; Chris@0: $form['meta']['published'] = [ Chris@0: '#type' => 'item', Chris@0: '#markup' => $node->isPublished() ? $this->t('Published') : $this->t('Not published'), Chris@0: '#access' => !$node->isNew(), Chris@0: '#wrapper_attributes' => ['class' => ['entity-meta__title']], Chris@0: ]; Chris@0: $form['meta']['changed'] = [ Chris@0: '#type' => 'item', Chris@0: '#title' => $this->t('Last saved'), Chris@18: '#markup' => !$node->isNew() ? $this->dateFormatter->format($node->getChangedTime(), 'short') : $this->t('Not saved yet'), Chris@0: '#wrapper_attributes' => ['class' => ['entity-meta__last-saved']], Chris@0: ]; Chris@0: $form['meta']['author'] = [ Chris@0: '#type' => 'item', Chris@0: '#title' => $this->t('Author'), Chris@18: '#markup' => $node->getOwner()->getAccountName(), Chris@0: '#wrapper_attributes' => ['class' => ['entity-meta__author']], Chris@0: ]; Chris@0: Chris@0: $form['status']['#group'] = 'footer'; Chris@0: Chris@0: // Node author information for administrators. Chris@0: $form['author'] = [ Chris@0: '#type' => 'details', Chris@0: '#title' => t('Authoring information'), Chris@0: '#group' => 'advanced', Chris@0: '#attributes' => [ Chris@0: 'class' => ['node-form-author'], Chris@0: ], Chris@0: '#attached' => [ Chris@0: 'library' => ['node/drupal.node'], Chris@0: ], Chris@0: '#weight' => 90, Chris@0: '#optional' => TRUE, Chris@0: ]; Chris@0: Chris@0: if (isset($form['uid'])) { Chris@0: $form['uid']['#group'] = 'author'; Chris@0: } Chris@0: Chris@0: if (isset($form['created'])) { Chris@0: $form['created']['#group'] = 'author'; Chris@0: } Chris@0: Chris@0: // Node options for administrators. Chris@0: $form['options'] = [ Chris@0: '#type' => 'details', Chris@0: '#title' => t('Promotion options'), Chris@0: '#group' => 'advanced', Chris@0: '#attributes' => [ Chris@0: 'class' => ['node-form-options'], Chris@0: ], Chris@0: '#attached' => [ Chris@0: 'library' => ['node/drupal.node'], Chris@0: ], Chris@0: '#weight' => 95, Chris@0: '#optional' => TRUE, Chris@0: ]; Chris@0: Chris@0: if (isset($form['promote'])) { Chris@0: $form['promote']['#group'] = 'options'; Chris@0: } Chris@0: Chris@0: if (isset($form['sticky'])) { Chris@0: $form['sticky']['#group'] = 'options'; Chris@0: } Chris@0: Chris@0: $form['#attached']['library'][] = 'node/form'; Chris@0: Chris@0: return $form; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Entity builder updating the node status with the submitted value. Chris@0: * Chris@0: * @param string $entity_type_id Chris@0: * The entity type identifier. Chris@0: * @param \Drupal\node\NodeInterface $node Chris@0: * The node updated with the submitted values. Chris@0: * @param array $form Chris@0: * The complete form array. Chris@0: * @param \Drupal\Core\Form\FormStateInterface $form_state Chris@0: * The current state of the form. Chris@0: * Chris@0: * @see \Drupal\node\NodeForm::form() Chris@0: * Chris@0: * @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0. Chris@0: * The "Publish" button was removed. Chris@0: */ Chris@0: public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) { Chris@0: $element = $form_state->getTriggeringElement(); Chris@0: if (isset($element['#published_status'])) { Chris@17: $element['#published_status'] ? $node->setPublished() : $node->setUnpublished(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function actions(array $form, FormStateInterface $form_state) { Chris@0: $element = parent::actions($form, $form_state); Chris@0: $node = $this->entity; Chris@0: $preview_mode = $node->type->entity->getPreviewMode(); Chris@0: Chris@0: $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed'); Chris@0: Chris@0: $element['preview'] = [ Chris@0: '#type' => 'submit', Chris@0: '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')), Chris@0: '#value' => t('Preview'), Chris@0: '#weight' => 20, Chris@0: '#submit' => ['::submitForm', '::preview'], Chris@0: ]; Chris@0: Chris@0: $element['delete']['#access'] = $node->access('delete'); Chris@0: $element['delete']['#weight'] = 100; Chris@0: Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Form submission handler for the 'preview' action. Chris@0: * Chris@0: * @param $form Chris@0: * An associative array containing the structure of the form. Chris@0: * @param $form_state Chris@0: * The current state of the form. Chris@0: */ Chris@0: public function preview(array $form, FormStateInterface $form_state) { Chris@0: $store = $this->tempStoreFactory->get('node_preview'); Chris@0: $this->entity->in_preview = TRUE; Chris@0: $store->set($this->entity->uuid(), $form_state); Chris@0: Chris@0: $route_parameters = [ Chris@0: 'node_preview' => $this->entity->uuid(), Chris@0: 'view_mode_id' => 'full', Chris@0: ]; Chris@0: Chris@0: $options = []; Chris@0: $query = $this->getRequest()->query; Chris@0: if ($query->has('destination')) { Chris@0: $options['query']['destination'] = $query->get('destination'); Chris@0: $query->remove('destination'); Chris@0: } Chris@0: $form_state->setRedirect('entity.node.preview', $route_parameters, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save(array $form, FormStateInterface $form_state) { Chris@0: $node = $this->entity; Chris@0: $insert = $node->isNew(); Chris@0: $node->save(); Chris@18: $node_link = $node->toLink($this->t('View'))->toString(); Chris@0: $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link]; Chris@18: $t_args = ['@type' => node_get_type_label($node), '%title' => $node->toLink()->toString()]; Chris@0: Chris@0: if ($insert) { Chris@0: $this->logger('content')->notice('@type: added %title.', $context); Chris@17: $this->messenger()->addStatus($this->t('@type %title has been created.', $t_args)); Chris@0: } Chris@0: else { Chris@0: $this->logger('content')->notice('@type: updated %title.', $context); Chris@17: $this->messenger()->addStatus($this->t('@type %title has been updated.', $t_args)); Chris@0: } Chris@0: Chris@0: if ($node->id()) { Chris@0: $form_state->setValue('nid', $node->id()); Chris@0: $form_state->set('nid', $node->id()); Chris@0: if ($node->access('view')) { Chris@0: $form_state->setRedirect( Chris@0: 'entity.node.canonical', Chris@0: ['node' => $node->id()] Chris@0: ); Chris@0: } Chris@0: else { Chris@0: $form_state->setRedirect(''); Chris@0: } Chris@0: Chris@0: // Remove the preview entry from the temp store, if any. Chris@0: $store = $this->tempStoreFactory->get('node_preview'); Chris@0: $store->delete($node->uuid()); Chris@0: } Chris@0: else { Chris@0: // In the unlikely case something went wrong on save, the node will be Chris@0: // rebuilt and node form redisplayed the same way as in preview. Chris@17: $this->messenger()->addError($this->t('The post could not be saved.')); Chris@0: $form_state->setRebuild(); Chris@0: } Chris@0: } Chris@0: Chris@0: }