Jump to content

Custom Upload Names


adrian

Recommended Posts

3 minutes ago, adrian said:

Ok, I think I have narrowed it down - if you don't have "overwrite" checked and you use $file->mtime it doesn't know how to name the file at the correct point it needs to. Let me see if I can fix.

Yep, I'm getting the same results with you.

  • Like 1
Link to comment
Share on other sites

@PWaddict - I have managed to solve the problem you are seeing, but there is another issue when using $file->mtime in the rename rule for images in repeaters that I am having trouble fixing. The problem is that when you save an image, the mtime changes and there is an order problem with the mtime changing before or after the renaming of the pagefile which is causing problems. I noticed though that it all works fine if you use $file->modified instead. Would that work ok for your needs instead of mtime?

  • Like 1
Link to comment
Share on other sites

9 hours ago, adrian said:

I noticed though that it all works fine if you use $file->modified instead. Would that work ok for your needs instead of mtime?

Thanks a lot. I switched from mtime to modified and the problem is gone :) Maybe you should add a note on the Rename rules to avoid using mtime.

Link to comment
Share on other sites

1 minute ago, PWaddict said:

Thanks a lot. I switched from mtime to modified and the problem is gone 🙂 Maybe you should add a note on the Rename rules to avoid using mtime.

Maybe I could programmatically replace mtime with modified. Can you think of any reason why mtime would actually be needed vs modified?

Link to comment
Share on other sites

3 minutes ago, adrian said:

Maybe I could programmatically replace mtime with modified. Can you think of any reason why mtime would actually be needed vs modified?

modified: Unix timestamp of when Pagefile (file, description or tags) was last modified.
mtime: Unix timestamp of when file (only) was last modified.

Taken from Pagefile API Reference.

Link to comment
Share on other sites

  • 5 months later...

I'm trying to use this module, but currently no renaming is happening, either on upload or after save.

My rule is : {$page->compGrade->title}_{$page->compSubject->title}_{$page->compMedium->title}_{$page->title}

$page->compSubject and $page->compMedium are Options fieldtype.

I get for example: imgp0887_upload_tmp.jpg which is definitely not right.

I'm on PW 3.0.144

There's not by any chance an issue with the module not handling mixed case field names? Processwire itself does allow mixed case field names, however I've struck several modules including from Ryan himself, that convert everything to lowercase, to match database table names.

That's the only thing I can think of that might be causing renaming not to work.

Here's screenshot of configuration below:

 

image.thumb.png.34047fdfaca68c058b87b018363a00dc.png

Link to comment
Share on other sites

Hey @Kiwi Chris - the "_upload_tmp" suffix is added when the value for one of the fields is not available yet. What should happen (and it working for me here) is that if there is no value for one of those fields listed in the format rule, after initial ajax loading of the image, it will have that "_upload_tmp" suffix, and then when you save the page, assuming you have selected something for those required format fields, the file will be renamed once again, now that it has the data it needs. 

BTW, I just tested with an Options fieldtype with a mixed case name, so there should be no problem there.

I am sure you've thought of this, but be aware that Options and Page Reference fields set to store multiple values will automatically use the first selected value.

Not sure where to go from here, but maybe you could start with a simple {$page->title} rule and if that works, add another field and see which one breaks it and we can investigate from there.

Link to comment
Share on other sites

1 minute ago, Kiwi Chris said:

I didn't realise it would drop the whole filename pattern rather than just the incorrect part

Yeah, it's the only way to handle it really because that upload_tmp suffix is needed to trigger the later rename. Anyway, glad you got it sorted.

Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...

Hi there. I am pretty new to processwire. Been using the module and it works perfectly!

I have one question. Sometimes when images are uploaded, from the gui, the module adds a "-1" is there a get around for this?

I basically am trying to keep the name of the file same for each different upload.

 

Thank you!

 

image.png

Edited by helin
Link to comment
Share on other sites

Hi @helin,

Sorry for the late response. This has annoyed me for a long time and while it might seem like an easy fix, it has eluded me several times. I have put in another concerted effort and I think I have a working solution, but it would be great if you could test before I commit to the repo. Please try the attached file and let me know. It should work regardless of whether you have the "Rename on Save" option checked, but please note that if you upload a replacement file for a single image field (max allowed 1 file), then the -1 will be appended until you save the page - this is simply because the other image still exists until you save the page, so the filename must be adjusted, but it should now be removed if there are no other conflicting filenames.

 

ProcessCustomUploadNames.module.php

Link to comment
Share on other sites

  • 1 month later...

Hey @adrian, it seems it's impossible to delete a rule. When I click the trash icon the rule disappears but I'm getting the following error on console and when I press the submit button the rule appears again.

JqueryCore.js?v=183:2 Uncaught RangeError: Maximum call stack size exceeded
    at String.replace (<anonymous>)
    at nt (JqueryCore.js?v=183:2)
    at Function.nt.matches (JqueryCore.js?v=183:2)
    at Function.filter (JqueryCore.js?v=183:2)
    at init.v.fn.<computed> [as children] (JqueryCore.js?v=183:2)
    at Object.toggle (inputfields.js?v=31e:178)
    at Object.focus (inputfields.js?v=31e:400)
    at inputfields.js?v=31e:401
    at completed (inputfields.js?v=31e:191)
    at Object.toggle (inputfields.js?v=31e:246)

 

