I have a set of images that get used a lot on my site. So instead of saving them in every folder again I would love an image select field that combine images from the current page and the site folder.
I looked at the image field and did not see an option for that and in docs for select I don’t see how to build the query
If there is a fieldtype already I did not find it (yet)
Sorry to drudge up an old topic, but seeing as this was a relevant search result in my own googling, I thought this may be of value to someone.
On every image select, I wanted to see all images in the site, and all images in the page (and I didn’t want to lose the image preview of selected image in panel). So I…
copied the original field at panel/app/fields/image to site/fields/images
renamed all the files from image.* to images.*
change the class name in site/fields/images/images.php from ImageField to ImagesField.
change the options function in site/fields/images/images.php to…
public function options() {
$options = [];
$site = panel()->site();
foreach($site->images() as $image) {
$options[$image->filename()] = $image;
}
foreach($this->images() as $image) {
$options[$image->filename()] = $image;
}
return $options;
}
Then in the blueprint, I changed “type: image” to “type: images”.
If a file exists in both, it should be overwritten by the page version in the list.
I ended up using this. Perhaps it’s not ideal. Would be better to work the check into the function itself or store the url itself as the value perhaps.
$image->uri was returning the url without “content/” in it. I suppose that’s fine if you want to append content to it manually, but I preferred to use URL so I don’t have to tamper with the resulting value. Perhaps I was doing something incorrectly?
public function options() {
$options = [];
$site = panel()->site();
foreach($site->images() as $image) {
$options[$image->url()] = $image;
}
foreach($this->images() as $image) {
$options[$image->url()] = $image;
}
return $options;
}
The only thing I don’t like it about this, is that now the options on the $page level don’t overwrite the options on the $site level. That seemed like a nice graceful override that I could appreciate. If one were to use generic names like “hero.jpg” then it would always show a “hero.jpg” in the list, but if a page doesn’t have one, it will inherit it from $site default “hero.jpg”. But the list doesn’t become cluttered with duplicate file names.