Jump to content

define upload destination paths for fields & templates


virgomania
 Share

Recommended Posts

Hi,

I am new to processwire and was wondering, if one can define custom upload paths for File/ImageUploadFields based on fieldname and/or templatename - and how to do this. I know that the template provides the path to the assets folder (but based on its page id) - if I like to present the editor with a list of all uploaded files or images (based on field and/or template) how can I do this? (I like to use the Module InputfieldSelectImages but you either can only get certain images from pages or from a custom directory, thats where I am stuck). Can anyone help?

Cheers, Christian

Link to comment
Share on other sites

Welcome to the PW forums @virgomania!

17 hours ago, virgomania said:

I am new to processwire and was wondering, if one can define custom upload paths for File/ImageUploadFields based on fieldname and/or templatename - and how to do this.

I don't believe that's possible.

The PW core philosophy is to have images and files connected to the pages they are uploaded to and not in one central repository. In the past Ryan (lead developer of PW) has discussed why he favours this approach. Here's one old topic...

...and maybe others will chime in with links to more background on this subject.

So a few suggestions:

1. Consider if you absolutely must have a central image/file repository. Maybe you could try the PW-way and you might find it works pretty well for 99% of cases. I think a lot of people new to PW first thought they needed a media library and then gradually learned that they didn't. ?

2. If your main use case is linking/inserting files or images into rich text (CKEditor) fields then be aware that you can link/insert images from any page, not only the current page. Some people even create one or more dedicated "library" pages which hold a lot of files/images that they use on other pages. The Media Library module can help with this.

3. If your main use case is dedicated fields which will hold files/images then have a look at the commercial Media Manager module. I think this module also allows you to link/insert the media in CKEditor fields.

4. You mentioned the Select Images inputfield (used together with Dynamic Options I assume). Because you supply the selectable images via a hook you can make images from multiple different pages selectable. Here's an example hook that would make images selectable from any image field on any page using the basic_page or home template:

$wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) {
	// The Dynamic Options field
	$field = $event->arguments(1);

	// For a field named "select_images"
	if($field->name === 'select_images') {
		$options = [];
		// For each page that uses the basic_page or home template
		foreach($event->wire()->pages->find("template=basic_page|home") as $p) {
			// Get the FieldtypeImage fields that are in the page's template
			$image_fields = $p->fields->find("type=FieldtypeImage");
			// For each image field
			foreach($image_fields as $field) {
				// Get the images stored on the page in that field
				$images = $p->getUnformatted($field->name);
				// For each image
				foreach($images as $image) {
					// Add a selectable option
					$options[$image->url] = "{$image->basename}<br>{$image->filesizeStr}";
				}
			}
		}
		$event->return = $options;
	}
});

But be aware that this won't scale infinitely. At some point the number of selectable images might be unmanageable and you may strike performance issues in the page editor. I think Media Manager might be more scalable in this respect.

Link to comment
Share on other sites

Hi Robin S,

thanks for pointing me to the right direction - I think I will go with the 4th of your suggestions, since the different templates I have will be mostly seperate from each other and overspilling images/files with one another is likely not going to happen. I thank your for the example code that helps a lot.

Many thanks ?

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...