Jump to content

Files Rename Replace


Robin S
 Share

Recommended Posts

An Images field allows you to:

  • Rename images by clicking the filename in the edit panel or in list view.
  • Replace images, keeping metadata and filename (when possible) by dropping a new image on the thumbnail in the edit panel. Introduced here.

But neither of these things is possible in File fields, which prompted this module. The way that files are renamed or replaced in this module is not as slick as in the Images field but it gets the job done. The most time-consuming part was dealing with the UI differences of the core admin themes. @tpr, gives me even more respect for the work that must go into AdminOnSteroids.

Most of the code to support the rename/replace features is already present in InputfieldFile - there is just no UI for it currently. So hopefully that means these features will be offered in the core soon and this module can become obsolete.

 

Files Rename Replace

Allows files to be renamed or replaced in Page Edit.

rename-replace

Usage

Install the Files Rename Replace module.

If you want to limit the module to certain roles only, select the roles in the module config. If no roles are selected then any role may rename/replace files.

In Page Edit, click "Rename/Replace" for a file...

Rename

Use the text input to edit the existing name (excluding file extension).

Replace

Use the "Replace with" select to choose a replacement file from the same field. On page save the file will be replaced with the file you selected. Metadata (description, tags) will be retained, and the filename also if the file extensions are the same.

Tip: newly uploaded files will appear in the "Replace with" select after the page has been saved.

 

https://github.com/Toutouwai/FilesRenameReplace
http://modules.processwire.com/modules/files-rename-replace/

Edited by Robin S
Added modules directory link.
  • Like 14
  • Thanks 3
Link to comment
Share on other sites

I have only recently started to work on something similar, only streamlined for frontend editing, so kudos for tackling that topic  :) I'm likely going to peek inside the code and steal a few pieces.

The only nitpick I have is that the behavior of the replacement option might not be clear to end users. The field should probably be a little more verbose and tell the user that description and/or tags are replaced by those of the selected file, or they might assume it's the other way round. Perhaps a "Replace with" option would be even more intuitive (use the selected file and keep the rest as is). It's how I'd write it as a user story - "I have a file entry on the page with a description and tags and just want to replace the old file with a new one in that entry". Just playing devil's advocate here, as I'm thinking of my 50+ intranet editors who only update their pages a few times every year.

  • Like 1
Link to comment
Share on other sites

1 hour ago, BitPoet said:

Perhaps a "Replace with" option would be even more intuitive (use the selected file and keep the rest as is).

I hear what you're saying, but the idea for this module is to bring the behaviour of the core Images field to the Files field (albeit in a less ambitious way). Ideally the Files field would have a dropzone similar to the edit panel thumbnail of the Images field, and a file uploaded via that dropzone would immediately replace the edited file. But tackling the Javascript that would be involved feels like too much work, hence the less elegant approach taken in this module. Pull requests from JS wizards would be welcome. :)

The way I'm imagining this module being used is you have a file with some metadata, and perhaps that file is linked to in a CKEditor field. You want to replace that file with another file (not yet uploaded) without having to manually copy the metadata or recreate the link in CKEditor. The new file is uploaded to the field and then immediately afterwards the target file is replaced. So it's like a poor man's version of the image replace feature, using the same underlying core code in InputfieldFile, in two steps instead of one.

I hadn't imagined the replacement file being one that has already existed in the field for a while and has metadata associated with it (my screencast demo wasn't that well thought out - I'll redo it). But I think it should be quite easy to support what you've suggested, via a checkbox next to the select for "Replace metadata also". Or some other wording you think would be clearer?

Link to comment
Share on other sites

1 hour ago, BitPoet said:

Perhaps a "Replace with" option would be even more intuitive (use the selected file and keep the rest as is).

I think I misunderstood you before. You're suggesting the dropdown would be "Replace with" and you would use the dropdown in the target file rather than the source file, and select a source file to be a replacement. I agree that is more intuitive, but the replace code in InputfieldFile doesn't work that way - each file can specify another file that it will replace, but not a file that will be it's own replacement. But I could go away from letting InputfieldFile handle the replacement and do it all within the module (I'm already having to handle the metadata and sort position).

Will have a think about it and may well change to that. So to any early adopters... be advised that the workflow may change. :)

Link to comment
Share on other sites

That's a great solution!  Thanks Robin for providing this module.  I just installed it and noticed a minor issue:  See how the "R" in Rename and Replace gets cutoff?

replace.jpg.1f71aa3f419ef636c9abf97cfea7d8b4.jpg

Do you plan on adding it to the Modules directory?  It would make it easier to upgrade.

It would also be cool if you made a similar module to handle image fields the same way.  Maybe with an option to disable core's "click the filename to rename" feature and the drag and drop on thumbnail to swap the image?  That way file and image fields are consistently the same and the "Rename / Replace" feature would be controlled by a permission both for image and file fields.  I doubt many people know of those features any way?  Drag and drop an image on a thumbnail could be considered a destructive action that may not be the intended behavior of the user?

An easy way to disable the rename of the filename feature could be to remove the contenteditable attribute?

