commit 6d9c746eb9055d5ed239b92e7e2b563bae26e70b Author: Francesco Placella Date: Fri Jul 3 17:33:31 2015 +0100 Prevented entity validation from being skipped. diff --git a/core/lib/Drupal/Core/Entity/ContentEntityForm.php b/core/lib/Drupal/Core/Entity/ContentEntityForm.php index c4df8b9..4903130 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityForm.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityForm.php @@ -78,10 +78,26 @@ public function validateForm(array &$form, FormStateInterface $form_state) { ->filterByFields(array_diff(array_keys($entity->getFieldDefinitions()), $this->getEditedFieldNames($form_state))); $this->flagViolations($violations, $form, $form_state); + + // Mark the entity as validated. See ::save(). + $form_state->setTemporaryValue('entity_validated', TRUE); + return $entity; } /** + * {@inheritdoc} + */ + public function save(array $form, FormStateInterface $form_state) { + // Make sure button-level validation handlers do not prevent entity + // validation from being executed. + if (!$form_state->getTemporaryValue('entity_validated')) { + throw new \LogicException('Entity validation was skipped.'); + } + return parent::save($form, $form_state); + } + + /** * Gets the names of all fields edited in the form. * * If the entity form customly adds some fields to the form (i.e. without diff --git a/core/modules/aggregator/src/FeedForm.php b/core/modules/aggregator/src/FeedForm.php index aa5069c..a9ef844 100644 --- a/core/modules/aggregator/src/FeedForm.php +++ b/core/modules/aggregator/src/FeedForm.php @@ -22,7 +22,10 @@ class FeedForm extends ContentEntityForm { public function save(array $form, FormStateInterface $form_state) { $feed = $this->entity; $insert = (bool) $feed->id(); - $feed->save(); + + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); + if ($insert) { drupal_set_message($this->t('The feed %feed has been updated.', array('%feed' => $feed->label()))); $form_state->setRedirectUrl($feed->urlInfo('canonical')); diff --git a/core/modules/block_content/src/BlockContentForm.php b/core/modules/block_content/src/BlockContentForm.php index ec267cc..839ce9c 100644 --- a/core/modules/block_content/src/BlockContentForm.php +++ b/core/modules/block_content/src/BlockContentForm.php @@ -178,7 +178,10 @@ public function save(array $form, FormStateInterface $form_state) { } $insert = $block->isNew(); - $block->save(); + + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); + $context = array('@type' => $block->bundle(), '%info' => $block->label()); $logger = $this->logger('block_content'); $block_type = $this->blockContentTypeStorage->load($block->bundle()); diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index e786fe1..1244447 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -360,7 +360,9 @@ public function save(array $form, FormStateInterface $form_state) { $logger = $this->logger('content'); if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) { - $comment->save(); + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); + $form_state->setValue('cid', $comment->id()); // Add a log entry. diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 27352c7..899a346 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -225,10 +225,11 @@ public function save(array $form, FormStateInterface $form_state) { else { $form_state->setRedirect(''); } + // Save the message. In core this is a no-op but should contrib wish to // implement message storage, this will make the task of swapping in a real // storage controller straight-forward. - $message->save(); + parent::save($form, $form_state); } /** diff --git a/core/modules/forum/src/Form/ForumForm.php b/core/modules/forum/src/Form/ForumForm.php index 1a517c0..1f270cd 100644 --- a/core/modules/forum/src/Form/ForumForm.php +++ b/core/modules/forum/src/Form/ForumForm.php @@ -76,8 +76,14 @@ public function buildEntity(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { + // See ContentEntityForm::save(). + if (!$form_state->getTemporaryValue('entity_validated')) { + throw new \LogicException('Entity validation was skipped.'); + } + $term = $this->entity; $term_storage = $this->entityManager->getStorage('taxonomy_term'); + $status = $term_storage->save($term); $route_name = $this->urlStub == 'container' ? 'entity.taxonomy_term.forum_edit_container_form' : 'entity.taxonomy_term.forum_edit_form'; diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index 747c1fd..0398426 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Menu\MenuParentFormSelectorInterface; @@ -120,18 +121,18 @@ public function buildEntity(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - // The entity is rebuilt in parent::submit(). - $menu_link = $this->entity; - $saved = $menu_link->save(); + try { + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); - if ($saved) { + // The entity is rebuilt in parent::submit(). drupal_set_message($this->t('The menu link has been saved.')); $form_state->setRedirect( 'entity.menu_link_content.canonical', - array('menu_link_content' => $menu_link->id()) + array('menu_link_content' => $this->entity->id()) ); } - else { + catch (EntityStorageException $e) { drupal_set_message($this->t('There was an error saving the menu link.'), 'error'); $form_state->setRebuild(); } diff --git a/core/modules/node/src/NodeForm.php b/core/modules/node/src/NodeForm.php index e363dca..bcb4040 100644 --- a/core/modules/node/src/NodeForm.php +++ b/core/modules/node/src/NodeForm.php @@ -374,7 +374,10 @@ public function buildEntity(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) { $node = $this->entity; $insert = $node->isNew(); - $node->save(); + + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); + $node_link = $node->link($this->t('View')); $context = array('@type' => $node->getType(), '%title' => $node->label(), 'link' => $node_link); $t_args = array('@type' => node_get_type_label($node), '%title' => $node->label()); diff --git a/core/modules/shortcut/src/ShortcutForm.php b/core/modules/shortcut/src/ShortcutForm.php index c0f8714..0aa6e53 100644 --- a/core/modules/shortcut/src/ShortcutForm.php +++ b/core/modules/shortcut/src/ShortcutForm.php @@ -27,13 +27,16 @@ class ShortcutForm extends ContentEntityForm { */ public function save(array $form, FormStateInterface $form_state) { $entity = $this->entity; - $status = $entity->save(); + $insert = $entity->isNew(); - if ($status == SAVED_UPDATED) { - $message = $this->t('The shortcut %link has been updated.', array('%link' => $entity->getTitle())); + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); + + if ($insert) { + $message = $this->t('Added a shortcut for %title.', array('%title' => $entity->getTitle())); } else { - $message = $this->t('Added a shortcut for %title.', array('%title' => $entity->getTitle())); + $message = $this->t('The shortcut %link has been updated.', array('%link' => $entity->getTitle())); } drupal_set_message($message); diff --git a/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php b/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php index fb3c486..e679585 100644 --- a/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php +++ b/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php @@ -59,7 +59,9 @@ public function save(array $form, FormStateInterface $form_state) { } $is_new = $entity->isNew(); - $entity->save(); + + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); if ($is_new) { $message = t('%entity_type @id has been created.', array('@id' => $entity->id(), '%entity_type' => $entity->getEntityTypeId())); diff --git a/core/modules/taxonomy/src/TermForm.php b/core/modules/taxonomy/src/TermForm.php index 9da0993..42f9ee5 100644 --- a/core/modules/taxonomy/src/TermForm.php +++ b/core/modules/taxonomy/src/TermForm.php @@ -125,19 +125,19 @@ public function buildEntity(array $form, FormStateInterface $form_state) { */ public function save(array $form, FormStateInterface $form_state) { $term = $this->entity; + $insert = $term->isNew(); - $result = $term->save(); + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); $link = $term->link($this->t('Edit'), 'edit-form'); - switch ($result) { - case SAVED_NEW: - drupal_set_message($this->t('Created new term %term.', array('%term' => $term->getName()))); - $this->logger('taxonomy')->notice('Created new term %term.', array('%term' => $term->getName(), 'link' => $link)); - break; - case SAVED_UPDATED: - drupal_set_message($this->t('Updated term %term.', array('%term' => $term->getName()))); - $this->logger('taxonomy')->notice('Updated term %term.', array('%term' => $term->getName(), 'link' => $link)); - break; + if ($insert) { + drupal_set_message($this->t('Created new term %term.', array('%term' => $term->getName()))); + $this->logger('taxonomy')->notice('Created new term %term.', array('%term' => $term->getName(), 'link' => $link)); + } + else { + drupal_set_message($this->t('Updated term %term.', array('%term' => $term->getName()))); + $this->logger('taxonomy')->notice('Updated term %term.', array('%term' => $term->getName(), 'link' => $link)); } $current_parent_count = count($form_state->getValue('parent')); diff --git a/core/modules/user/src/ProfileForm.php b/core/modules/user/src/ProfileForm.php index e119693..5cfea9c 100644 --- a/core/modules/user/src/ProfileForm.php +++ b/core/modules/user/src/ProfileForm.php @@ -48,9 +48,9 @@ protected function actions(array $form, FormStateInterface $form_state) { */ public function save(array $form, FormStateInterface $form_state) { $account = $this->entity; - $account->save(); + // Make sure we do not skip the parent's entity save logic. + parent::save($form, $form_state); $form_state->setValue('uid', $account->id()); - drupal_set_message($this->t('The changes have been saved.')); } diff --git a/core/modules/user/src/RegisterForm.php b/core/modules/user/src/RegisterForm.php index 53dad45..94ad8a5 100644 --- a/core/modules/user/src/RegisterForm.php +++ b/core/modules/user/src/RegisterForm.php @@ -102,9 +102,10 @@ public function save(array $form, FormStateInterface $form_state) { $admin = $form_state->getValue('administer_users'); $notify = !$form_state->isValueEmpty('notify'); - // Save has no return value so this cannot be tested. - // Assume save has gone through correctly. - $account->save(); + // Save has no return value so this cannot be tested. Assume save has gone + // through correctly. Make sure we do not skip the parent's entity save + // logic. + parent::save($form, $form_state); $form_state->set('user', $account); $form_state->setValue('uid', $account->id());