diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php index 68543fcdb1d274defbb2872784d3048267501369..6191ae43cef61201caf6d9ea761942344c71befe 100644 --- a/src/Entity/FillPdfForm.php +++ b/src/Entity/FillPdfForm.php @@ -28,11 +28,9 @@ use Drupal\fillpdf\FillPdfFormInterface; * base_table = "fillpdf_forms", * entity_keys = { * "id" = "fid", + * "label" = "title", * "uuid" = "uuid", * }, - * links = { - * "delete-form" = "/admin/structure/fillpdf/{fillpdf_form}/delete" - * } * ) */ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface { @@ -78,19 +76,10 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface { 'weight' => 10, )); - // @todo: Validate this with a custom constraint or whatever - $fields['default_entity_type'] = BaseFieldDefinition::create('string') - ->setLabel(t('Default entity type')) - ->setDescription(t('The type of the below entity ID.')); - - // @todo: Validate this with a custom constraint, if possible - $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', array( - 'type' => 'string', - 'weight' => 15, - )); + // @todo: Revisit this...I would probably need to store the entity and bundle types as well. +// $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.')); // @todo: set display options on this $fields['destination_path'] = BaseFieldDefinition::create('string') diff --git a/src/FillPdfLinkManipulatorInterface.php b/src/FillPdfLinkManipulatorInterface.php index e66d1107fef6493c546d9debd88d6a10e88fb02b..26efd4f84d36d2dc4727255ea7250c5ebf75ad14 100644 --- a/src/FillPdfLinkManipulatorInterface.php +++ b/src/FillPdfLinkManipulatorInterface.php @@ -5,7 +5,9 @@ */ namespace Drupal\fillpdf; + use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * Defines an interface to allow parsing and building FillPDF Links. @@ -17,7 +19,7 @@ use Symfony\Component\HttpFoundation\Request; interface FillPdfLinkManipulatorInterface { /** - * @param \Symfony\Component\HttpFoundation\Request $request The request containing the query string to parse. + * @param Request $request The request containing the query string to parse. * @return array */ public function parseLink(Request $request); @@ -26,7 +28,7 @@ interface FillPdfLinkManipulatorInterface { * @param array $parameters * The array of parameters to be converted into a * URL and query string. - * @return \Drupal\Core\Url + * @return string */ public function generateLink(array $parameters); diff --git a/src/Form/FillPdfFormForm.php b/src/Form/FillPdfFormForm.php index 1e995c8a6b161b4733d405a2786ed6ee549ae415..a7e63b0adc09b1b1c70fffea0fdc445884ece56f 100644 --- a/src/Form/FillPdfFormForm.php +++ b/src/Form/FillPdfFormForm.php @@ -7,30 +7,20 @@ namespace Drupal\fillpdf\Form; use Drupal\Core\Entity\ContentEntityForm; use Drupal\Core\Form\FormStateInterface; -use Drupal\file\Entity\File; -use Drupal\file\FileInterface; use Drupal\fillpdf\Component\Utility\FillPdf; use Drupal\fillpdf\FillPdfAdminFormHelperInterface; -use Drupal\fillpdf\FillPdfFormInterface; -use Drupal\fillpdf\FillPdfLinkManipulatorInterface; use Symfony\Component\DependencyInjection\ContainerInterface; class FillPdfFormForm extends ContentEntityForm { protected $adminFormHelper; - protected $linkManipulator; - public function __construct(FillPdfAdminFormHelperInterface $admin_form_helper, - FillPdfLinkManipulatorInterface $link_manipulator) { + public function __construct(FillPdfAdminFormHelperInterface $admin_form_helper) { $this->adminFormHelper = $admin_form_helper; - $this->linkManipulator = $link_manipulator; } public static function create(ContainerInterface $container) { - return new static( - $container->get('fillpdf.admin_form_helper'), - $container->get('fillpdf.link_manipulator') - ); + return new static($container->get('fillpdf.admin_form_helper')); } /** @@ -39,7 +29,7 @@ class FillPdfFormForm extends ContentEntityForm { public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - /** @var FillPdfFormInterface $entity */ + /** @var FillPdfForm $entity */ $entity = $this->entity; $form['tokens'] = array( @@ -50,125 +40,20 @@ class FillPdfFormForm extends ContentEntityForm { 'token_tree' => $this->adminFormHelper->getAdminTokenForm(), ); - $entity_types = array(); - $entity_type_definitions = $this->entityManager->getDefinitions(); - - foreach ($entity_type_definitions as $machine_name => $definition) { - $label = $definition->getLabel(); - $entity_types[$machine_name] = "$machine_name ($label)"; - } - - // @todo: Encapsulate this logic into a ::getDefaultEntityType() method on FillPdfForm - $default_entity_type = $entity->get('default_entity_type')->first()->value; - if (empty($default_entity_type)) { - $default_entity_type = 'node'; - } - - $form['default_entity_type'] = array( - '#type' => 'select', - '#title' => $this->t('Default entity type'), - '#options' => $entity_types, - '#weight' => 12.5, - '#default_value' => $default_entity_type, - ); - - $fid = $entity->id(); - - /** @var FileInterface $file_entity */ - $file_entity = File::load($entity->get('file')->first()->target_id); - $pdf_info_weight = 0; - $form['pdf_info'] = array( - '#type' => 'fieldset', - '#title' => $this->t('PDF form information'), - '#weight' => $form['default_entity_id']['#weight'] + 1, - - 'submitted_pdf' => array( - '#type' => 'item', - '#title' => t('Uploaded PDF'), - '#description' => $file_entity->getFileUri(), - '#weight' => $pdf_info_weight++, - ), - - // @todo: make work - 'upload_pdf' => array( - '#type' => 'file', - '#title' => 'Update PDF template', - '#description' => 'Update the PDF template used by this form', - '#weight' => $pdf_info_weight++, - ), - - 'sample_populate' => array( - '#type' => 'item', - '#title' => 'Sample PDF', - '#description' => $this->l($this->t('See which fields are which in this PDF.'), - $this->linkManipulator->generateLink(array( - 'fid' => $fid, - 'sample' => TRUE, - ))) . '<br />' . - $this->t('If you have set a custom path on this PDF, the sample will be saved there silently.'), - '#weight' => $pdf_info_weight++, - ), - - 'form_id' => array( - '#type' => 'item', - '#title' => 'Form Info', - '#description' => "Form ID: [$fid]. Populate this form with entity IDs, such as /fillpdf?fid=$fid&entity_type=node&entity_id=10<br/>", - '#weight' => $pdf_info_weight++, - ), - ); - - if (!empty($entity->get('default_entity_id')->first()->value)) { - $form['pdf_info']['populate_default'] = array( - '#type' => 'item', - '#title' => 'Fill PDF from default node', - '#description' => $this->l($this->t('Download this PDF filled with data from the default entity (@entity_type:@entity).', - array( - '@entity_type', $entity->get('default_entity_type')->first()->value, - '@entity' => $entity->get('default_entity_id')->first()->value) - ), - $this->linkManipulator->generateLink(array('fid' => $fid))) . '<br />' . - $this->t('If you have set a custom path on this PDF, the sample will be saved there silently.'), - '#weight' => $form['pdf_info']['form_id']['#weight'] - 0.1, - ); - } - - $form['additional_settings'] = array( - '#type' => 'details', - '#title' => $this->t('Additional settings'), - '#weight' => $form['pdf_info']['#weight'] + 1, - '#open' => $entity->get('destination_path')->first()->value || $entity->get('destination_redirect')->first()->value, - ); - - $form['destination_path']['#group'] = 'additional_settings'; - $form['destination_redirect']['#group'] = 'additional_settings'; - $form['fillpdf_fields'] = FillPdf::embedView('fillpdf_form_fields', 'block_1', $entity->id()); $form['fillpdf_fields']['#weight'] = 100; - // @todo: Add import/export links once those routes actually exist - return $form; } - public function validate(array $form, FormStateInterface $form_state) { - // @todo: default_entity_id without a default entity type might not make sense. but maybe defaulting to node is fine for now. - - return parent::validate($form, $form_state); - } - - /** * {@inheritdoc} */ public function save(array $form, FormStateInterface $form_state) { - /** @var FillPdfFormInterface $entity */ $entity = $this->getEntity(); - - $entity->set('default_entity_type', $form_state->getValue('default_entity_type')); - $entity->save(); } diff --git a/src/Service/FillPdfAdminFormHelper.php b/src/Service/FillPdfAdminFormHelper.php index 55cae96187df2a3ce7aef00f0078846d654cd1f7..464efb52ecc1e602f32a79daab16c0040935d2e3 100644 --- a/src/Service/FillPdfAdminFormHelper.php +++ b/src/Service/FillPdfAdminFormHelper.php @@ -22,10 +22,17 @@ class FillPdfAdminFormHelper implements FillPdfAdminFormHelperInterface { * {@inheritdoc} */ public function getAdminTokenForm() { + $token_types = array('node', 'webform-tokens', 'submission'); + + // If not using Webform Rules, then show potential Webform Tokens + // webform:-namespaced tokens. + if ($this->moduleHandler->moduleExists('webform_rules') === FALSE) { + $token_types[] = 'webform'; + } return array( '#theme' => 'token_tree', - '#token_types' => 'all', - '#global_types' => TRUE, + '#token_types' => $token_types, + '#global_types' => FALSE, ); } diff --git a/src/Service/FillPdfLinkManipulator.php b/src/Service/FillPdfLinkManipulator.php index 366118014c8007ccca90419ecfc9bb601ee23540..ee900b84b35f859cf4bcd731dcd47e21b3ac4975 100644 --- a/src/Service/FillPdfLinkManipulator.php +++ b/src/Service/FillPdfLinkManipulator.php @@ -6,10 +6,8 @@ namespace Drupal\fillpdf\Service; -use Drupal\Core\Url; use Drupal\fillpdf\FillPdfLinkManipulatorInterface; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; class FillPdfLinkManipulator implements FillPdfLinkManipulatorInterface { @@ -50,9 +48,6 @@ class FillPdfLinkManipulator implements FillPdfLinkManipulatorInterface { $entity_type = $entity_id_parts[0]; $entity_id = $entity_id_parts[1]; } - elseif (!empty($request_context['entity_type'])) { - $entity_type = $request_context['entity_type']; - } else { $entity_type = 'node'; } @@ -75,28 +70,13 @@ class FillPdfLinkManipulator implements FillPdfLinkManipulatorInterface { } /** - * {@inheritdoc} + * @param array $parameters + * The array of parameters to be converted into a + * URL and query string. + * @return string */ public function generateLink(array $parameters) { - $query_options = array(); - - if (!isset($parameters['fid'])) { - throw new \InvalidArgumentException("The $parameters argument must contain the fid key (the FillPdfForm's ID)."); - } - - $query_options['fid'] = $parameters['fid']; - - if (!empty($parameters['sample'])) { - $query_options['sample'] = 1; - } - - // TODO: Implement rest of generateLink() method. - - $fillpdf_link = Url::fromRoute('fillpdf.populate_pdf', - array(), - array('query' => $query_options)); - - return $fillpdf_link; + // TODO: Implement generateLink() method. } }