diff --git a/fillpdf.module b/fillpdf.module index 5eaa5f55639f0d681ad3e225e44f1ac44935b108..ac3b032ad19a83ea3b363ac3602f176a5f75a94b 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 9b6043cd436573cda33ba03be3d208f65f442090..55bb119d14a88762193334234b29a11b0b8a600d 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 ae4f7941d7bb124f7099555071d5d77ecfea8a7f..9956f4b48c53631bcf04f9436a3f138399ff4aa3 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