You are here

class FillPdfOverviewForm in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/Form/FillPdfOverviewForm.php \Drupal\fillpdf\Form\FillPdfOverviewForm

FillPDF overview form.

Hierarchy

Expanded class hierarchy of FillPdfOverviewForm

1 string reference to 'FillPdfOverviewForm'
fillpdf.routing.yml in ./fillpdf.routing.yml
fillpdf.routing.yml

File

src/Form/FillPdfOverviewForm.php, line 21

Namespace

Drupal\fillpdf\Form
View source
class FillPdfOverviewForm extends FormBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The FillPDF backend manager.
   *
   * @var \Drupal\fillpdf\Plugin\PdfBackendManager
   */
  protected $backendManager;

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The FillPDF input helper.
   *
   * @var \Drupal\fillpdf\InputHelperInterface
   */
  protected $inputHelper;

  /**
   * Constructs a FillPdfSettingsForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\fillpdf\Plugin\PdfBackendManager $backend_manager
   *   The FillPDF backend manager.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   * @param \Drupal\fillpdf\InputHelperInterface $input_helper
   *   The FillPDF input helper.
   */
  public function __construct(ModuleHandlerInterface $module_handler, PdfBackendManager $backend_manager, FileSystemInterface $file_system, InputHelperInterface $input_helper) {
    $this->moduleHandler = $module_handler;
    $this->backendManager = $backend_manager;
    $this->fileSystem = $file_system;
    $this->inputHelper = $input_helper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('module_handler'), $container
      ->get('plugin.manager.fillpdf.pdf_backend'), $container
      ->get('file_system'), $container
      ->get('fillpdf.input_helper'));
  }

  /**
   * Returns a unique string identifying the form.
   *
   * @return string
   *   The unique string identifying the form.
   */
  public function getFormId() {
    return 'fillpdf_forms_admin';
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Form submission handler.
   *
   * @param array $form
   *   An associative array containing the structure of the form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('upload_pdf')) {

      /** @var \Drupal\file\FileInterface $file */
      $file = File::load($form_state
        ->getValue('upload_pdf')['0']);
      $added = $this->inputHelper
        ->attachPdfToForm($file);

      /** @var \Drupal\fillpdf\Entity\FillPdfForm $fillpdf_form */
      $fillpdf_form = $added['form'];
      $fid = $fillpdf_form
        ->id();
      $this
        ->logger('fillpdf')
        ->notice('Added FillPDF form %id.', [
        '%id' => $fid,
      ]);
      $this
        ->messenger()
        ->addStatus($this
        ->t('New FillPDF form has been created.'));

      /** @var \Drupal\fillpdf\FillPdfFormFieldInterface[] $form_fields */
      $form_fields = $added['fields'];
      if (count($form_fields) === 0) {
        $this
          ->messenger()
          ->addWarning($this
          ->t('No fields detected in PDF. Are you sure it contains editable fields?'));
      }
      else {
        $this
          ->messenger()
          ->addStatus($this
          ->t("You may now create mappings between the fields of the PDF form and an entity type."));
      }
      $form_state
        ->setRedirect('entity.fillpdf_form.edit_form', [
        'fillpdf_form' => $fid,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FillPdfOverviewForm::$backendManager protected property The FillPDF backend manager.
FillPdfOverviewForm::$fileSystem protected property The file system.
FillPdfOverviewForm::$inputHelper protected property The FillPDF input helper.
FillPdfOverviewForm::$moduleHandler protected property The module handler.
FillPdfOverviewForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
FillPdfOverviewForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
FillPdfOverviewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FillPdfOverviewForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
FillPdfOverviewForm::__construct public function Constructs a FillPdfSettingsForm object.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.