You are here

public function FillPdfFormForm::form in FillPDF 8.4

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

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/FillPdfFormForm.php, line 134

Class

FillPdfFormForm
Form controller for the FillPDFForm edit form.

Namespace

Drupal\fillpdf\Form

Code

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

  /** @var \Drupal\fillpdf\FillPdfFormInterface $fillpdf_form */
  $fillpdf_form = $this->entity;
  $form['title']['token_tree'] = $this->adminFormHelper
    ->getAdminTokenForm();

  // @todo: Encapsulate this logic into a ::getDefaultEntityType() method on FillPdfForm
  $stored_default_entity_type = $fillpdf_form
    ->get('default_entity_type');
  $default_entity_type = count($stored_default_entity_type) ? $stored_default_entity_type
    ->first()->value : NULL;
  $form['default_entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default entity type'),
    '#options' => $this
      ->getEntityTypeOptions(),
    '#empty_option' => $this
      ->t('- None -'),
    '#weight' => 12.5,
    '#default_value' => $default_entity_type,
    '#ajax' => [
      'callback' => '::ajaxUpdateEntityId',
      'event' => 'change',
      'wrapper' => 'test-entity-wrapper',
      'progress' => [
        'type' => 'none',
      ],
    ],
  ];

  // On AJAX-triggered rebuild, work with the user input instead of previously
  // stored values.
  if ($form_state
    ->isRebuilding()) {
    $default_entity_type = $form_state
      ->getValue('default_entity_type');
    $default_entity_id = $form_state
      ->getValue('default_entity_id');
  }
  else {
    $stored_default_entity_id = $fillpdf_form
      ->get('default_entity_id');
    $default_entity_id = count($stored_default_entity_id) ? $stored_default_entity_id
      ->first()->value : NULL;
  }
  $form['default_entity_id'] = [
    '#title' => $this
      ->t('Default entity'),
    '#target_type' => $default_entity_type,
    '#weight' => 13,
    '#prefix' => '<div id="test-entity-wrapper">',
    '#suffix' => '</div>',
    '#ajax' => [
      'callback' => '::ajaxUpdateEntityId',
      'event' => 'autocompleteclose autocompletechange',
      'wrapper' => 'test-entity-wrapper',
      'progress' => [
        'type' => 'none',
      ],
    ],
  ];

  // If a default entity type is set, allow selecting a default entity, too.
  if ($default_entity_type) {
    $storage = $this->entityTypeManager
      ->getStorage($default_entity_type);
    $default_entity = $default_entity_id ? $storage
      ->load($default_entity_id) : NULL;
    if (!empty($default_entity)) {
      $description = Link::fromTextAndUrl($this
        ->t('Download this PDF template populated with data from the @type %label (@id).', [
        '@type' => $default_entity_type,
        '%label' => $default_entity
          ->label(),
        '@id' => $default_entity_id,
      ]), $this->linkManipulator
        ->generateLink([
        'fid' => $this->entity
          ->id(),
        'entity_ids' => [
          $default_entity_type => [
            $default_entity_id,
          ],
        ],
      ]))
        ->toString();
    }
    $entity_ids = $storage
      ->getQuery()
      ->range(0, self::SELECT_MAX + 1)
      ->execute();
    if (count($entity_ids) > self::SELECT_MAX) {
      if (!isset($description)) {
        $description = $this
          ->t('Enter the title of a %type to test populating the PDF template.', [
          '%type' => $default_entity_type,
        ]);
      }
      $form['default_entity_id'] += [
        '#type' => 'entity_autocomplete',
        '#default_value' => $default_entity,
        '#description' => $description,
      ];
    }
    else {
      $options = [];
      foreach ($storage
        ->loadMultiple($entity_ids) as $id => $entity) {
        $options[$id] = $entity
          ->label();
      }
      if (!isset($description)) {
        $description = $this
          ->t('Choose a %type to test populating the PDF template.', [
          '%type' => $default_entity_type,
        ]);
      }
      $form['default_entity_id'] += [
        '#type' => 'select',
        '#options' => $options,
        '#empty_option' => $this
          ->t('- None -'),
        '#default_value' => $default_entity_id,
        '#description' => $description,
      ];
    }
  }
  else {
    $form['default_entity_id'] += [
      '#type' => 'hidden',
    ];
  }
  $fid = $fillpdf_form
    ->id();

  /** @var \Drupal\file\FileInterface $file_entity */
  $file_entity = File::load($fillpdf_form
    ->get('file')
    ->first()->target_id);
  $pdf_info_weight = 0;
  $form['pdf_info'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('PDF form information'),
    '#weight' => $form['default_entity_type']['#weight'] + 1,
    'submitted_pdf' => [
      '#type' => 'item',
      '#title' => $this
        ->t('Uploaded PDF'),
      '#description' => $file_entity
        ->getFileUri(),
      '#weight' => $pdf_info_weight++,
    ],
  ];
  $upload_location = FillPdf::buildFileUri($this
    ->config('fillpdf.settings')
    ->get('template_scheme'), 'fillpdf');
  if (!$this->fileSystem
    ->prepareDirectory($upload_location, FileSystemInterface::CREATE_DIRECTORY + FileSystemInterface::MODIFY_PERMISSIONS)) {
    $this
      ->messenger()
      ->addError($this
      ->t('The directory %directory does not exist or is not writable. Please check permissions.', [
      '%directory' => $this->fileSystem
        ->realpath($upload_location),
    ]));
  }
  else {
    $form['pdf_info']['upload_pdf'] = [
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Update PDF template'),
      '#accept' => 'application/pdf',
      '#upload_validators' => [
        'file_validate_extensions' => [
          'pdf',
        ],
      ],
      '#upload_location' => $upload_location,
      '#description' => $this
        ->t('Update the PDF file used as template by this form.'),
      '#weight' => $pdf_info_weight++,
    ];
  }
  $form['pdf_info']['sample_populate'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Sample PDF'),
    '#description' => $this
      ->t('@link<br />If you have set a custom path on this PDF, the sample will be saved there silently.', [
      '@link' => Link::fromTextAndUrl($this
        ->t('See which fields are which in this PDF.'), $this->linkManipulator
        ->generateLink([
        'fid' => $fid,
        'sample' => TRUE,
      ]))
        ->toString(),
    ]),
    '#weight' => $pdf_info_weight++,
  ];
  $form['pdf_info']['form_id'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Form info'),
    '#description' => $this
      ->t('Form ID: [@fid].  Populate this form with entity IDs, such as %path . For more usage examples, see <a href="@documentation">the documentation</a>.', [
      '@fid' => $fid,
      '%path' => "/fillpdf?fid={$fid}&entity_ids[]=node:10&entity_ids[]=user:7",
      '@documentation' => 'https://www.drupal.org/docs/8/modules/fillpdf/usage#makelink',
    ]),
    '#weight' => $pdf_info_weight,
  ];
  $available_schemes = $form['scheme']['widget']['#options'];

  // If only one option is available, this is 'none', so there's nothing to
  // chose.
  if (count($available_schemes) == 1) {
    $form['scheme']['#type'] = 'hidden';
    $form['destination_path']['#type'] = 'hidden';
    $form['destination_redirect']['#type'] = 'hidden';
  }
  else {
    $form['storage_download'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Storage and download'),
      '#weight' => $form['pdf_info']['#weight'] + 1,
      '#open' => TRUE,
      '#attached' => [
        'library' => [
          'fillpdf/form',
        ],
      ],
    ];
    $form['storage_download']['storage'] = [
      '#type' => 'container',
    ];

    // @todo: Check for empty value after Core issue is fixed.
    // See: https://www.drupal.org/project/drupal/issues/1585930
    $states_no_scheme = [
      ':input[name="scheme"]' => [
        'value' => '_none',
      ],
    ];
    $form['scheme']['#group'] = 'storage';
    $form['destination_path']['#group'] = 'storage';
    $form['destination_path']['widget']['0']['value']['#field_prefix'] = 'fillpdf/';
    $form['destination_path']['#states'] = [
      'invisible' => $states_no_scheme,
    ];
    $form['destination_path']['token_tree'] = $this->adminFormHelper
      ->getAdminTokenForm();
    $description = $this
      ->t('If filled PDFs should be automatically saved to disk, chose a file storage');
    $description .= isset($available_schemes['public']) ? '; ' . $this
      ->t('note that %public storage does not provide any access control.', [
      '%public' => 'public://',
    ]) : '.';
    $description .= ' ' . $this
      ->t('Otherwise, filled PDFs are sent to the browser for download.');
    $form['storage_download']['storage']['description_scheme_none'] = [
      '#type' => 'item',
      '#description' => $description,
      '#weight' => 22,
      '#states' => [
        'visible' => $states_no_scheme,
      ],
    ];
    $form['storage_download']['storage']['description_scheme_set'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('As PDFs are saved to disk, make sure you include the <em>&download=1</em> flag to send them to the browser as well.'),
      '#weight' => 23,
      '#states' => [
        'invisible' => $states_no_scheme,
      ],
    ];
    $form['destination_redirect']['#group'] = 'storage_download';
    $form['destination_redirect']['#states'] = [
      'invisible' => $states_no_scheme,
    ];
  }
  $form['additional_settings'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Additional settings'),
    '#weight' => $form['pdf_info']['#weight'] + 1,
    '#open' => $fillpdf_form && (!empty($fillpdf_form->replacements->value) || !empty($fillpdf_form->pdftk_encryption->value) || !empty($fillpdf_form->permissions->value) || !empty($fillpdf_form->owner_password->value) || !empty($fillpdf_form->user_password->value)),
  ];
  $form['replacements']['#group'] = 'additional_settings';
  $form['additional_settings']['security'] = [
    '#name' => 'security',
    '#type' => 'details',
    '#title' => t('PDF Security (currently only works with pdftk)'),
    '#weight' => 100,
    '#open' => $fillpdf_form && (!empty($fillpdf_form->pdftk_encryption->value) || !empty($fillpdf_form->permissions->value) || !empty($fillpdf_form->owner_password->value) || !empty($fillpdf_form->user_password->value)),
  ];
  $form['pdftk_encryption']['#group'] = 'security';
  $form['permissions']['#group'] = 'security';
  $form['owner_password']['#group'] = 'security';
  $form['user_password']['#group'] = 'security';

  // @todo: Add a button to let them attempt re-parsing if it failed.
  $form['fillpdf_fields']['fields'] = FillPdf::embedView('fillpdf_form_fields', 'block_1', $fillpdf_form
    ->id());
  $form['fillpdf_fields']['#weight'] = 100;
  $form['export_fields'] = [
    '#prefix' => '<div>',
    '#markup' => Link::fromTextAndUrl($this
      ->t('Export these field mappings'), Url::fromRoute('entity.fillpdf_form.export_form', [
      'fillpdf_form' => $fillpdf_form
        ->id(),
    ]))
      ->toString(),
    '#suffix' => '</div>',
    '#weight' => 100,
  ];
  $form['import_fields'] = [
    '#prefix' => '<div>',
    '#markup' => Link::fromTextAndUrl($this
      ->t('Import a previous export into this PDF'), Url::fromRoute('entity.fillpdf_form.import_form', [
      'fillpdf_form' => $fillpdf_form
        ->id(),
    ]))
      ->toString(),
    '#suffix' => '</div>',
    '#weight' => 100,
  ];
  return $form;
}