View source
<?php
namespace Drupal\node;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class NodeForm extends ContentEntityForm {
protected $tempStoreFactory;
protected $currentUser;
protected $dateFormatter;
public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->tempStoreFactory = $temp_store_factory;
$this->currentUser = $current_user;
$this->dateFormatter = $date_formatter;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('tempstore.private'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('current_user'), $container
->get('date.formatter'));
}
public function form(array $form, FormStateInterface $form_state) {
$store = $this->tempStoreFactory
->get('node_preview');
$request_uuid = \Drupal::request()->query
->get('uuid');
if (!$form_state
->isRebuilding() && $request_uuid && ($preview = $store
->get($request_uuid))) {
$form_state
->setStorage($preview
->getStorage());
$form_state
->setUserInput($preview
->getUserInput());
$form_state
->setRebuild();
$form_state
->setRequestMethod('POST');
$this->entity = $preview
->getFormObject()
->getEntity();
$this->entity->in_preview = NULL;
$form_state
->set('has_been_previewed', TRUE);
}
$node = $this->entity;
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('<em>Edit @type</em> @title', [
'@type' => node_get_type_label($node),
'@title' => $node
->label(),
]);
}
$form['changed'] = [
'#type' => 'hidden',
'#default_value' => $node
->getChangedTime(),
];
$form = parent::form($form, $form_state);
$form['advanced']['#attributes']['class'][] = 'entity-meta';
$form['meta'] = [
'#type' => 'details',
'#group' => 'advanced',
'#weight' => -10,
'#title' => $this
->t('Status'),
'#attributes' => [
'class' => [
'entity-meta__header',
],
],
'#tree' => TRUE,
'#access' => $this->currentUser
->hasPermission('administer nodes'),
];
$form['meta']['published'] = [
'#type' => 'item',
'#markup' => $node
->isPublished() ? $this
->t('Published') : $this
->t('Not published'),
'#access' => !$node
->isNew(),
'#wrapper_attributes' => [
'class' => [
'entity-meta__title',
],
],
];
$form['meta']['changed'] = [
'#type' => 'item',
'#title' => $this
->t('Last saved'),
'#markup' => !$node
->isNew() ? $this->dateFormatter
->format($node
->getChangedTime(), 'short') : $this
->t('Not saved yet'),
'#wrapper_attributes' => [
'class' => [
'entity-meta__last-saved',
],
],
];
$form['meta']['author'] = [
'#type' => 'item',
'#title' => $this
->t('Author'),
'#markup' => $node
->getOwner()
->getAccountName(),
'#wrapper_attributes' => [
'class' => [
'entity-meta__author',
],
],
];
$form['status']['#group'] = 'footer';
$form['author'] = [
'#type' => 'details',
'#title' => $this
->t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'node-form-author',
],
],
'#attached' => [
'library' => [
'node/drupal.node',
],
],
'#weight' => 90,
'#optional' => TRUE,
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
$form['options'] = [
'#type' => 'details',
'#title' => $this
->t('Promotion options'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'node-form-options',
],
],
'#attached' => [
'library' => [
'node/drupal.node',
],
],
'#weight' => 95,
'#optional' => TRUE,
];
if (isset($form['promote'])) {
$form['promote']['#group'] = 'options';
}
if (isset($form['sticky'])) {
$form['sticky']['#group'] = 'options';
}
$form['#attached']['library'][] = 'node/form';
return $form;
}
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$node = $this->entity;
$preview_mode = $node->type->entity
->getPreviewMode();
$element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state
->get('has_been_previewed');
$element['preview'] = [
'#type' => 'submit',
'#access' => $preview_mode != DRUPAL_DISABLED && ($node
->access('create') || $node
->access('update')),
'#value' => $this
->t('Preview'),
'#weight' => 20,
'#submit' => [
'::submitForm',
'::preview',
],
];
if (array_key_exists('delete', $element)) {
$element['delete']['#weight'] = 100;
}
return $element;
}
public function preview(array $form, FormStateInterface $form_state) {
$store = $this->tempStoreFactory
->get('node_preview');
$this->entity->in_preview = TRUE;
$store
->set($this->entity
->uuid(), $form_state);
$route_parameters = [
'node_preview' => $this->entity
->uuid(),
'view_mode_id' => 'full',
];
$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);
}
public function save(array $form, FormStateInterface $form_state) {
$node = $this->entity;
$insert = $node
->isNew();
$node
->save();
$node_link = $node
->toLink($this
->t('View'))
->toString();
$context = [
'@type' => $node
->getType(),
'%title' => $node
->label(),
'link' => $node_link,
];
$t_args = [
'@type' => node_get_type_label($node),
'%title' => $node
->toLink()
->toString(),
];
if ($insert) {
$this
->logger('content')
->info('@type: added %title.', $context);
$this
->messenger()
->addStatus($this
->t('@type %title has been created.', $t_args));
}
else {
$this
->logger('content')
->info('@type: updated %title.', $context);
$this
->messenger()
->addStatus($this
->t('@type %title has been updated.', $t_args));
}
if ($node
->id()) {
$form_state
->setValue('nid', $node
->id());
$form_state
->set('nid', $node
->id());
if ($node
->access('view')) {
$form_state
->setRedirect('entity.node.canonical', [
'node' => $node
->id(),
]);
}
else {
$form_state
->setRedirect('<front>');
}
$store = $this->tempStoreFactory
->get('node_preview');
$store
->delete($node
->uuid());
}
else {
$this
->messenger()
->addError($this
->t('The post could not be saved.'));
$form_state
->setRebuild();
}
}
}