I'm in hurry right now, but I just wanted to share the few thoughts I had.

Thanks again

Link to comment
Share on other sites

@gmclelland, thanks for the feedback.

2 hours ago, gmclelland said:

It would also be cool if you made a similar module to handle image fields the same way.  Maybe with an option to disable core's "click the filename to rename" feature and the drag and drop on thumbnail to swap the image?

I don't want to reinvent the wheel for functionality that's already in the core. I'd actually prefer this module to be more like the Images field behaviour rather than the other way around. I would have done the file renaming the same way as the Images field but the filename is used as a link in the Files field. I will probably work on refining this module to be closer to the renaming/replacing behaviour of the Images field when I have time.

I also think Ryan does intend to bring renaming and replacing to the Files field, as the underlying code is in InputfieldFile and labelled with "currently only used by InputfieldImage".

2 hours ago, gmclelland said:

would be controlled by a permission both for image and file fields

We've talked previously about how to limit image renaming by a permission:

I don't think there's an easy way to prevent image replacement, but it doesn't really make sense to anyway because if a user has access to an Images inputfield then they can already upload and delete images which is no less destructive than replacing them.

2 hours ago, gmclelland said:

I just installed it and noticed a minor issue:  See how the "R" in Rename and Replace gets cutoff?

I can't see that here so hard for me to fix - it might be OS dependent. When you have a chance, could you have a play around with the CSS and see if you can find a solution? Hopefully without changing the position of the elements. Maybe take a look at line-height, overflow or z-index? Thanks.

2 hours ago, gmclelland said:

Do you plan on adding it to the Modules directory?

Yes, it is awaiting approval for the directory.

  • Like 1
Link to comment
Share on other sites

3 hours ago, Robin S said:

I don't want to reinvent the wheel for functionality that's already in the core. I'd actually prefer this module to be more like the Images field behaviour rather than the other way around. I would have done the file renaming the same way as the Images field but the filename is used as a link in the Files field. I will probably work on refining this module to be closer to the renaming/replacing behaviour of the Images field when I have time.

The problem with the core implementation is that isn't immediately discoverable that you can click the image's filename and rename it whereas your solution make's it obvious with a label "Rename / Replace."  Most content editors aren't very technical savvy.

Quote

We've talked previously about how to limit image renaming by a permission:

Ah..Forgot about that one.  Thanks

Quote

I can't see that here so hard for me to fix - it might be OS dependent. When you have a chance, could you have a play around with the CSS and see if you can find a solution? Hopefully without changing the position of the elements. Maybe take a look at line-height, overflow or z-index? Thanks.

I tried to take a look at tonight, but it looks a little more involved to get it just right across the different themes.  I think the cutoff text is due to the negative margins.  I'll investigate some more the next chance I get.  I'm running Chrome on a mac with the latest version of Processwire and AOS.

Link to comment
Share on other sites

Strangely, I tried updating the module with the ProcessWireUpgrade module, but it gave me an error "Session: Invalid download URL specified."  It might have been because I originally downloaded the module from github.

The text looks good now, it's no longer cut off.  Thanks Robin!

  • Like 1
Link to comment
Share on other sites

8 hours ago, gmclelland said:

Strangely, I tried updating the module with the ProcessWireUpgrade module, but it gave me an error "Session: Invalid download URL specified."

That was an error in the info at the modules directory, caused by mismatched browser autofill. Catches me out on a semi-regular basis. Would be nice if the modules directory used autocomplete attributes on the fields to give browsers some guidance for autofill.

Link to comment
Share on other sites

  • 5 years later...

@Robin S When a file is replaced, I need to capture the date of the replacement and put in a page field.

I'm not anywhere near your skill level, but starting to get comfortable with the basics all thanks to many of your recent posts and replies.

Could you offer some guidance on how I might pursue this either with your module's code or some sort of hook arrangement? Thanks again!

Link to comment
Share on other sites

@Jim Bailie, if you update the module to v0.2.3 there is a hookable method that fires when a file is about to be replaced.

$wire->addHookAfter('FilesRenameReplace::replaceReady', function(HookEvent $event) {
	/** @var InputfieldFile $inputfield */
	$inputfield = $event->arguments(0);
	/** @var Pagefile $pagefile */
	$pagefile = $event->arguments(1);
	/** @var Pagefile $replacement */
	$replacement = $event->arguments(2);
	
	// Do something here...
});

I don't think you could do anything useful to the Pagefile objects at this point given that the original file is about to be deleted and the replacement file is about to have all its properties overwritten from the original file, but you could set some other field value on the page like this:

$wire->addHookAfter('FilesRenameReplace::replaceReady', function(HookEvent $event) {
	/** @var InputfieldFile $inputfield */
	$inputfield = $event->arguments(0);
	/** @var Pagefile $pagefile */
	$pagefile = $event->arguments(1);
	/** @var Pagefile $replacement */
	$replacement = $event->arguments(2);
	
	// Set a date field on the page
	$page = $inputfield->hasPage;
	if($page && $page->id) {
		$page->setAndSave('my_date_field', time());
	}
});

 

  • 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

×
×
  • Create New...