diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php index 56765f874040ae3b4f63b679ea0ee7c59a51eead..2d89e19358fcf4f96f6c6884e0cd7eb93a00a841 100644 --- a/src/Entity/FillPdfForm.php +++ b/src/Entity/FillPdfForm.php @@ -227,4 +227,17 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface { return array_intersect($available, $allowed); } + /** + * {@inheritdoc} + */ + public function getPropertiesToExport() { + $fields = array_keys($this->getFields()); + $fields_to_ignore = [ + 'fid', + 'uuid', + 'file', + ]; + return array_diff($fields, $fields_to_ignore); + } + } diff --git a/src/Entity/FillPdfFormField.php b/src/Entity/FillPdfFormField.php index 10847e905219b8658c0383ac20fe22815d5a113b..18fa69c58b36d7b04b0f8f60227b7627dd4ffbb7 100644 --- a/src/Entity/FillPdfFormField.php +++ b/src/Entity/FillPdfFormField.php @@ -103,4 +103,17 @@ class FillPdfFormField extends ContentEntityBase implements FillPdfFormFieldInte return $fields; } + /** + * {@inheritdoc} + */ + public function getPropertiesToExport() { + $fields = array_keys($this->getFields()); + $fields_to_ignore = [ + 'ffid', + 'uuid', + 'fillpdf_form', + ]; + return array_diff($fields, $fields_to_ignore); + } + } diff --git a/src/ExportableContentEntityInterface.php b/src/ExportableContentEntityInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..37d17bfb90cd150d1e2c188413d2522c162e5301 --- /dev/null +++ b/src/ExportableContentEntityInterface.php @@ -0,0 +1,22 @@ +<?php + +namespace Drupal\fillpdf; + +use Drupal\Core\Entity\ContentEntityInterface; + +/** + * Interface ExportableContentEntityInterface. + * + * @package Drupal\fillpdf + */ +interface ExportableContentEntityInterface extends ContentEntityInterface { + + /** + * Gets the content entity properties to export if declared on the annotation. + * + * @return array|null + * The properties to export or NULL if they can not be determined. + */ + public function getPropertiesToExport(); + +} diff --git a/src/FillPdfFormFieldInterface.php b/src/FillPdfFormFieldInterface.php index 739888dd5767f3fed96e4b53a7da083ab297c677..19cf60cc7327b2e80ed3750978a6bd5b8bf9d806 100644 --- a/src/FillPdfFormFieldInterface.php +++ b/src/FillPdfFormFieldInterface.php @@ -2,13 +2,11 @@ namespace Drupal\fillpdf; -use Drupal\Core\Entity\ContentEntityInterface; - /** * Interface FillPdfFormFieldInterface. * * @package Drupal\fillpdf */ -interface FillPdfFormFieldInterface extends ContentEntityInterface { +interface FillPdfFormFieldInterface extends ExportableContentEntityInterface { } diff --git a/src/FillPdfFormInterface.php b/src/FillPdfFormInterface.php index b9f1eadee026716c684cae6cffeb7e9cf2b204b8..b5fbd9cc079134ce319f1948e2032c12b9a88f76 100644 --- a/src/FillPdfFormInterface.php +++ b/src/FillPdfFormInterface.php @@ -2,14 +2,12 @@ namespace Drupal\fillpdf; -use Drupal\Core\Entity\ContentEntityInterface; - /** * Interface FillPdfFormInterface. * * @package Drupal\fillpdf */ -interface FillPdfFormInterface extends ContentEntityInterface { +interface FillPdfFormInterface extends ExportableContentEntityInterface { /** * Returns all FillPdfFormFields associated with this FillPdfForm. diff --git a/src/Serializer.php b/src/Serializer.php index cfb750dd7973666a7ca1e6f4db1a62c26b653a4d..2c0d841bcabc89a1af8393c66e8ea01b49f8d576 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -79,24 +79,17 @@ class Serializer implements SerializerInterface { * {@inheritdoc} */ public function importForm(FillPdfFormInterface $fillpdf_form, FillPdfFormInterface $imported_form, array $imported_fields) { - // Key the existing FillPDF fields on PDF keys. - $existing_fields = $fillpdf_form->getFormFields(); - - // Iterate over FillPdfForm fields and copy them, EXCEPT for IDs and - // references. - $form_fields_to_ignore = [ - 'fid', - 'uuid', - 'file', - ]; + $properties_to_import = $imported_form->getPropertiesToExport(); foreach ($imported_form->getFields() as $name => $data) { - if (!in_array($name, $form_fields_to_ignore, TRUE)) { + if (in_array($name, $properties_to_import, TRUE)) { $fillpdf_form->{$name} = $data; } } $fillpdf_form->save(); - $unmatched_pdf_keys = $this->importFormFields($imported_fields, $existing_fields); + // Key the existing FillPDF fields on PDF keys. + $existing_fields = $fillpdf_form->getFormFields(); + $unmatched_pdf_keys = $this->importFormFields($imported_fields, $existing_fields); return $unmatched_pdf_keys; } @@ -104,34 +97,25 @@ class Serializer implements SerializerInterface { * {@inheritdoc} */ public function importFormFields(array $keyed_fields, array $existing_fields = []) { - // Iterate over each FillPdfFormField and override matching PDF keys - // (if any current fields have them). $existing_fields_by_key = []; foreach ($existing_fields as $existing_field) { $existing_fields_by_key[$existing_field->pdf_key->value] = $existing_field; } $existing_field_pdf_keys = array_keys($existing_fields_by_key); - $field_fields_to_ignore = [ - 'ffid', - 'uuid', - 'fillpdf_form', - ]; $unmatched_pdf_keys = []; foreach ($keyed_fields as $pdf_key => $keyed_field) { // If the imported field's PDF key matching the PDF key of the - // existing field, then copy the constituent entity fields. - // I know: they're both called fields. It's confusing as hell. - // I am sorry. + // existing field, then copy the constituent entity properties. if (in_array($pdf_key, $existing_field_pdf_keys, TRUE)) { - $existing_field_by_key = $existing_fields_by_key[$pdf_key]; + $properties_to_import = $keyed_field->getPropertiesToExport(); foreach ($keyed_field->getFields() as $keyed_field_name => $keyed_field_data) { - if (!in_array($keyed_field_name, $field_fields_to_ignore, TRUE)) { - $existing_field_by_key->{$keyed_field_name} = $keyed_field_data; + if (in_array($keyed_field_name, $properties_to_import, TRUE)) { + $existing_fields_by_key[$pdf_key]->{$keyed_field_name} = $keyed_field_data; } } - $existing_field_by_key->save(); + $existing_fields_by_key[$pdf_key]->save(); } else { $unmatched_pdf_keys[] = $pdf_key;