You are here

public function FillPdfOverviewForm::buildForm in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Form/FillPdfOverviewForm.php \Drupal\fillpdf\Form\FillPdfOverviewForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

src/Form/FillPdfOverviewForm.php, line 95

Class

FillPdfOverviewForm
FillPDF overview form.

Namespace

Drupal\fillpdf\Form

Code

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

  // @todo: convert to OOP
  $form['existing_forms'] = views_embed_view('fillpdf_forms', 'block_1');
  $config = $this
    ->config('fillpdf.settings');

  // Only show PDF upload form if fillpdf is configured.
  if (!$config
    ->get('backend')) {
    $form['message'] = [
      '#markup' => '<p>' . $this
        ->t('Before you can upload PDF files, you must @link.', [
        '@link' => new FormattableMarkup(Link::fromTextAndUrl($this
          ->t('configure FillPDF'), Url::fromRoute('fillpdf.settings'))
          ->toString(), []),
      ]) . '</p>',
    ];
    $this
      ->messenger()
      ->addError($this
      ->t('FillPDF is not configured.'));
    return $form;
  }

  // If using FillPDF Service, ensure XML-RPC module is present.
  if ($config
    ->get('backend') === 'fillpdf_service' && !$this->moduleHandler
    ->moduleExists('xmlrpc')) {
    $this
      ->messenger()
      ->addError($this
      ->t('You must install the <a href="@xmlrpc">contributed XML-RPC module</a> in order to use FillPDF Service as your PDF-filling method.', [
      '@xmlrpc' => Url::fromUri('https://drupal.org/project/xmlrpc')
        ->toString(),
    ]));
    return $form;
  }
  $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['upload_pdf'] = [
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Upload PDF template'),
      '#accept' => 'application/pdf',
      '#upload_validators' => [
        'file_validate_extensions' => [
          'pdf',
        ],
      ],
      '#upload_location' => $upload_location,
      '#description' => $this
        ->t('Upload a fillable PDF file to create a new form.'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Create'),
      '#weight' => 15,
    ];
  }
  return $form;
}