Example #1
0
  /**
   * {@inheritdoc}
   */
  public function importForm(FillPdfFormInterface $fillpdf_form, FillPdfFormInterface $imported_form, array $imported_fields) {
    // Key the existing FillPDF fields on PDF keys.
    $existing_fields = $this->entityHelper->getFormFields($fillpdf_form);

    // Iterate over FillPdfForm fields and copy them, EXCEPT for IDs and
    // references.
    $fillpdf_form_type = $this->entityTypeManager->getDefinition('fillpdf_form');
    $form_fields_to_ignore = array_filter(array_values($fillpdf_form_type->getKeys()));
    $form_fields_to_ignore[] = 'file';
    foreach ($imported_form->getFields() as $name => $data) {
      if (!in_array($name, $form_fields_to_ignore, TRUE)) {
        $fillpdf_form->{$name} = $data;
      }
    }
    $fillpdf_form->save();
    $unmatched_pdf_keys = $this->importFormFields($imported_fields, $existing_fields);

    return $unmatched_pdf_keys;
  }
  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {
    /** @var FillPdfFormInterface $entity */
    $entity = $this->getEntity();

    /** @var \Drupal\file\FileInterface $file */
    $file = $form_state->getValue('upload_pdf');

    if ($file) {
      $existing_fields = $this->entityHelper->getFormFields($entity);

      // Delete existing fields.
      /** @var FillPdfFormFieldInterface $existing_field */
      foreach ($existing_fields as $existing_field) {
        $existing_field->delete();
      }

      $added = $this->inputHelper->attachPdfToForm($file, $entity);

      $form_fields = $added['fields'];

      // Import previous form field values over new fields.
      $non_matching_msg = '';
      $non_matching_fields = $this->serializer->importFormFieldsByKey($existing_fields, $form_fields);
      if (count($non_matching_fields)) {
        $non_matching_msg = $this->t(" These keys couldn't be found in the new PDF");
      }

      drupal_set_message($this->t("Your previous field mappings have been transferred to the new PDF template you uploaded.") . $non_matching_msg);

      foreach ($non_matching_fields as $non_matching_field) {
        drupal_set_message($non_matching_field, 'warning');
      }

      drupal_set_message($this->t('You might also want to update the <em>Filename pattern</em> field; this has not been changed.'));
    }

    $entity->set('default_entity_type', $form_state->getValue('default_entity_type'));

    $entity->save();
  }