From 49f09bd2e760f79d52120c0d0780f52c012f770c Mon Sep 17 00:00:00 2001 From: Luciano Spiegel Date: Mon, 16 Sep 2024 19:14:40 +0200 Subject: [PATCH] replace filename with tokens if set in File field's settings --- .../WebformHandler/CivicrmWebformHandler.php | 8 ++++++++ src/WebformCivicrmBase.php | 9 +++++++-- src/WebformCivicrmPostProcess.php | 15 ++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Plugin/WebformHandler/CivicrmWebformHandler.php b/src/Plugin/WebformHandler/CivicrmWebformHandler.php index 5c3a07911..556ba99ff 100644 --- a/src/Plugin/WebformHandler/CivicrmWebformHandler.php +++ b/src/Plugin/WebformHandler/CivicrmWebformHandler.php @@ -31,12 +31,20 @@ class CivicrmWebformHandler extends WebformHandlerBase { */ protected $civicrm; + /** + * The Token Manager service. + * + * @var \Drupal\webform\WebformTokenManager + */ + public $tokenManager; + /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition); $instance->civicrm = $container->get('civicrm'); + $instance->tokenManager = $container->get('webform.token_manager'); return $instance; } diff --git a/src/WebformCivicrmBase.php b/src/WebformCivicrmBase.php index cc4059cc7..437071d01 100644 --- a/src/WebformCivicrmBase.php +++ b/src/WebformCivicrmBase.php @@ -776,13 +776,18 @@ function addPaymentJs() { * Copies a drupal file into the Civi file system * * @param int $id: drupal file id + * @param string $filename drupal filename * @return int|null Civi file id */ - public static function saveDrupalFileToCivi($id) { + public static function saveDrupalFileToCivi($id, $filename = NULL) { $file = File::load($id); if ($file) { $config = \CRM_Core_Config::singleton(); - $path = \Drupal::service('file_system')->copy($file->getFileUri(), $config->customFileUploadDir); + $copyTo = $config->customFileUploadDir; + if(isset($filename)) { + $copyTo .= '/' . $filename; + } + $path = \Drupal::service('file_system')->copy($file->getFileUri(), $copyTo); if ($path) { $result = \Drupal::service('webform_civicrm.utils')->wf_civicrm_api('file', 'create', [ 'uri' => str_replace($config->customFileUploadDir, '', $path), diff --git a/src/WebformCivicrmPostProcess.php b/src/WebformCivicrmPostProcess.php index 00707d895..7ef1bda12 100644 --- a/src/WebformCivicrmPostProcess.php +++ b/src/WebformCivicrmPostProcess.php @@ -47,6 +47,11 @@ class WebformCivicrmPostProcess extends WebformCivicrmBase implements WebformCiv */ private $database; + /** + * @var \Drupal\webform_civicrm\Plugin\WebformHandler + */ + private $handler; + /** * @var \Drupal\webform\WebformSubmissionInterface */ @@ -79,10 +84,10 @@ function initialize(WebformSubmissionInterface $webform_submission) { $handler_collection = $this->node->getHandlers('webform_civicrm'); $instance_ids = $handler_collection->getInstanceIds(); - $handler = $handler_collection->get(reset($instance_ids)); + $this->handler = $handler_collection->get(reset($instance_ids)); $this->database = \Drupal::database(); - $this->settings = $handler->getConfiguration()['settings']; + $this->settings = $this->handler->getConfiguration()['settings']; $this->data = $this->settings['data']; $this->enabled = $this->utils->wf_crm_enabled_fields($this->node); $this->all_fields = $this->utils->wf_crm_get_fields(); @@ -2549,7 +2554,11 @@ private function fillDataFromSubmission() { } } elseif ($dataType == 'File') { - if (empty($val[0]) || !($val = $this->saveDrupalFileToCivi($val[0]))) { + // Replace filename (with tokens) if set. + if (isset($component['#file_name']) && $component['#file_name']) { + $newFilename = $this->handler->tokenManager->replace($component['#file_name'], $this->submission); + } + if (empty($val[0]) || !($val = $this->saveDrupalFileToCivi($val[0], $newFilename))) { // This field can't be emptied due to the nature of file uploads continue; }