5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\ContentEntityForm;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\user\PrivateTempStoreFactory;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Form handler for the node edit forms.
16 class NodeForm extends ContentEntityForm {
19 * The tempstore factory.
21 * @var \Drupal\user\PrivateTempStoreFactory
23 protected $tempStoreFactory;
26 * Constructs a NodeForm object.
28 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
30 * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
31 * The factory for the temp store object.
32 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
33 * The entity type bundle service.
34 * @param \Drupal\Component\Datetime\TimeInterface $time
37 public function __construct(EntityManagerInterface $entity_manager, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL) {
38 parent::__construct($entity_manager, $entity_type_bundle_info, $time);
39 $this->tempStoreFactory = $temp_store_factory;
45 public static function create(ContainerInterface $container) {
47 $container->get('entity.manager'),
48 $container->get('user.private_tempstore'),
49 $container->get('entity_type.bundle.info'),
50 $container->get('datetime.time')
57 public function form(array $form, FormStateInterface $form_state) {
58 // Try to restore from temp store, this must be done before calling
60 $store = $this->tempStoreFactory->get('node_preview');
62 // Attempt to load from preview when the uuid is present unless we are
63 // rebuilding the form.
64 $request_uuid = \Drupal::request()->query->get('uuid');
65 if (!$form_state->isRebuilding() && $request_uuid && $preview = $store->get($request_uuid)) {
66 /** @var $preview \Drupal\Core\Form\FormStateInterface */
68 $form_state->setStorage($preview->getStorage());
69 $form_state->setUserInput($preview->getUserInput());
72 $form_state->setRebuild();
74 // The combination of having user input and rebuilding the form means
75 // that it will attempt to cache the form state which will fail if it is
77 $form_state->setRequestMethod('POST');
79 $this->entity = $preview->getFormObject()->getEntity();
80 $this->entity->in_preview = NULL;
82 $form_state->set('has_been_previewed', TRUE);
85 /** @var \Drupal\node\NodeInterface $node */
86 $node = $this->entity;
88 if ($this->operation == 'edit') {
89 $form['#title'] = $this->t('<em>Edit @type</em> @title', ['@type' => node_get_type_label($node), '@title' => $node->label()]);
92 // Changed must be sent to the client, for later overwrite error checking.
95 '#default_value' => $node->getChangedTime(),
98 $form = parent::form($form, $form_state);
100 $form['advanced']['#attributes']['class'][] = 'entity-meta';
102 // Node author information for administrators.
104 '#type' => 'details',
105 '#title' => t('Authoring information'),
106 '#group' => 'advanced',
108 'class' => ['node-form-author'],
111 'library' => ['node/drupal.node'],
117 if (isset($form['uid'])) {
118 $form['uid']['#group'] = 'author';
121 if (isset($form['created'])) {
122 $form['created']['#group'] = 'author';
125 // Node options for administrators.
127 '#type' => 'details',
128 '#title' => t('Promotion options'),
129 '#group' => 'advanced',
131 'class' => ['node-form-options'],
134 'library' => ['node/drupal.node'],
140 if (isset($form['promote'])) {
141 $form['promote']['#group'] = 'options';
144 if (isset($form['sticky'])) {
145 $form['sticky']['#group'] = 'options';
148 $form['#attached']['library'][] = 'node/form';
150 $form['#entity_builders']['update_status'] = '::updateStatus';
156 * Entity builder updating the node status with the submitted value.
158 * @param string $entity_type_id
159 * The entity type identifier.
160 * @param \Drupal\node\NodeInterface $node
161 * The node updated with the submitted values.
163 * The complete form array.
164 * @param \Drupal\Core\Form\FormStateInterface $form_state
165 * The current state of the form.
167 * @see \Drupal\node\NodeForm::form()
169 public function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
170 $element = $form_state->getTriggeringElement();
171 if (isset($element['#published_status'])) {
172 $node->setPublished($element['#published_status']);
179 protected function actions(array $form, FormStateInterface $form_state) {
180 $element = parent::actions($form, $form_state);
181 $node = $this->entity;
182 $preview_mode = $node->type->entity->getPreviewMode();
184 $element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state->get('has_been_previewed');
186 // If saving is an option, privileged users get dedicated form submit
187 // buttons to adjust the publishing status while saving in one go.
188 // @todo This adjustment makes it close to impossible for contributed
189 // modules to integrate with "the Save operation" of this form. Modules
190 // need a way to plug themselves into 1) the ::submit() step, and
191 // 2) the ::save() step, both decoupled from the pressed form button.
192 if ($element['submit']['#access'] && \Drupal::currentUser()->hasPermission('administer nodes')) {
193 // isNew | prev status » default & publish label & unpublish label
194 // 1 | 1 » publish & Save and publish & Save as unpublished
195 // 1 | 0 » unpublish & Save and publish & Save as unpublished
196 // 0 | 1 » publish & Save and keep published & Save and unpublish
197 // 0 | 0 » unpublish & Save and keep unpublished & Save and publish
199 // Add a "Publish" button.
200 $element['publish'] = $element['submit'];
201 // If the "Publish" button is clicked, we want to update the status to "published".
202 $element['publish']['#published_status'] = TRUE;
203 $element['publish']['#dropbutton'] = 'save';
204 if ($node->isNew()) {
205 $element['publish']['#value'] = t('Save and publish');
208 $element['publish']['#value'] = $node->isPublished() ? t('Save and keep published') : t('Save and publish');
210 $element['publish']['#weight'] = 0;
212 // Add a "Unpublish" button.
213 $element['unpublish'] = $element['submit'];
214 // If the "Unpublish" button is clicked, we want to update the status to "unpublished".
215 $element['unpublish']['#published_status'] = FALSE;
216 $element['unpublish']['#dropbutton'] = 'save';
217 if ($node->isNew()) {
218 $element['unpublish']['#value'] = t('Save as unpublished');
221 $element['unpublish']['#value'] = !$node->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
223 $element['unpublish']['#weight'] = 10;
225 // If already published, the 'publish' button is primary.
226 if ($node->isPublished()) {
227 unset($element['unpublish']['#button_type']);
229 // Otherwise, the 'unpublish' button is primary and should come first.
231 unset($element['publish']['#button_type']);
232 $element['unpublish']['#weight'] = -10;
235 // Remove the "Save" button.
236 $element['submit']['#access'] = FALSE;
239 $element['preview'] = [
241 '#access' => $preview_mode != DRUPAL_DISABLED && ($node->access('create') || $node->access('update')),
242 '#value' => t('Preview'),
244 '#submit' => ['::submitForm', '::preview'],
247 $element['delete']['#access'] = $node->access('delete');
248 $element['delete']['#weight'] = 100;
254 * Form submission handler for the 'preview' action.
257 * An associative array containing the structure of the form.
259 * The current state of the form.
261 public function preview(array $form, FormStateInterface $form_state) {
262 $store = $this->tempStoreFactory->get('node_preview');
263 $this->entity->in_preview = TRUE;
264 $store->set($this->entity->uuid(), $form_state);
266 $route_parameters = [
267 'node_preview' => $this->entity->uuid(),
268 'view_mode_id' => 'full',
272 $query = $this->getRequest()->query;
273 if ($query->has('destination')) {
274 $options['query']['destination'] = $query->get('destination');
275 $query->remove('destination');
277 $form_state->setRedirect('entity.node.preview', $route_parameters, $options);
283 public function save(array $form, FormStateInterface $form_state) {
284 $node = $this->entity;
285 $insert = $node->isNew();
287 $node_link = $node->link($this->t('View'));
288 $context = ['@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link];
289 $t_args = ['@type' => node_get_type_label($node), '%title' => $node->link($node->label())];
292 $this->logger('content')->notice('@type: added %title.', $context);
293 drupal_set_message(t('@type %title has been created.', $t_args));
296 $this->logger('content')->notice('@type: updated %title.', $context);
297 drupal_set_message(t('@type %title has been updated.', $t_args));
301 $form_state->setValue('nid', $node->id());
302 $form_state->set('nid', $node->id());
303 if ($node->access('view')) {
304 $form_state->setRedirect(
305 'entity.node.canonical',
306 ['node' => $node->id()]
310 $form_state->setRedirect('<front>');
313 // Remove the preview entry from the temp store, if any.
314 $store = $this->tempStoreFactory->get('node_preview');
315 $store->delete($node->uuid());
318 // In the unlikely case something went wrong on save, the node will be
319 // rebuilt and node form redisplayed the same way as in preview.
320 drupal_set_message(t('The post could not be saved.'), 'error');
321 $form_state->setRebuild();