Jump to content

FieldtypeSelectFile & InputfieldSelectFile


Recommended Posts

FieldtypeSelectFile & InputfieldSelectFile

Inputfield Select File is an Inputfield & Fieldtype to select a single file or folder and stores the name and / or use the selected file as page template. The last option enables the editor to use multiple views for a page, depending on the selected template.

Settings

  • The folder containing the files and/or folders.
    • A relative path relative to the /site/templates/ folder.
  • Hide file extensions
  • Hide files
  • Hide folders
  • Natural Sort (Select options)
    • Sort files and folders in natural ordering (PHP >= 5.4.0)
  • Change Page Template

    • Just before the Page::loaded event the selected file is set as template file for the page. This setting can only be applied once per a page and folders are exluded from the select inputfield.

      Note that a page with no associated template file will render with the selected file.

When to use ?

Let editors select a file and base your own logic upon this. With the change page template setting you're able to use the selected file as template file. This could reduce the amount of normal templates needed and let editors choose how the page get rendered. There are plenty of use cases for this Inputfield.

In the examples I call the field selected_file.

// let the editor choose a CSS file
$config->styles->append($config->urls->templates . "styles/" . $page->selected_file);

/**
 * advanced usage example
 *
 * You need multiple ways to render your markup. Let the site editor choose which
 * file the page need to render.
 *
 */

$tpl = new TemplateFile($config->paths->templates . "includes/" . $page->selected_file);
$tpl->set('current', $page);
$markup = $tpl->render();

(It could be a real good companion with InputfieldSelector)

Download at GitHub

Modules directory

Edited by Martijn Geerts
  • Like 19
Link to comment
Share on other sites

  • 3 weeks later...

this is great...! i was trying to write this module about 2 months ago and couldn't figure out how to do it..

edit: brilliant, works perfectly, and even works using ../ to get out of the templates folder... this is going to be extremely useful for html5 video, selecting files to be used instead of uploading to processwire page.... and in other situations where we want to store large media assets somewhere else in the filesystem..

many thanks for this.

  • Like 4
Link to comment
Share on other sites

Tnx for the followup !

Gonna test this weekend. 


I didn't thought people would use this fieldtype with ../ in the field settings, so I cleaned the values when I discovered this behaviour.

Normal users can't set those settings so I removed this checking and clearing of the data in the update. (can't think of doing real harm)

Thanks for finding this Macrura. The module is updated on Github.

Edited by Martijn Geerts
  • Like 1
Link to comment
Share on other sites

Sorry to hijack this thread, but if you are working with HTML5 video, have you seen my draft video fieldtype module:

https://processwire.com/talk/topic/4580-video-fieldtype/

@Adrian - looks really good! In the use case i have, my video page has options for youtube, vimeo or local video; with the local video, some of them were very large sizes so i didn't feel like bothering with the uploader; for the html5 video, i have mp4 and webm versions of both, plus the poster image...so i have a media folder in /site/ for all of those things.. having the ability to let the user select the video file makes this whole thing possible.. but i will look into your module for sure on future stuff and i'm redoing a site now and will probably upgrade my system to use your module... will comment on that thread soon!

  • Like 1
Link to comment
Share on other sites

@martijn, thanks for that update! this works for sure now with regular pages, only issue is that i can't seem to get it to work with repeaters, i looked at the code but nothing popped out at me as to why this would be the case..

Link to comment
Share on other sites

@Martijn, thanks, i'm not sure what the problem is, just can't get values to save in these repeaters i have;

i will keep testing/troubleshooting this; i tried changing the path, checking/unchecking the hide file extension, and i checked the database.. they just don't save..

thanks for you help.. nothing urgent...

Link to comment
Share on other sites

You may want to also check other modules that you may have installed recently. I had an issue where repeaters wouldn't save and it turned out to be an issue with a recently installed module.  I only figured it out by retracing my steps and uninstalling modules.

  • Like 1
Link to comment
Share on other sites

ok, figured it out, sorry for posting an error that wasn't really this module... it was something to do with the conditional visibility of fields that was breaking my repeater...

thanks again.. this is all sorted!

  • Like 2
Link to comment
Share on other sites

  • 8 months later...

@Martijn,

I was researching how to get this module to return an alphabetically sorted file list;

found some stuff on stack exchange and php about reading the files into an array and sorting...

$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}
sort($files);

just wondering if you might be up to being 'sponsored' to add that feature... can send beer $

Link to comment
Share on other sites

@Macrura:

I like your suggestion so updated the module with an option to use SORT_NATURAL on the options of the select. 

// Natural Sort (NOT checked)
image_1.jpg
image_12.jpg
image_21.jpg
image_4.jpg
 
// Natural Sort (checked)
image_1.jpg
image_4.jpg
image_12.jpg
image_21.jpg
 
 
  • Like 5
Link to comment
Share on other sites

Just pushed a (major) update which adds a new feature: Change Page Template.

Change Page Template:

Just before the Page::loaded event the selected file is set as template file for the page. This enables you to have multiple file templates for the same page, selectable by the editor. This setting can only be applied once per a page and folders are exluded from the select inputfield. Note that a page with no associated template file will render fine with a selected file. :-)

+ Changed the sorting from SORT_NATURAL flag to the natcasesort function because the flag requires PHP 5.4.0 or higher.

First post is modified & please report issues.

  • Like 2
Link to comment
Share on other sites

  • 3 months later...
  • 3 months later...

I am currently using this field for the first time and I love it! Really a useful little time saver!

One minor issue I found: I configured the folder path without a trailing slash. When editing a template i could see the files in the folder, but the field never saved the selected value. After adding a trailing slash to the foldername everything worked as expected. I did not see any warnings or errors on screen or in my logs.

Another thing I still could not figure out is, how to use this field with showIf field dependencies. Is this even possible? I would like to show some other fields only if a certain file is selected. 

Link to comment
Share on other sites

Could you change the line in the sanitizeValue method in The fieldtype to:

$file = $this->config->paths->templates . trim(trim($field->folderPath, '/')) . '/' . $value;

Then the first 'issue' should be fixed. (although it was mentioned in the notes below the setting)

For the second issue, I have no time to invest now... will look at it this weekend. (Could be related to the first.)

Link to comment
Share on other sites

Thanks Martijn! After changing the sanitizeValue method the trailing slash is no longer needed. 

Field dependencies still don't work, but I did not expect it either :) Thanks for looking into it, but in my current project I can totally live without it. It's just a "nice to have", and I wanted to give you some feedback.

  • Like 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...