class FillPdfOverviewForm in FillPDF 5.0.x
Same name and namespace in other branches
- 8.4 src/Form/FillPdfOverviewForm.php \Drupal\fillpdf\Form\FillPdfOverviewForm
FillPDF overview form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\fillpdf\Form\FillPdfOverviewForm
Expanded class hierarchy of FillPdfOverviewForm
1 string reference to 'FillPdfOverviewForm'
File
- src/
Form/ FillPdfOverviewForm.php, line 21
Namespace
Drupal\fillpdf\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FillPdfOverviewForm:: |
protected | property | The FillPDF backend manager. | |
FillPdfOverviewForm:: |
protected | property | The file system. | |
FillPdfOverviewForm:: |
protected | property | The FillPDF input helper. | |
FillPdfOverviewForm:: |
protected | property | The module handler. | |
FillPdfOverviewForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
FillPdfOverviewForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FillPdfOverviewForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FillPdfOverviewForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
FillPdfOverviewForm:: |
public | function | Constructs a FillPdfSettingsForm object. | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
72 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |