comfortable-mexican-sofa icon indicating copy to clipboard operation
comfortable-mexican-sofa copied to clipboard

Multiple files with labels

Open fareesh opened this issue 6 years ago • 4 comments

Is it possible to have multiple files on a page, each with its own label. The examples don't show an option of giving editors the ability to attach multiple files, each with their own label, or even alt text for that matter.

For example, if I am making a list of case studies. Some will have 3 reports, each titled, some will have 2 downloadable drawings, each titled.

Currently the only way to make it work seems like creating these as child pages. Is that the recommended approach?

fareesh avatar Apr 27 '18 23:04 fareesh

Right now there's no easy way to do what you're describing.

Files are attached to Fragment model via has_many_attached :attachments. Out of the box ActiveStorage doesn't have any ways to add metadata to what you're uploading. I recall seeing a blog post that outlines how you can add that functionality, but I can't find it again. Even after you monkey-patch ActiveStorage::Attachment you'll need to have some sort of UI to add titles and alt-text for images handled by {{cms:files foo}} tag.

One way to solve your problem is to introduce a way to define collection of fragments. Here's what I'd imagine it would look like:

{{cms:collection images, render: false}}
  {{cms:file image}}
  {{cms:text title}}
  {{cms:text alt-text}} 
{{cms:end}}

Then you somehow pull images collection and using a view helper you can render html you want.

This feature is on the roadmap for version 2.1

For now you can always define tags like {{cms:file image_a}}, {{cms:text image_a_title}}, then b and c, etc. Clunky as hell but throw render: false on those and use a view helper to generate html based on fragments that are actually filled in.

GBH avatar Apr 29 '18 22:04 GBH

Just a note on "metadata" for attachments: the current "best practice" approach is to introduce another custom attachment model for your app which has exactly one active storage attachment. Your model can then hold any metadata you require. We did that in two projects where we needed metadata (sort order and permission information) and it worked quite well. :)

gr8bit avatar Aug 11 '19 22:08 gr8bit

Hey @GBH - how is that thing with collections going? Has this been implemented in any way? As i can't find anything related to collections in the source code while this feature is sadly crucial for the project i need to start to work on :)

mbajur avatar Feb 12 '20 08:02 mbajur

@mbajur unfortunately it's a non-trivial feature to add. If you have a requirement to support collections in some way, I suggest digging into Tag code and creating your own custom tag that can render form fields and save data.

GBH avatar Feb 12 '20 17:02 GBH