Robin S Posted November 21, 2023 Share Posted November 21, 2023 Following on from an earlier tutorial for using $pagefile->uploadName... Tip 1 Suppose your website audience uses a non-latin alphabet and you have a file to upload with a filename like this: (I don't know any Chinese but Google Translate tells me that this is a translation for "my example filename") If this file is uploaded to a PW files field it ends up as shown below because none of the filename characters are allowed. This makes it hard to identify what the file relates to. Since PW 3.0.212 the original filename will be saved to the uploadName property of the Pagefile but it would be nice to improve the actual filename assigned to the file. The hooks below in /site/ready.php make use of the SanitizerEasySlugger module to transliterate the original filename to latin characters that are allowed in a PW files field. // When a file is added to InputfieldFile $wire->addHookAfter('InputfieldFile::fileAdded', function(HookEvent $event) { /** @var Pagefile $pagefile */ $pagefile = $event->arguments(0); // The uploadName property is not available at this point so just flag that the file is new $pagefile->isNewFile = true; }); // When InputfieldFile is processed in Page Edit $wire->addHookAfter('InputfieldFile::processInputFile', function(HookEvent $event) { /** @var Pagefile $pagefile */ $pagefile = $event->arguments(1); // Only for newly added files if(!$pagefile->isNewFile) return; // Only if the uploadName property is different to the basename if($pagefile->uploadName === $pagefile->basename) return; // Divide the upload name into parts $parts = pathinfo($pagefile->uploadName); // Transliterate non-latin characters in the filename using the SanitizerEasySlugger module // https://processwire.com/modules/sanitizer-easy-slugger/ $transliterated = $event->wire()->sanitizer->utf8Slugger($parts['filename']); // Rename the file (renamed file will be visible after the page is saved) $pagefile->rename($transliterated . '.' . $pagefile->ext); }); After saving the page the filename now at least has some connection to the original filename, and this is presumably more useful to a Chinese speaker: Tip 2 When using CKEditor or TinyMCE to link to a file, by default you can only see the sanitized name: But it would be helpful to also see the original filename that has been saved to $pagefile->uploadName. Add the hook below to /site/ready.php: // When the Add Link form is created $wire->addHookAfter('ProcessPageEditLink::buildForm', function(HookEvent $event) { /** @var InputfieldForm $form */ $form = $event->return; // Get the "Select File" select /** @var InputfieldSelect $select */ $select = $form->getChildByName('link_page_file'); // Get an instance of PagefilesManager via the Home page $fm = $event->wire()->pages->get('/')->filesManager(); // For each of the options in the select foreach($select->options as $filename => $label) { if(!$filename) continue; $pagefile = $fm->getFile($filename); if(!$pagefile) continue; // Only if the uploadName is different from the filename if($pagefile->uploadName === $pagefile->basename) continue; // Add the uploadName to the option label $select->replaceOption($filename, $filename, $label . " ($pagefile->uploadName)"); } }); Now the uploadName is appended to the select option label: 1 1 Link to comment Share on other sites More sharing options...
Gideon So Posted November 21, 2023 Share Posted November 21, 2023 @Robin S Wow!! What can I say. How great is this community and how helpful everyone here is. Thanks Robin. I can wait longer until the final solution comes. Gideon 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now