You are here

public function InputHelper::parseFields in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/InputHelper.php \Drupal\fillpdf\InputHelper::parseFields()

Parses fields of a FillPDF form.

Parameters

\Drupal\fillpdf\FillPdfFormInterface $fillpdf_form: The FillPdfForm the PDF template file should be attached to.

Return value

\Drupal\fillpdf\FillPdfFormFieldInterface[] Associative array of FillPdfFormField objects keyed by the PDF key.

1 call to InputHelper::parseFields()
InputHelper::attachPdfToForm in src/InputHelper.php
Attaches a PDF template file to a FillPdfForm.

File

src/InputHelper.php, line 86

Class

InputHelper
Class InputHelper.

Namespace

Drupal\fillpdf

Code

public function parseFields(FillPdfFormInterface $fillpdf_form) {
  $config = $this->configManager
    ->get('fillpdf.settings');

  /** @var \Drupal\fillpdf\Plugin\PdfBackendInterface $backend */
  $backend = $this->backendManager
    ->createInstance($config
    ->get('backend'), $config
    ->get());

  // Attempt to parse the fields in the PDF.
  $parsed_fields = $backend
    ->parseFile(File::load($fillpdf_form->file->target_id));
  $unique_fields = [];
  foreach ((array) $parsed_fields as $field) {

    // Don't store "container" fields.
    if ($field['type']) {

      // Use the field name as key, so to consolidate duplicate fields.
      $unique_fields[$field['name']] = TRUE;
    }
  }

  // Create a FillPdfFormField object for each unique field.
  $form_fields = [];
  foreach (array_keys($unique_fields) as $pdf_key) {
    $form_fields[$pdf_key] = FillPdfFormField::create([
      'fillpdf_form' => $fillpdf_form,
      'pdf_key' => $pdf_key,
      'value' => '',
    ]);
  }
  return $form_fields;
}