Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename uploaded files using webform filename pattern (and tokens) #1009

Merged
merged 3 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just hit an error here with a vanilla install and I think this is the problem. It assumes we set the new Filename above. But I'm getting a warning "Undefined variable $newFilename " which suggests we didn't.

// This field can't be emptied due to the nature of file uploads
continue;
}
Expand Down
Loading