mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2026-02-06 06:40:24 +01:00
Filepicker form field (#750)
# What this PR introduces
This PR introduces a `filepicker` field which is a replacement for the `pagemediaselect` field.
This field loads the files list via AJAX each time it's triggered. This means you can now upload a file via FTP or via Admin and the new file is immediately available to be selected.
Previously, `pagemediaselect` required a page reload to see the newly added files.
--
### Options
#### `accept`
`accept` allows file extensions. For example, to only allow `yaml` and `json` files:
```yaml
accept:
- .yaml
- .json
```
By default, any file is listed.
#### `folder`
```yaml
folder: 'user/plugins/testing`
```
folder has been enhanced to allow `self@` as well as `page@:` and `theme@:` prefixes.
Example of usage, assuming we have a blog item at the route `/blog/ajax-upload` (physical location being `user/pages/02.blog/ajax-upload`), with the `page@:` prefix the folder would be:
```yaml
folder: 'page@:/blog/ajax-upload'
```
#### `preview_images`
```yaml
preview_images: true
```
If enabled, shows a preview for the images file types
> ### NOTE: `self@` is not allowed outside of the Pages scope, an error will be thrown.
## Example usage
```
header.a_file:
type: filepicker
folder: 'user/plugins/admin'
label: Select a file
```
This commit is contained in:
@@ -339,6 +339,56 @@ class AdminController
|
||||
exit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by the filepicker field to get a list of files in a folder.
|
||||
*/
|
||||
protected function taskGetFilesInFolder()
|
||||
{
|
||||
if (!$this->authorizeTask('save', $this->dataPermissions())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data = $this->view == 'pages' ? $this->admin->page(true) : $this->prepareData([]);
|
||||
|
||||
$settings = $data->blueprints()->schema()->getProperty($this->post['name']);
|
||||
|
||||
$folder = $settings['folder'];
|
||||
|
||||
// Do not use self@ outside of pages
|
||||
if ($this->view != 'pages' && in_array($folder, ['@self', 'self@'])) {
|
||||
$this->admin->json_response = [
|
||||
'status' => 'error',
|
||||
'message' => sprintf($this->admin->translate('PLUGIN_ADMIN.FILEUPLOAD_PREVENT_SELF', null, true), $folder)
|
||||
];
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set destination
|
||||
$folder = Folder::getRelativePath(rtrim($folder, '/'));
|
||||
$folder = $this->admin->getPagePathFromToken($folder);
|
||||
|
||||
$available_files = Folder::all($folder, ['recursive' => false]);
|
||||
|
||||
// Handle Accepted file types
|
||||
// Accept can only be file extensions (.pdf|.jpg)
|
||||
$accepted_files = [];
|
||||
if (!isset($settings['accept'])) {
|
||||
$accepted_files = $available_files;
|
||||
} else {
|
||||
foreach ((array) $available_files as $available_file) {
|
||||
foreach ((array) $settings['accept'] as $type) {
|
||||
$find = str_replace('*', '.*', $type);
|
||||
$match = preg_match('#' . $find . '$#', $available_file);
|
||||
if ($match) {
|
||||
$accepted_files[] = $available_file;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->admin->json_response = ['status' => 'success', 'files' => $accepted_files, 'folder' => $folder];
|
||||
}
|
||||
|
||||
protected function taskGetNewsFeed()
|
||||
{
|
||||
$cache = $this->grav['cache'];
|
||||
@@ -1697,7 +1747,7 @@ class AdminController
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Removes a file from the flash object session, before it gets saved
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user