-
Bernd Oliver Suenderhauf authored
Issue #3046257 by Pancho, wizonesolutions: Move EntityHelper::getFormFields() to the actual FillPdfForm entity and deprecate the 'fillpdf.entity_helper' service
Bernd Oliver Suenderhauf authoredIssue #3046257 by Pancho, wizonesolutions: Move EntityHelper::getFormFields() to the actual FillPdfForm entity and deprecate the 'fillpdf.entity_helper' service
FillPdfForm.php 7.54 KiB
<?php
namespace Drupal\fillpdf\Entity;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;
use Drupal\fillpdf\FillPdfFormInterface;
use Drupal\fillpdf\Service\FillPdfAdminFormHelper;
/**
* Defines the entity for managing uploaded FillPDF forms.
*
* @ContentEntityType(
* id = "fillpdf_form",
* label = @Translation("FillPDF form"),
* handlers = {
* "views_data" = "Drupal\fillpdf\FillPdfFormViewsData",
* "form" = {
* "edit" = "Drupal\fillpdf\Form\FillPdfFormForm",
* "delete" = "Drupal\fillpdf\Form\FillPdfFormDeleteForm",
* "export" = "Drupal\fillpdf\Form\FillPdfFormExportForm",
* "import" = "Drupal\fillpdf\Form\FillPdfFormImportForm",
* },
* "list_builder" = "Drupal\fillpdf\FillPdfFormListBuilder",
* "access" = "Drupal\fillpdf\FillPdfFormAccessControlHandler",
* },
* admin_permission = "administer pdfs",
* base_table = "fillpdf_forms",
* entity_keys = {
* "id" = "fid",
* "label" = "admin_title",
* "uuid" = "uuid",
* },
* links = {
* "canonical" = "/admin/structure/fillpdf/{fillpdf_form}",
* "edit-form" = "/admin/structure/fillpdf/{fillpdf_form}",
* "delete-form" = "/admin/structure/fillpdf/{fillpdf_form}/delete",
* "export-form" = "/admin/structure/fillpdf/{fillpdf_form}/export",
* "import-form" = "/admin/structure/fillpdf/{fillpdf_form}/import",
* "collection" = "/admin/structure/fillpdf",
* }
* )
*/
class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = [];
$fields['fid'] = BaseFieldDefinition::create('integer')
->setLabel(t('FillPDF Form ID'))
->setDescription(t('The ID of the FillPdfForm entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
$fields['uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->setDescription(t('The UUID of the FillPdfForm entity.'))
->setReadOnly(TRUE);
$fields['file'] = BaseFieldDefinition::create('file')
->setLabel(t('The associated managed file.'))
->setSetting('file_extensions', 'pdf')
->setDescription(t('The associated managed file.'));
$overview_url = Url::fromUri('base://admin/structure/fillpdf')->toString();
$fields['admin_title'] = BaseFieldDefinition::create('string')
->setLabel(t('Administrative title'))
->setDescription(t('Enter an administrative title to help identifying this FillPDF Form on the <a href="@overview_url">form overview page</a> and in some other places.', ['@overview_url' => $overview_url]))
->setDisplayOptions('form', [
'type' => 'string',
'weight' => 0,
]);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Filename pattern'))
->setDescription(t('Enter a title for this mapping configuration. This will be used for deciding the filename of your PDF. <strong>This field supports tokens.</strong>'))
->setDisplayOptions('form', [
'type' => 'string',
'weight' => 10,
]);
$fields['default_entity_type'] = BaseFieldDefinition::create('string')
->setLabel(t('Default entity type'))
->setDescription(t('The type of the below entity ID.'));
$fields['default_entity_id'] = BaseFieldDefinition::create('integer')
->setLabel(t('Default entity ID'))
->setDescription(t('The default entity ID to be filled from this FillPDF Form.'))
->setDisplayOptions('form', [
'type' => 'string',
'weight' => 15,
]);
$fields['destination_path'] = BaseFieldDefinition::create('string')
->setLabel(t('Where to save generated PDFs'))
->setDescription(t("<p>By default, filled PDFs are not saved to disk; they are simply sent
directly to the browser for download. Enter a path here to change this behavior (tokens allowed).
<strong>Warning! Unless you include the &download=1 flag in the FillPDF URL, PDFs will only
be saved to disk <em>and won't</em> be sent to the browser as well.</strong></p><p>The path
you specify must be in the following format:<br />
<ul>
<li><code>path/to/directory</code> (path will be treated as relative to
your <em>fillpdf</em> files subdirectory)</li>
filesystem)</li>
</ul>
Note that you are responsible for ensuring that the user under which PHP is running can write to this path. Do not include a trailing slash.</p>"))
->setDisplayOptions('form', [
'type' => 'string',
'weight' => 20,
]);
// @todo: add post_save_redirect field for where to send the browser by default after they generate a PDF
$fields['scheme'] = BaseFieldDefinition::create('list_string')
->setLabel('Storage system for generated PDFs')
->setDescription(t('This setting is used as the storage/download method for generated PDFs. The use of public files is more efficient, but does not provide any access control. Changing this setting will require you to migrate associated files and data yourself and is not recommended after you have uploaded a template.'))
->setSettings([
'allowed_values_function' => [get_called_class(), 'getSchemeAllowedValues'],
])
->setDefaultValueCallback(get_called_class() . '::getSchemeDefaultValue')
->setRequired(TRUE)
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 25,
]);
$fields['destination_redirect'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Redirect browser directly to saved PDF'))
->setDescription(t("<strong>This setting is applicable only if <em>Where to save generated PDFs</em> is set.</strong> Instead of redirecting your visitors to the front page, it will redirect them directly to the PDF. However, if you pass Drupal's <em>destination</em> query string parameter, that will override this setting."))
->setDisplayOptions('form', [
'type' => 'boolean_checkbox',
'weight' => 30,
'settings' => [
'display_label' => TRUE,
],
]);
// Restore database and make this be string_long instead like
// FillPdfFormField.
$fields['replacements'] = BaseFieldDefinition::create('string_long')
->setLabel(t('Change text before sending to PDF (Transform values)'))
->setDescription(FillPdfAdminFormHelper::getReplacementsDescription())
->setDisplayOptions('form', [
'type' => 'string_long',
'weight' => 40,
]);
return $fields;
}
/**
* {@inheritdoc}
*/
public function getFormFields() {
$field_ids = \Drupal::entityQuery('fillpdf_form_field')
->condition('fillpdf_form', $this->id())
->execute();
$field_storage = \Drupal::entityTypeManager()->getStorage('fillpdf_form_field');
return $field_storage->loadMultiple($field_ids);
}
/**
* Allowed values callback for 'scheme' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return array
* Stream wrapper descriptions, keyed by scheme.
*/
public static function getSchemeAllowedValues() {
return \Drupal::service('fillpdf.admin_form_helper')->schemeOptions();
}
/**
* Default value callback for 'scheme' base field definition.
*
* @see ::baseFieldDefinitions()
*
* @return string
* The system's default scheme.
*/
public static function getSchemeDefaultValue() {
return \Drupal::config('system.file')->get('default_scheme');
}
}