summaryrefslogtreecommitdiffstats
path: root/core/modules/node/src/NodeForm.php
diff options
context:
space:
mode:
authorNathaniel Catchpole2015-07-03 13:40:43 (GMT)
committerNathaniel Catchpole2015-07-03 13:40:43 (GMT)
commita47cadae5e94dbb0545a666d173e5bc9227a0f47 (patch)
tree59494960d22a093b07059eac230e269c5ceacf11 /core/modules/node/src/NodeForm.php
parent9907029cdb30ccc691832865658288af1cfdd25d (diff)
Issue #2498919 by stefan.r, Berdir, catch: Node::isPublished() and Node::getOwnerId() are expensive
Diffstat (limited to 'core/modules/node/src/NodeForm.php')
-rw-r--r--core/modules/node/src/NodeForm.php58
1 files changed, 27 insertions, 31 deletions
diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php
index 33dec70..c584660 100644
--- a/core/modules/node/src/NodeForm.php
+++ b/core/modules/node/src/NodeForm.php
@@ -7,7 +7,6 @@
namespace Drupal\node;
-use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -204,10 +203,33 @@ class NodeForm extends ContentEntityForm {
$form['#attached']['library'][] = 'node/form';
+ $form['#entity_builders']['update_status'] = [$this, 'updateStatus'];
+
return $form;
}
/**
+ * 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()
+ */
+ function updateStatus($entity_type_id, NodeInterface $node, array $form, FormStateInterface $form_state) {
+ $element = $form_state->getTriggeringElement();
+ if (isset($element['#published_status'])) {
+ $node->setPublished($element['#published_status']);
+ }
+ }
+
+ /**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
@@ -232,6 +254,8 @@ class NodeForm extends ContentEntityForm {
// Add a "Publish" button.
$element['publish'] = $element['submit'];
+ // If the "Publish" button is clicked, we want to update the status to "published".
+ $element['publish']['#published_status'] = TRUE;
$element['publish']['#dropbutton'] = 'save';
if ($node->isNew()) {
$element['publish']['#value'] = t('Save and publish');
@@ -240,10 +264,11 @@ class NodeForm extends ContentEntityForm {
$element['publish']['#value'] = $node->isPublished() ? t('Save and keep published') : t('Save and publish');
}
$element['publish']['#weight'] = 0;
- array_unshift($element['publish']['#submit'], '::publish');
// Add a "Unpublish" button.
$element['unpublish'] = $element['submit'];
+ // If the "Unpublish" button is clicked, we want to update the status to "unpublished".
+ $element['unpublish']['#published_status'] = FALSE;
$element['unpublish']['#dropbutton'] = 'save';
if ($node->isNew()) {
$element['unpublish']['#value'] = t('Save as unpublished');
@@ -252,7 +277,6 @@ class NodeForm extends ContentEntityForm {
$element['unpublish']['#value'] = !$node->isPublished() ? t('Save and keep unpublished') : t('Save and unpublish');
}
$element['unpublish']['#weight'] = 10;
- array_unshift($element['unpublish']['#submit'], '::unpublish');
// If already published, the 'publish' button is primary.
if ($node->isPublished()) {
@@ -328,34 +352,6 @@ class NodeForm extends ContentEntityForm {
}
/**
- * Form submission handler for the 'publish' action.
- *
- * @param $form
- * An associative array containing the structure of the form.
- * @param $form_state
- * The current state of the form.
- */
- public function publish(array $form, FormStateInterface $form_state) {
- $node = $this->entity;
- $node->setPublished(TRUE);
- return $node;
- }
-
- /**
- * Form submission handler for the 'unpublish' action.
- *
- * @param $form
- * An associative array containing the structure of the form.
- * @param $form_state
- * The current state of the form.
- */
- public function unpublish(array $form, FormStateInterface $form_state) {
- $node = $this->entity;
- $node->setPublished(FALSE);
- return $node;
- }
-
- /**
* {@inheritdoc}
*/
public function buildEntity(array $form, FormStateInterface $form_state) {