From 124c76c2cee73b46881a170f811ca978e687ff25 Mon Sep 17 00:00:00 2001 From: qdoscc <qdoscc@3133911.no-reply.drupal.org> Date: Tue, 14 Apr 2020 16:01:36 +0200 Subject: [PATCH] Issue #2189049 by wizonesolutions, qdoscc: Allow working with nodes in Rules --- fillpdf.module | 4 +++- fillpdf.rules.inc | 31 +++++++++++++++++++++++++ tests/FillPdfMergeTestCase.test | 40 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/fillpdf.module b/fillpdf.module index 5eaa5f5..ac3b032 100644 --- a/fillpdf.module +++ b/fillpdf.module @@ -919,7 +919,9 @@ function fillpdf_load_entities($fillpdf_info, $nids, $webform_array, $uc_order_i if ($entity_mode) { // If no entity IDs are specified but we have a default NID, prime a plain // entity ID here. The default entity type will be added just below. - if (empty($entity_ids) && !empty($fillpdf_info->default_nid)) { + // Note that we don't need a default entity if we already have a Node + // or Webform in context. + if (empty($nids) && empty($webform_array) && empty($entity_ids) && !empty($fillpdf_info->default_nid)) { $entity_ids = array($fillpdf_info->default_nid); } diff --git a/fillpdf.rules.inc b/fillpdf.rules.inc index 9b6043c..55bb119 100644 --- a/fillpdf.rules.inc +++ b/fillpdf.rules.inc @@ -80,6 +80,24 @@ function fillpdf_rules_action_info() { ), ), ), + 'fillpdf_merge_node' => $defaults + array( + 'label' => t('Fill a PDF with Node data'), + 'base' => 'fillpdf_rules_action_merge_node', + 'description' => t('Populates the PDF with Node data and updates the + Rules variable with all information necessary to handle it.'), + 'parameter' => array( + 'fillpdf' => array( + 'type' => 'fillpdf', + 'label' => t('FillPDF metadata'), + ), + 'node_nid' => array( + 'type' => 'integer', + 'label' => t('Node ID'), + 'optional' => TRUE, + 'description' => t('If you leave this blank, the <em>Default Node ID</em> from the FillPDF configuration will be used.'), + ), + ), + ), 'fillpdf_merge_webform' => $defaults + array( 'label' => t('Fill a PDF with webform data'), 'base' => 'fillpdf_rules_action_merge_webform', @@ -198,6 +216,19 @@ function fillpdf_rules_action_merge_webform($fillpdf, $webform_nid = '', $webfor return array('fillpdf' => $fillpdf); } +/** + * Populates a loaded FillPDF configuration's PDF with node data. + */ +function fillpdf_rules_action_merge_node($fillpdf, $node_nid) { + $skip_access_check = FALSE; + $flatten = TRUE; + $node_nid = array($node_nid); + + // @todo: Parameterize $skip_access_check and $flatten in Rules. + $fillpdf = fillpdf_merge_pdf($fillpdf->info->fid, $node_nid, NULL, NULL, FALSE, $skip_access_check, $flatten, FALSE); + return array('fillpdf' => $fillpdf); +} + /** * Save the PDF to a file and return the file path. */ diff --git a/tests/FillPdfMergeTestCase.test b/tests/FillPdfMergeTestCase.test index ae4f794..9956f4b 100644 --- a/tests/FillPdfMergeTestCase.test +++ b/tests/FillPdfMergeTestCase.test @@ -104,6 +104,46 @@ class FillPdfMergeTestCase extends DrupalWebTestCase { 'PDF is populated with the title of the node.' ); + + // ******************************* + // ******************************* + // Ensure entity defaults don't overpower legacy nids in links or + // fillpdf_merge_pdf() calls. + // ******************************* + // ******************************* + $default_nid_test_node = node_load($this->createImageFieldEntity($image, 'field_test_image', 'node', 'article')); + + // Test with a node. + $this->uploadTestPdf(); + $fillpdf_form_default = fillpdf_load($this->getLatestFillPdfForm()); + $this->drupalPost("admin/structure/fillpdf/{$fillpdf_form_default->fid}", array( + 'default_nid' => $default_nid_test_node->nid, + ), t('Update')); + + // Get the field definitions for the form that was created and configure + // them. + $fid = $fillpdf_form_default->fid; + $fields = fillpdf_get_fields($fid); + $this->mapFillPdfFieldsToEntityFields('node', $fields, $fillpdf_form_default->fid); + + // Hit the FillPDF URL, check the results from the test fill method. + $this->drupalGet('fillpdf', array( + 'query' => array( + 'fid' => $fillpdf_form_default->fid, + 'nid' => $this->testNode->nid, + ), + )); + + // We don't actually care about downloading the fake PDF. We just want to + // check what happened in the backend. + $merge_result = variable_get('fillpdf_test_last_merge_metadata'); + + $this->assertEqual( + $merge_result['fields']['TextField'], + $this->testNode->title, + 'Regression test: PDF is populated with the title of the correct node, not the default.' + ); + // These tests cover the official (and some unofficial) ways of building // FillPDF Links for entities. Official: entity_id, entity_ids, entity_type // + entity_id. Unofficial: entity (synonym for entity_id), entities -- GitLab