diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php index 5b6bfdf0c637a7cd83327266713109e08fea0806..968ac07e5a79add7d94504de6e526fe53dba11c7 100644 --- a/src/Entity/FillPdfForm.php +++ b/src/Entity/FillPdfForm.php @@ -3,6 +3,7 @@ namespace Drupal\fillpdf\Entity; use Drupal\Core\Entity\ContentEntityBase; +use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Url; @@ -153,6 +154,24 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface { return $fields; } + /** + * Acts on FillPdfForms before they are deleted and before hooks are invoked. + * + * Deletes the FillPdfForm's FillPdfFormFields. + * + * @param \Drupal\Core\Entity\EntityStorageInterface $storage + * The entity storage object. + * @param \Drupal\fillpdf\FillPdfFormInterface[] $entities + * An array of FillPdfForms. + */ + public static function preDelete(EntityStorageInterface $storage, array $entities) { + parent::preDelete($storage, $entities); + + foreach ($entities as $fillpdf_form) { + \Drupal::entityTypeManager()->getStorage('fillpdf_form_field')->delete($fillpdf_form->getFormFields()); + } + } + /** * {@inheritdoc} */ diff --git a/tests/src/Functional/FillPdfFormDeleteFormTest.php b/tests/src/Functional/FillPdfFormDeleteFormTest.php index 3ef4457a0371a91af5c066e3cb7c4374d5134656..c756059d0b6ffff8e027e38a799c254f87facbc1 100644 --- a/tests/src/Functional/FillPdfFormDeleteFormTest.php +++ b/tests/src/Functional/FillPdfFormDeleteFormTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\fillpdf\Functional; use Drupal\fillpdf\Entity\FillPdfForm; +use Drupal\fillpdf\Entity\FillPdfFormField; use Drupal\Tests\BrowserTestBase; use Drupal\Tests\fillpdf\Traits\TestFillPdfTrait; use Drupal\Core\Url; @@ -31,7 +32,7 @@ class FillPdfFormDeleteFormTest extends BrowserTestBase { /** * Tests the cancel link works. */ - public function testCancelDeletion() { + public function testDeleteFormCancel() { $this->uploadTestPdf('fillpdf_test_v3.pdf'); $fillpdf_form = FillPdfForm::load($this->getLatestFillPdfForm()); @@ -69,4 +70,28 @@ class FillPdfFormDeleteFormTest extends BrowserTestBase { $this->assertSession()->addressEquals($fillpdf_form->toUrl('canonical')); } + /** + * Tests the cancel link works. + */ + public function testDeleteForm() { + $this->uploadTestPdf('fillpdf_test_v3.pdf'); + $form_id = $this->getLatestFillPdfForm(); + + // Verify the FillPdfForm's fields are stored. + $field_ids = \Drupal::entityQuery('fillpdf_form_field')->condition('fillpdf_form', $form_id)->execute(); + $this->assertCount(3, $field_ids, "3 FillPdfFormFields have been created."); + + // We're on the edit form. Click 'Delete' and confirm deletion. + $this->clickLink('Delete'); + $this->drupalPostForm(NULL, NULL, 'Delete'); + $this->assertSession()->pageTextContains('FillPDF form deleted.'); + $this->assertSession()->addressEquals(Url::fromRoute('fillpdf.forms_admin')); + + // Now verify the FillPdfForm and its fields have actually been deleted. + $this->assertNull(FillPdfForm::load($form_id), "The FillPdfForm #{$form_id} doesn't exist anymore."); + foreach ($field_ids as $id) { + $this->assertNull(FillPdfFormField::load($id), "The FillPdfFormField #{$id} doesn't exist anymore."); + } + } + }