Skip to content

Commit

Permalink
replace filename with tokens if set in File field's settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sluc23 committed Sep 16, 2024
2 parents 97ec425 + 49f09bd commit 180443a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/Plugin/WebformHandler/CivicrmWebformHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 7 additions & 2 deletions src/WebformCivicrmBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,13 +779,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),
Expand Down
15 changes: 12 additions & 3 deletions src/WebformCivicrmPostProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class WebformCivicrmPostProcess extends WebformCivicrmBase implements WebformCiv
*/
private $database;

/**
* @var \Drupal\webform_civicrm\Plugin\WebformHandler
*/
private $handler;

/**
* @var \Drupal\webform\WebformSubmissionInterface
*/
Expand Down Expand Up @@ -85,10 +90,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();
Expand Down Expand Up @@ -2523,7 +2528,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;
}
Expand Down

0 comments on commit 180443a

Please sign in to comment.