Newer
Older
<?php
namespace Drupal\node;
Alex Pott
committed
use Drupal\Component\Datetime\TimeInterface;
Alex Pott
committed
use Drupal\Core\Entity\ContentEntityForm;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
Alex Pott
committed
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
Angie Byron
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
* Form handler for the node edit forms.
*
* @internal
Alex Pott
committed
class NodeForm extends ContentEntityForm {
Angie Byron
committed
/**
* The tempstore factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
Angie Byron
committed
*/
protected $tempStoreFactory;
/**
* The Current User object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
Angie Byron
committed
/**
* Constructs a NodeForm object.
Angie Byron
committed
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
Angie Byron
committed
* The factory for the temp store object.
Alex Pott
committed
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
Angie Byron
committed
*/
public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user) {
Alex Pott
committed
parent::__construct($entity_manager, $entity_type_bundle_info, $time);
Angie Byron
committed
$this->tempStoreFactory = $temp_store_factory;
$this->currentUser = $current_user;
Angie Byron
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('tempstore.private'),
Alex Pott
committed
$container->get('entity_type.bundle.info'),
$container->get('datetime.time'),
$container->get('current_user')
Angie Byron
committed
);
}
Dries Buytaert
committed
* {@inheritdoc}
Dries Buytaert
committed
public function form(array $form, FormStateInterface $form_state) {
// Try to restore from temp store, this must be done before calling
// parent::form().
Angie Byron
committed
$store = $this->tempStoreFactory->get('node_preview');
// Attempt to load from preview when the uuid is present unless we are
// rebuilding the form.
$request_uuid = \Drupal::request()->query->get('uuid');
if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
Angie Byron
committed
/** @var $preview \Drupal\Core\Form\FormStateInterface */
catch
committed
$form_state->setStorage($preview->getStorage());
$form_state->setUserInput($preview->getUserInput());
Angie Byron
committed
// Rebuild the form.
Angie Byron
committed
$form_state->setRebuild();
// The combination of having user input and rebuilding the form means
// that it will attempt to cache the form state which will fail if it is
// a GET request.
$form_state->setRequestMethod('POST');
$this->entity = $preview->getFormObject()->getEntity();
Angie Byron
committed
$this->entity->in_preview = NULL;
Angie Byron
committed
Alex Pott
committed
$form_state->set('has_been_previewed', TRUE);
Angie Byron
committed
}
/** @var \Drupal\node\NodeInterface $node */
$node = $this->entity;
Dries Buytaert
committed
Alex Pott
committed
if ($this->operation == 'edit') {
$form['#title'] = $this->t('<em>Edit @type</em> @title', [
'@type' => node_get_type_label($node),
'@title' => $node->label()
]);
Alex Pott
committed
}
// Changed must be sent to the client, for later overwrite error checking.
$form['changed'] = [
'#type' => 'hidden',
'#default_value' => $node->getChangedTime(),
];
$form = parent::form($form, $form_state);
$form['advanced']['#attributes']['class'][] = 'entity-meta';
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
$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() ? format_date($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()->getUsername(),
'#wrapper_attributes' => ['class' => ['entity-meta__author']],
];
$form['status']['#group'] = 'footer';
// Node author information for administrators.
$form['author'] = [
'#type' => 'details',
'#title' => t('Authoring information'),
Dries Buytaert
committed
'#group' => 'advanced',
'#attributes' => [
'class' => ['node-form-author'],
],
'#attached' => [
'library' => ['node/drupal.node'],
],
'#weight' => 90,
Angie Byron
committed
'#optional' => TRUE,
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
// Node options for administrators.
$form['options'] = [
'#type' => 'details',
Dries Buytaert
committed
'#title' => t('Promotion options'),
Dries Buytaert
committed
'#group' => 'advanced',
'#attributes' => [
'class' => ['node-form-options'],
],
'#attached' => [
'library' => ['node/drupal.node'],
],
'#weight' => 95,
Angie Byron
committed
'#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;
catch
committed
/**
* Entity builder updating the node status with the submitted value.
*
* @param string $entity_type_id
* The entity type identifier.
* @param \Drupal\node\NodeInterface $node
* The node updated with the submitted values.
* @param array $form
* The complete form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @see \Drupal\node\NodeForm::form()
*
* @deprecated in Drupal 8.4.x, will be removed before Drupal 9.0.0.
* The "Publish" button was removed.
catch
committed
*/
public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
catch
committed
$element = $form_state->getTriggeringElement();
if (isset($element['#published_status'])) {
$node->setPublished($element['#published_status']);
}
}
Dries Buytaert
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Dries Buytaert
committed
protected function actions(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$element = parent::actions($form, $form_state);
$node = $this->entity;
$preview_mode = $node->type->entity->getPreviewMode();
Dries Buytaert
committed
Alex Pott
committed
$element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
Dries Buytaert
committed
$element['preview'] = [
'#type' => 'submit',
'#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
'#value' => t('Preview'),
Angie Byron
committed
'#weight' => 20,
'#submit' => ['::submitForm', '::preview'],
];
$element['delete']['#access'] = $node->access('delete');
Angie Byron
committed
$element['delete']['#weight'] = 100;
return $element;
}
/**
* Form submission handler for the 'preview' action.
*
* @param $form
* An associative array containing the structure of the form.
* @param $form_state
Dries Buytaert
committed
* The current state of the form.
Dries Buytaert
committed
public function preview(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$store = $this->tempStoreFactory->get('node_preview');
$this->entity->in_preview = TRUE;
$store->set($this->entity->uuid(), $form_state);
Alex Pott
committed
$route_parameters = [
Angie Byron
committed
'node_preview' => $this->entity->uuid(),
Alex Pott
committed
'view_mode_id' => 'full',
Alex Pott
committed
];
$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);
}
/**
Dries Buytaert
committed
* {@inheritdoc}
Dries Buytaert
committed
public function save(array $form, FormStateInterface $form_state) {
$node = $this->entity;
$insert = $node->isNew();
$node->save();
$node_link = $node->link($this->t('View'));
$context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
$t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())];
if ($insert) {
$this->logger('content')->notice('@type: added %title.', $context);
drupal_set_message(t('@type %title has been created.', $t_args));
}
else {
$this->logger('content')->notice('@type: updated %title.', $context);
drupal_set_message(t('@type %title has been updated.', $t_args));
}
if ($node->id()) {
Alex Pott
committed
$form_state->setValue('nid', $node->id());
Angie Byron
committed
$form_state->set('nid', $node->id());
Angie Byron
committed
if ($node->access('view')) {
Alex Pott
committed
$form_state->setRedirect(
Alex Pott
committed
'entity.node.canonical',
['node' => $node->id()]
Angie Byron
committed
);
}
else {
Alex Pott
committed
$form_state->setRedirect('<front>');
Angie Byron
committed
}
// Remove the preview entry from the temp store, if any.
$store = $this->tempStoreFactory->get('node_preview');
Angie Byron
committed
$store->delete($node->uuid());
}
else {
// In the unlikely case something went wrong on save, the node will be
// rebuilt and node form redisplayed the same way as in preview.
drupal_set_message(t('The post could not be saved.'), 'error');
Angie Byron
committed
$form_state->setRebuild();