Renaming uploaded file trought admin panel

Hi!!

I join the @brigaill request. I was looking for something simpler: rename the uploaded image with a unique name, even if that meant overwriting the file.

Looking for an answer I have seen some threads with no answer, except for some exceptions:

  • Here @hughbris proposes something with the save: filename: property, but not being documented I don’t know how to implement that.

  • Here @pamtbaau answers with the form file field, but according to the docu this field does’nt work for editing pages in the admin (a useful case), only plugins, themes and custom settings.

My intention (for ex.) was to allow the client to change the background image of a module through the admin panel, being that background image implemented through CSS (in the styles file, not in an html style attribute). I can’t think of how to accomplish that any other way (any ideas?), but renaming the file would make it very simple.

Thanks for the help!

1 Like

Hi @sdesergi,

I think the examples were related to front end forms, and this will not work in the admin panel.

I would choose this approach: Use the filepicker field in the module’s blueprint. Then your client simply add the image via the media order and pick the file using the filepicker.

This needs to be added t your blueprint:

header.bgImage:
  ordering@: header.quoteTitle
  type: filepicker
  size: large
  label: Background Image
  help: This image will be used as background.

Then within the twig template you can make the image available via an inline style like this:

<div style="background-image: url('{{ page.media[page.header.bgImage].url }}');">
</div>

You can check out a working example via GitHub - npetri13/grav-tinker: A test and tinker theme for the flat file cms grav.org.

Best regards.