From fcfa48c0529443458893a05fc9fae0d23ff28902 Mon Sep 17 00:00:00 2001 From: Kevin Kaland <kevin@wizonesolutions.com> Date: Fri, 15 May 2015 17:00:00 -0700 Subject: [PATCH] Issue #2539213: Flesh out FillPdfForm more. --- src/Entity/FillPdfForm.php | 21 +++- src/FillPdfLinkManipulatorInterface.php | 6 +- src/Form/FillPdfFormForm.php | 121 +++++++++++++++++++++++- src/Service/FillPdfAdminFormHelper.php | 11 +-- src/Service/FillPdfLinkManipulator.php | 30 +++++- 5 files changed, 163 insertions(+), 26 deletions(-) diff --git a/src/Entity/FillPdfForm.php b/src/Entity/FillPdfForm.php index 6191ae4..68543fc 100644 --- a/src/Entity/FillPdfForm.php +++ b/src/Entity/FillPdfForm.php @@ -28,9 +28,11 @@ 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 { @@ -76,10 +78,19 @@ class FillPdfForm extends ContentEntityBase implements FillPdfFormInterface { 'weight' => 10, )); - // @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: 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: set display options on this $fields['destination_path'] = BaseFieldDefinition::create('string') diff --git a/src/FillPdfLinkManipulatorInterface.php b/src/FillPdfLinkManipulatorInterface.php index 26efd4f..e66d110 100644 --- a/src/FillPdfLinkManipulatorInterface.php +++ b/src/FillPdfLinkManipulatorInterface.php @@ -5,9 +5,7 @@ */ namespace Drupal\fillpdf; - use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\RequestStack; /** * Defines an interface to allow parsing and building FillPDF Links. @@ -19,7 +17,7 @@ use Symfony\Component\HttpFoundation\RequestStack; interface FillPdfLinkManipulatorInterface { /** - * @param Request $request The request containing the query string to parse. + * @param \Symfony\Component\HttpFoundation\Request $request The request containing the query string to parse. * @return array */ public function parseLink(Request $request); @@ -28,7 +26,7 @@ interface FillPdfLinkManipulatorInterface { * @param array $parameters * The array of parameters to be converted into a * URL and query string. - * @return string + * @return \Drupal\Core\Url */ public function generateLink(array $parameters); diff --git a/src/Form/FillPdfFormForm.php b/src/Form/FillPdfFormForm.php index a7e63b0..1e995c8 100644 --- a/src/Form/FillPdfFormForm.php +++ b/src/Form/FillPdfFormForm.php @@ -7,20 +7,30 @@ 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) { + public function __construct(FillPdfAdminFormHelperInterface $admin_form_helper, + FillPdfLinkManipulatorInterface $link_manipulator) { $this->adminFormHelper = $admin_form_helper; + $this->linkManipulator = $link_manipulator; } public static function create(ContainerInterface $container) { - return new static($container->get('fillpdf.admin_form_helper')); + return new static( + $container->get('fillpdf.admin_form_helper'), + $container->get('fillpdf.link_manipulator') + ); } /** @@ -29,7 +39,7 @@ class FillPdfFormForm extends ContentEntityForm { public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); - /** @var FillPdfForm $entity */ + /** @var FillPdfFormInterface $entity */ $entity = $this->entity; $form['tokens'] = array( @@ -40,20 +50,125 @@ 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 464efb5..55cae96 100644 --- a/src/Service/FillPdfAdminFormHelper.php +++ b/src/Service/FillPdfAdminFormHelper.php @@ -22,17 +22,10 @@ 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' => $token_types, - '#global_types' => FALSE, + '#token_types' => 'all', + '#global_types' => TRUE, ); } diff --git a/src/Service/FillPdfLinkManipulator.php b/src/Service/FillPdfLinkManipulator.php index ee900b8..3661180 100644 --- a/src/Service/FillPdfLinkManipulator.php +++ b/src/Service/FillPdfLinkManipulator.php @@ -6,8 +6,10 @@ 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 { @@ -48,6 +50,9 @@ 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'; } @@ -70,13 +75,28 @@ class FillPdfLinkManipulator implements FillPdfLinkManipulatorInterface { } /** - * @param array $parameters - * The array of parameters to be converted into a - * URL and query string. - * @return string + * {@inheritdoc} */ public function generateLink(array $parameters) { - // TODO: Implement generateLink() method. + $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; } } -- GitLab