Need a Folder TV

I am in need of a folder-TV. Which is a TV to select a folder.

Default TV selection has no means for that demand and all I could find is a 12 year old post about this: TV input-type selecting a folder | MODX Community Forums

Idea behind this is an option to let the customer upload all files i.e.pdfs (of unpredictable count) into one folder and the select the ‘downloads’ folder in the resource. I as developer provide a snippet that lists all appropriate files of that folder via download-link. or image gallery or sound-widgets…

Is it possible that in all those years nobody else deeded something like this? It would be a very reasonable part of the default tv-types.

I am currently working with modx 3.0.3 on latest debian.

Is there perhaps anybody who could provide a 1-2-3 description on how to - or is it more delicate?

~d

can’t you just use a @SNIPPET or @CHUNK binding to fill the options?

1 Like

Hey @domindian

It may depend on how many folders / levels you need to list.

It may be the case that you really need full, interactive access to a collapsible tree of folders to allow a selection to be made. In that case - I’m not sure how best to advise.

But if you only need to list the children of a parent folder - what @bruno17 suggested should be possible - I did something similar recently like this:

In your TV Input Options:

image

Then create your snippet called list-options:

<?php

$output = "";
$dirPath = "/path/to/parent/";                      // PATH TO YOUR PARENT FOLDER
$dirs = glob($dirPath . '/*' , GLOB_ONLYDIR);       // GET FOLDERS

foreach ($dirs as $idx => $dir) {                   // LOOP THROUGH FOLDERS
    $dirTrim = str_replace($dirPath, "", $dir);     // GET FOLDER NAME FROM FULL PATH
    if ($idx != 0) {$output .= "||";}               // ADD DELIMITER || TO OUTPUT
    $output .=  "$dirTrim==$dir";                   // ADD FOLDER TO OUTPUT        
}

echo $output;  

This should offer a list of the folders in your parent, like this:

image

I guess you could also do this recursively and get sub folders too - but I think it might soon get messy.

1 Like

Thank you very much, bruno, for pointing that out!

Since I don’t need TV-bindings too often I was under the false impression, that together with the removed @EVAL binding @snippet was abandoned as well with version 3. Had been prudent to try first.

I mixed up the memory about a post I read in the past that discussed some thoughts about security vectors.

This for sure works for many usecases perfectly and mixes great with a listbox TV!

1 Like

Also many thanks to you, dejaya!
The code is neat and working. And at least a useful start for everyone with the same need.
I will write one for my needs.

Your mentioning of the issue of interactivity in this matter however puts me right to my point of confusion here:

One might wonder that there is a file-tv and image-tv since - ever, and it ueses the (respectable if not wonderful) modx-file-browser … but that very browser is not as well available “just” to select a directory.
I imagine there could be a ton of reasons why this might be a too easy thought.

Anyway, thank you both for your help!
~d

1 Like

Glad you’re up and running!