Commit 9be3165c authored by alexpott's avatar alexpott

Issue #1728804 by Berdir: Introduce (Content)EntityDeleteForm and children to...

Issue #1728804 by Berdir: Introduce (Content)EntityDeleteForm and children to handle entity deletions
parent 9a5eb867
<?php
/**
* @file
* Contains \Drupal\Core\Entity\ContentEntityDeleteForm.
*/
namespace Drupal\Core\Entity;
/**
* Provides a generic base class for a content entity deletion form.
*/
class ContentEntityDeleteForm extends ContentEntityConfirmFormBase {
use EntityDeleteFormTrait;
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityConfirmFormBase.
*/
namespace Drupal\Core\Entity;
/**
* Provides a generic base class for an entity deletion form.
*
* @ingroup entity_api
*/
class EntityDeleteForm extends EntityConfirmFormBase {
use EntityDeleteFormTrait;
}
<?php
/**
* @file
* Contains \Drupal\Core\Entity\EntityDeleteFormTrait.
*/
namespace Drupal\Core\Entity;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a trait for an entity deletion form.
*
* This trait relies on the StringTranslationTrait and the logger method added
* by FormBase.
*
* @ingroup entity_api
*/
trait EntityDeleteFormTrait {
/**
* Translates a string to the current language or to a given language.
*
* Provided by \Drupal\Core\StringTranslation\StringTranslationTrait.
*/
abstract protected function t($string, array $args = array(), array $options = array());
/**
* Returns the entity of this form.
*
* Provided by \Drupal\Core\Entity\EntityForm.
*
* @return \Drupal\Core\Entity\EntityInterface
* The entity.
*/
abstract public function getEntity();
/**
* Gets the logger for a specific channel.
*
* Provided by \Drupal\Core\Form\FormBase.
*
* @param string $channel
* The name of the channel.
*
* @return \Psr\Log\LoggerInterface
* The logger for this channel.
*/
abstract protected function logger($channel);
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the @entity-type %label?', array(
'@entity-type' => $this->getEntity()->getEntityType()->getLowercaseLabel(),
'%label' => $this->getEntity()->label(),
));
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* Returns the message to display to the user after deleting the entity.
*
* @return string
* The translated string of the deletion message.
*/
protected function getDeletionMessage() {
$entity = $this->getEntity();
return $this->t('The @entity-type %label has been deleted.', array(
'@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
'%label' => $entity->label(),
));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
$entity = $this->getEntity();
if ($entity->hasLinkTemplate('collection')) {
// If available, return the collection URL.
return $entity->urlInfo('collection');
}
else {
// Otherwise fall back to the default link template.
return $entity->urlInfo();
}
}
/**
* Logs a message about the deleted entity.
*/
protected function logDeletionMessage() {
$entity = $this->getEntity();
$this->logger($entity->getEntityType()->getProvider())->notice('The @entity-type %label has been deleted.', array(
'@entity-type' => $entity->getEntityType()->getLowercaseLabel(),
'%label' => $entity->label(),
));
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->getEntity()->delete();
drupal_set_message($this->getDeletionMessage());
$form_state->setRedirectUrl($this->getCancelUrl());
$this->logDeletionMessage();
}
}
......@@ -7,28 +7,13 @@
namespace Drupal\action\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Url;
/**
* Builds a form to delete an action.
*/
class ActionDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the action %action?', array('%action' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
class ActionDeleteForm extends EntityDeleteForm {
/**
* {@inheritdoc}
......@@ -37,16 +22,4 @@ public function getCancelUrl() {
return new Url('entity.action.collection');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->logger('user')->notice('Deleted action %aid (%action)', array('%aid' => $this->entity->id(), '%action' => $this->entity->label()));
drupal_set_message($this->t('Action %action was deleted', array('%action' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
......@@ -81,7 +81,7 @@ function testActionConfiguration() {
$this->assertResponse(200);
// Make sure that the action was actually deleted.
$this->assertRaw(t('Action %action was deleted', array('%action' => $new_action_label)), 'Make sure that we get a delete confirmation message.');
$this->assertRaw(t('The action %action has been deleted.', array('%action' => $new_action_label)), 'Make sure that we get a delete confirmation message.');
$this->drupalGet('admin/config/system/actions');
$this->assertResponse(200);
$this->assertNoText($new_action_label, "Make sure the action label does not appear on the overview page after we've deleted the action.");
......
......@@ -7,21 +7,13 @@
namespace Drupal\aggregator\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Url;
/**
* Provides a form for deleting a feed.
*/
class FeedDeleteForm extends ContentEntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the feed %feed?', array('%feed' => $this->entity->label()));
}
class FeedDeleteForm extends ContentEntityDeleteForm {
/**
* {@inheritdoc}
......@@ -33,18 +25,10 @@ public function getCancelUrl() {
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$this->logger('aggregator')->notice('Feed %feed deleted.', array('%feed' => $this->entity->label()));
drupal_set_message($this->t('The feed %feed has been deleted.', array('%feed' => $this->entity->label())));
$form_state->setRedirect('aggregator.admin_overview');
protected function getDeletionMessage() {
return $this->t('The feed %label has been deleted.', array(
'%label' => $this->entity->label(),
));
}
}
......@@ -7,21 +7,13 @@
namespace Drupal\block\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Url;
/**
* Provides a deletion confirmation form for the block instance deletion form.
*/
class BlockDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the block %name?', array('%name' => $this->entity->label()));
}
class BlockDeleteForm extends EntityDeleteForm {
/**
* {@inheritdoc}
......@@ -30,20 +22,4 @@ public function getCancelUrl() {
return new Url('block.admin_display');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message($this->t('The block %name has been removed.', array('%name' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
......@@ -171,7 +171,7 @@ function testBlock() {
$this->clickLink(t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $block['settings[label]'])));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('The block %name has been removed.', array('%name' => $block['settings[label]'])));
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $block['settings[label]'])));
// Test deleting a block via "Configure block" link.
$block = $this->drupalPlaceBlock('system_powered_by_block');
......@@ -179,7 +179,7 @@ function testBlock() {
$this->clickLink(t('Delete'));
$this->assertRaw(t('Are you sure you want to delete the block %name?', array('%name' => $block->label())));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('The block %name has been removed.', array('%name' => $block->label())));
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $block->label())));
$this->assertUrl('admin');
$this->assertNoRaw($block->id());
}
......
......@@ -7,35 +7,13 @@
namespace Drupal\block_content\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Provides a confirmation form for deleting a custom block entity.
*/
class BlockContentDeleteForm extends ContentEntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %name?', array('%name' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('block.admin_display');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
class BlockContentDeleteForm extends ContentEntityDeleteForm {
/**
* {@inheritdoc}
......@@ -51,14 +29,4 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message($this->t('Custom block %label has been deleted.', array('%label' => $this->entity->label())));
$this->logger('block_content')->notice('Custom block %label has been deleted.', array('%label' => $this->entity->label()));
$form_state->setRedirectUrl($this->entity->urlInfo('collection'));
}
}
......@@ -7,7 +7,7 @@
namespace Drupal\block_content\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
......@@ -16,7 +16,7 @@
/**
* Provides a confirmation form for deleting a custom block type entity.
*/
class BlockContentTypeDeleteForm extends EntityConfirmFormBase {
class BlockContentTypeDeleteForm extends EntityDeleteForm {
/**
* The query factory to create entity queries.
......@@ -44,27 +44,6 @@ public static function create(ContainerInterface $container) {
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %label?', array('%label' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->entity->urlInfo('collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
......@@ -80,14 +59,4 @@ public function buildForm(array $form, FormStateInterface $form_state) {
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(t('Custom block type %label has been deleted.', array('%label' => $this->entity->label())));
$this->logger('block_content')->notice('Custom block type %label has been deleted.', array('%label' => $this->entity->label()));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
......@@ -227,7 +227,7 @@ public function testBlockDelete() {
$this->assertText(\Drupal::translation()->formatPlural(1, 'This will also remove 1 placed block instance.', 'This will also remove @count placed block instance.'));
$this->drupalPostForm(NULL, array(), 'Delete');
$this->assertRaw(t('Custom block %name has been deleted.', array('%name' => $edit['info[0][value]'])));
$this->assertRaw(t('The custom block %name has been deleted.', array('%name' => $edit['info[0][value]'])));
// Create another block and force the plugin cache to flush.
$edit2 = array();
......
......@@ -99,7 +99,7 @@ public function testListing() {
$delete_text = t('Delete');
$this->clickLink($delete_text);
$this->assertResponse(200);
$this->assertTitle(strip_tags(t('Are you sure you want to delete %label?', array('%label' => $new_label)) . ' | Drupal'));
$this->assertTitle(strip_tags(t('Are you sure you want to delete the custom block %label?', array('%label' => $new_label)) . ' | Drupal'));
$this->drupalPostForm(NULL, array(), $delete_text);
// Verify that the text of the label and machine name does not appear in
......
......@@ -126,7 +126,7 @@ public function testBlockContentTypeDeletion() {
// Attempt to delete the block type, which should now be allowed.
$this->drupalGet('admin/structure/block/block-content/manage/' . $type->id() . '/delete');
$this->assertRaw(
t('Are you sure you want to delete %type?', array('%type' => $type->id())),
t('Are you sure you want to delete the custom block type %type?', array('%type' => $type->id())),
'The block type is available for deletion.'
);
$this->assertText(t('This action cannot be undone.'), 'The custom block type deletion confirmation form is available.');
......
......@@ -61,7 +61,7 @@ public function testPageEdit() {
// Test deleting the block.
$this->drupalGet("block/" . $revised_block->id());
$this->clickLink(t('Delete'));
$this->assertText(format_string('Are you sure you want to delete !label?', array('!label' => $revised_block->label())));
$this->assertText(format_string('Are you sure you want to delete the custom block !label?', array('!label' => $revised_block->label())));
}
}
......@@ -8,7 +8,7 @@
namespace Drupal\comment\Form;
use Drupal\comment\CommentManagerInterface;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\EntityDeleteForm;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
......@@ -20,7 +20,7 @@
/**
* Provides a confirmation form for deleting a comment type entity.
*/
class CommentTypeDeleteForm extends EntityConfirmFormBase {
class CommentTypeDeleteForm extends EntityDeleteForm {
/**
* The query factory to create entity queries.
......@@ -88,27 +88,6 @@ public static function create(ContainerInterface $container) {
);
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete %label?', array('%label' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->entity->urlInfo('collection');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
}
/**
* {@inheritdoc}
*/
......@@ -138,14 +117,4 @@ public function buildForm(array $form, FormStateInterface $form_state) {
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
$form_state->setRedirectUrl($this->entity->urlInfo('collection'));
drupal_set_message($this->t('Comment type %label has been deleted.', array('%label' => $this->entity->label())));
$this->logger->notice('comment type %label has been deleted.', array('%label' => $this->entity->label()));
}
}
......@@ -7,20 +7,13 @@
namespace Drupal\comment\Form;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Entity\ContentEntityDeleteForm;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides the comment delete confirmation form.
*/
class DeleteForm extends ContentEntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete the comment %title?', array('%title' => $this->entity->subject->value));
}
class DeleteForm extends ContentEntityDeleteForm {
/**
* {@inheritdoc}
......@@ -40,20 +33,15 @@ public function getDescription() {
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete');
protected function getDeletionMessage() {
return $this->t('The comment and all its replies have been deleted.');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Delete the comment and its replies.
$this->entity->delete();
drupal_set_message($this->t('The comment and all its replies have been deleted.'));
public function logDeletionMessage() {
$this->logger('content')->notice('Deleted comment @cid and its replies.', array('@cid' => $this->entity->id()));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
......@@ -169,7 +169,7 @@ public function testCommentTypeDeletion() {
// Attempt to delete the comment type, which should now be allowed.
$this->drupalGet('admin/structure/comment/manage/' . $type->id() . '/delete');
$this->assertRaw(
t('Are you sure you want to delete %type?', array('%type' => $type->id())),
t('Are you sure you want to delete the comment type %type?', array('%type' => $type->id())),
'The comment type is available for deletion.'
);
$this->assertText(t('This action cannot be undone.'), 'The comment type deletion confirmation form is available.');
......@@ -186,7 +186,7 @@ public function testCommentTypeDeletion() {
// Delete the comment type.
$this->drupalPostForm('admin/structure/comment/manage/' . $type->id() . '/delete', array(), t('Delete'));
$this->assertNull(CommentType::load($type->id()), 'Comment type deleted.');
$this->assertRaw(t('Comment type %label has been deleted.', array('%label' => $type->label())));
$this->assertRaw(t('The comment type %label has been deleted.', array('%label' => $type->label())));
}
}
......@@ -214,7 +214,7 @@ function testListUI() {
$this->assertLinkByHref('admin/structure/config_test/manage/albatross/delete');
$this->clickLink('Delete', 1);
$this->assertResponse(200);
$this->assertTitle('Are you sure you want to delete Albatross | Drupal');
$this->assertTitle('Are you sure you want to delete the test configuration Albatross? | Drupal');
$this->drupalPostForm(NULL, array(), t('Delete'));
// Verify that the text of the label and machine name does not appear in
......@@ -225,7 +225,7 @@ function testListUI() {
// Delete the original entity using the operations link.
$this->clickLink('Delete');
$this->assertResponse(200);
$this->assertTitle('Are you sure you want to delete Default | Drupal');
$this->assertTitle('Are you sure you want to delete the test configuration Default? | Drupal');
$this->drupalPostForm(NULL, array(), t('Delete'));
// Verify that the text of the label and machine name does not appear in
......
......@@ -241,7 +241,7 @@ function testCRUDUI() {
$label3 = $this->randomMachineName();
$message_insert = format_string('%label configuration has been created.', array('%label' => $label1));
$message_update = format_string('%label configuration has been updated.', array('%label' => $label2));
$message_delete = format_string('%label configuration has been deleted.', array('%label' => $label2));
$message_delete = format_string('The test configuration %label has been deleted.', array('%label' => $label2));
// Create a configuration entity.
$edit = array(
......
......@@ -23,7 +23,7 @@
* "list_builder" = "Drupal\config_test\ConfigTestListBuilder",
* "form" = {
* "default" = "Drupal\config_test\ConfigTestForm",
* "delete" = "Drupal\config_test\Form\ConfigTestDeleteForm"
* "delete" = "Drupal\Core\Entity\EntityDeleteForm"
* },
* "access" = "Drupal\config_test\ConfigTestAccessControlHandler"
* },
......
<?php
/**
* @file
* Contains \Drupal\config_test\Form\ConfigTestDeleteForm.
*/
namespace Drupal\config_test\Form;
use Drupal\Component\Utility\String;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Delete confirmation form for config_test entities.
*/
class ConfigTestDeleteForm extends EntityConfirmFormBase {
/**
* {@inheritdoc}
*/
public function getQuestion() {
return t('Are you sure you want to delete %label', array('%label' => $this->entity->label()));
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return t('Delete');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return $this->entity->urlInfo('collection');
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->entity->delete();
drupal_set_message(String::format('%label configuration has been deleted.', array('%label' => $this->entity->label())));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}