Skip to content
Snippets Groups Projects
Commit ba662bad authored by Bernd Oliver Suenderhauf's avatar Bernd Oliver Suenderhauf
Browse files

Issue #3046126 by Pancho: Delete FillPdfFormFields together with the FillPdfForm

parent 07cd6d5b
No related branches found
No related tags found
No related merge requests found
......@@ -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}
*/
......
......@@ -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.");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment