You are here

public function Serializer::importFormFields in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Serializer.php \Drupal\fillpdf\Serializer::importFormFields()

Imports FillPDF form fields.

Overwrites empty field values with previous existing field values.

Parameters

\Drupal\fillpdf\FillPdfFormFieldInterface[] $keyed_fields: Associative array of unsaved FillPDF Form objects keyed by PDF key.

\Drupal\fillpdf\FillPdfFormFieldInterface[] $existing_fields: (optional) Array of existing PDF keys.

bool $save_existing_fields: Whether to save the form fields in $existing_fields after updating them. If you pass FALSE, you will have to save them yourself.

Return value

string[] Array of unmatched PDF keys.

Overrides SerializerInterface::importFormFields

2 calls to Serializer::importFormFields()
Serializer::importForm in src/Serializer.php
Imports a FillPDF form..
Serializer::importFormFieldsByKey in src/Serializer.php
Overwrites empty new field values with previous existing values.

File

src/Serializer.php, line 102

Class

Serializer
Class Serializer.

Namespace

Drupal\fillpdf

Code

public function importFormFields(array $keyed_fields, array &$existing_fields = [], $save_existing_fields = TRUE) : array {
  $affected_fields = [];
  $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 properties.
    if (in_array($pdf_key, array_keys($existing_fields), TRUE)) {
      $properties_to_import = $keyed_field
        ->getPropertiesToExport();
      foreach ($keyed_field
        ->getFields() as $keyed_field_name => $keyed_field_data) {
        if (in_array($keyed_field_name, $properties_to_import, TRUE)) {
          $existing_fields[$pdf_key]->{$keyed_field_name} = $keyed_field_data;
        }
      }
      $affected_fields[] = $pdf_key;
    }
    else {
      $unmatched_pdf_keys[] = $pdf_key;
    }
  }

  // Save changed fields.
  foreach ($affected_fields as $pdf_key) {
    $existing_fields[$pdf_key]
      ->save();
  }
  return $unmatched_pdf_keys;
}