Link to comment
Share on other sites

@PWaddict - I am not having any problems deleting a rule. Can you please investigate the error further - is it specific to a particular browser? Are you running the regular Uikit theme? Can you try debugging the deleteRow function in the module's js file to see if you can prevent the error?

Link to comment
Share on other sites

35 minutes ago, adrian said:

@PWaddict - I am not having any problems deleting a rule. Can you please investigate the error further - is it specific to a particular browser? Are you running the regular Uikit theme? Can you try debugging the deleteRow function in the module's js file to see if you can prevent the error?

ProcessWire 3.0.165 (with default Uikit admin theme)
Google Chrome 87.0.4280.66 (Official Build) (64-bit)

I did more tests and I can only delete the last rule. So, if I want to delete the 1st or the 3rd etc, first I have set it as the last one, press the submit button to save the order list and then I can delete it.

Also tested on Firefox 83.0 (64-bit) and I'm getting the following console error if I try to delete any rule except the last one:

Uncaught TypeError: $(...).val() is undefined
    <anonymous> https://mysite.test/site/modules/ProcessCustomUploadNames/ProcessCustomUploadNames.js?v=1.2.9:68
    jQuery 2
    <anonymous> https://mysite.test/site/modules/ProcessCustomUploadNames/ProcessCustomUploadNames.js?v=1.2.9:59
    jQuery 9
    <anonymous> https://mysite.test/site/modules/ProcessCustomUploadNames/ProcessCustomUploadNames.js?v=1.2.9:54
    jQuery 11

 

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Additional information: I've a image field called "image" and a page fieldset called "meta". The image is mapped on the template and also mapped on meta, which mapped to the template too. So the image field "image" is two times mapped.

$page->image
$page->meta->image

For $page->image I want to rename the image to "{$page->title}" and for $page->meta->image I want to rename the image to "preview".

How do I configure the rules to match both cases?

Thanks.

Link to comment
Share on other sites

@rjgamer - a few things:

1) Not sure if it will break anything, but I'd steer clear of naming a field "meta" because PW has an inbuilt meta field: https://processwire.com/api/ref/page/meta/

2) When selecting enabled pages, I am not sure it's going to work to select the actual repeater page parent (although I'd need to check to be sure). Does it work if you select the parent of the tree of pages that has the repeater field in their template?

3) It should be fine to have 2 separate rules for each of the fields - have you tried adding an extra rule to handle this?

Link to comment
Share on other sites

13 minutes ago, adrian said:

1) Not sure if it will break anything, but I'd steer clear of naming a field "meta" because PW has an inbuilt meta field: https://processwire.com/api/ref/page/meta/

Doesn't seem to make any problems yet. But you are right, I will rename it for "safety first" reasons.

13 minutes ago, adrian said:

2) When selecting enabled pages, I am not sure it's going to work to select the actual repeater page parent (although I'd need to check to be sure). Does it work if you select the parent of the tree of pages that has the repeater field in their template?

No, it doesn't work if I select "Repeaters" as parent page and it does not work if I explicit select the template or page.

13 minutes ago, adrian said:

3) It should be fine to have 2 separate rules for each of the fields - have you tried adding an extra rule to handle this?

Doesn't work, because both times the same page and field got matched.

The module can currently not set rules for page repeater and different handle the fields with similar names (connected over page repeater).

 

Link to comment
Share on other sites

@rjgamer - just did some testing and I remember now that it's not designed to handle repeater fields as separate pages like you are trying to do. I assumed that rules should be applied to all images on a page, whether they are within a repeater field or as a regular image field. It would have actually been much easier to set up the other way 🙂

I am sure the module could be modified to achieve what you're looking for, but not sure I can do it in a timely fashion for you. I will take a look when I get a chance though. Remember that PRs are always welcome!

Link to comment
Share on other sites

On 3/22/2021 at 3:57 PM, adrian said:

@rjgamer - just did some testing and I remember now that it's not designed to handle repeater fields as separate pages like you are trying to do. I assumed that rules should be applied to all images on a page, whether they are within a repeater field or as a regular image field. It would have actually been much easier to set up the other way 🙂

I am sure the module could be modified to achieve what you're looking for, but not sure I can do it in a timely fashion for you. I will take a look when I get a chance though. Remember that PRs are always welcome!

Thanks. My module development skills are to low for any professional pull request. I would be really happy if you could implement support specific page repeater rules.

On 3/22/2021 at 4:22 PM, adrian said:

@rjgamer - you could of course just use a different image field in the repeater so the rules can target it separately.

Yeah, thats the current and only way to get it done. But who wants 3-5 similar fields with different names? 😞

Link to comment
Share on other sites

  • 1 month later...

This module works well, but with Lister Pro and inline editing within the Lister Pro table, even with the check box to update on save for this module, file names aren't renamed, although opening via a full edit and saving does work.

I suspect the issue might be the way Lister Pro works,  but if it's possible to get this module to work with inline editing mode in Lister Pro it would be handy.

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
×
×
  • Create New...