Jump to content

More tips for $pagefile->uploadName


Robin S
 Share

Recommended Posts

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:

image.png.145de57844b2a9787377bb0f4308bbf2.png

(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.

image.png.d1f741958e5119aa3c3dd53d68ae21d9.png

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:

image.png.9fca0f6789a2b06289c3f4285183e8f6.png

 

Tip 2

When using CKEditor or TinyMCE to link to a file, by default you can only see the sanitized name:

image.png.55b4152f177fbd498152614c8a47b501.png

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:

image.png.70e048c7a48cc2c06e58d009045864ae.png

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...