You are here

public function FillPdfFormForm::save in FillPDF 8.4

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

Throws

\Drupal\Core\Entity\EntityStorageException

\Drupal\Core\Entity\EntityMalformedException

Overrides EntityForm::save

File

src/Form/FillPdfFormForm.php, line 512

Class

FillPdfFormForm
Form controller for the FillPDFForm edit form.

Namespace

Drupal\fillpdf\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\fillpdf\FillPdfFormInterface $entity */
  $entity = $this
    ->getEntity();
  $this
    ->messenger()
    ->addStatus($this
    ->t('FillPDF Form %link has been updated.', [
    '%link' => $entity
      ->toLink()
      ->toString(),
  ]));
  if ($form_state
    ->getValue('upload_pdf')) {
    $existing_fields = $entity
      ->getFormFields();

    /** @var \Drupal\file\FileInterface $new_file */
    $new_file = File::load($form_state
      ->getValue('upload_pdf')['0']);
    $new_file
      ->setPermanent();
    $new_file
      ->save();

    // Set the new file to our unsaved FillPdf form and parse its fields.
    $entity->file = $new_file;
    $new_fields = $this->inputHelper
      ->parseFields($entity);

    // Enrich the new field objects with existing values. Note that we pass
    // them in seemingly-reverse order since we want to import the EXISTING
    // fields into the NEW fields.
    $non_matching_fields = $this->serializer
      ->importFormFields($existing_fields, $new_fields, FALSE);

    // Save new fields.

    /** @var \Drupal\fillpdf\FillPdfFormFieldInterface $field */
    foreach ($new_fields as $field) {
      $field
        ->save();
    }

    // Delete existing fields. Importing the new fields saved them.

    /** @var \Drupal\fillpdf\FillPdfFormFieldInterface $field */
    foreach ($existing_fields as $field) {
      $field
        ->delete();
    }
    if (count($existing_fields)) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Your previous field mappings have been transferred to the new PDF template you uploaded.'));
    }
    if (count($non_matching_fields)) {
      $message = [
        '#prefix' => $this
          ->t("These keys couldn't be found in the new PDF:"),
        [
          '#theme' => 'item_list',
          '#items' => array_keys($non_matching_fields),
        ],
      ];
      $this
        ->messenger()
        ->addWarning(\Drupal::service('renderer')
        ->render($message));
    }
    $this
      ->messenger()
      ->addStatus($this
      ->t('You might also want to update the <em>Filename pattern</em> field; this has not been changed.'));
  }

  // Save custom form elements' values, resetting default_entity_id to NULL,
  // if not matching the default entity type.
  $default_entity_type = $form_state
    ->getValue('default_entity_type');
  $default_entity_id = $default_entity_type == $form['default_entity_id']['#target_type'] ? $form_state
    ->getValue('default_entity_id') : NULL;
  $entity
    ->set('default_entity_type', $default_entity_type)
    ->set('default_entity_id', $default_entity_id)
    ->save();
}