public function form(array $form, FormStateInterface $form_state) {
    parent::form($form, $form_state);

    /** @var FillPdfFormInterface $entity */
    $entity = $this->getEntity();

    $code = $this->serializer->getFormExportCode($entity);

    $form = array();
    $form['export'] = array(
      '#type' => 'textarea',
      '#title' => t('FillPDF form configuration and mappings'),
      '#default_value' => $code,
      '#rows' => 30,
      '#description' => t('Copy this code and then on the site you want to import to, go to the Edit page for the FillPDF form for which you want to import these mappings, and paste it in there.'),
      '#attributes' => array(
        'style' => 'width: 97%;',
      ),
    );

    return $form;
  }
  /**
   * {@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();
  }
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state->cleanValues();

    /** @var \Drupal\fillpdf\FillPdfFormInterface $fillpdf_form */
    $fillpdf_form = $this->getEntity();

    $mappings = $form_state->getValue('mappings');

    /** @var \Drupal\fillpdf\FillPdfFormInterface $imported_form */
    $imported_form = $mappings['form'];

    /** @var array $imported_fields */
    $imported_fields = $mappings['fields'];

    $unmatched_pdf_keys = $this->serializer->importForm($fillpdf_form, $imported_form, $imported_fields);

    foreach ($unmatched_pdf_keys as $unmatched_pdf_key) {
      drupal_set_message($this->t('Your code contained field mappings for the PDF field key <em>@pdf_key</em>, but it does not exist on this form. Therefore, it was ignored.', ['@pdf_key' => $unmatched_pdf_key]), 'warning');
    }

    drupal_set_message($this->t('Successfully imported FillPDF form configuration and matching PDF field keys. If any field mappings failed to import, they are listed above.'));

    $form_state->setRedirect('entity.fillpdf_form.edit_form', ['fillpdf_form' => $fillpdf_form->id()]);
  }