From 60333f68ee3557ce9ecef1984c988a08c2fe6b51 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 5 Jun 2013 18:09:33 +0200 Subject: [PATCH] NEW UploadField lists all files, shows path info It doesn't make a lot of sense to limit the listing to assets/Uploads/, which is the default set through FileField->folderName. Showing all files regardless of folder makes them easier to find, users can still opt-in to filtering by folder through the TreeDropdownField. --- docs/en/changelogs/3.2.0.md | 8 ++++ forms/UploadField.php | 42 ++++++++++++++++----- tests/forms/uploadfield/UploadFieldTest.php | 25 +++++++++++- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/docs/en/changelogs/3.2.0.md b/docs/en/changelogs/3.2.0.md index 941e0d089..cc17062c9 100644 --- a/docs/en/changelogs/3.2.0.md +++ b/docs/en/changelogs/3.2.0.md @@ -4,6 +4,7 @@ * Minimum PHP version raised to 5.3.3 * DataObject::validate() method visibility changed to public + * UploadField "Select from files" shows files in all folders by default ## Changelog @@ -23,4 +24,11 @@ this example: ... } +### UploadField "Select from files" shows files in all folders by default + +In order to list files in a single folder by default (previous default behaviour), +use `setDisplayFolderName()` with a folder path relative to `assets/`: + + UploadField::create('MyField')->setDisplayFolderName('Uploads'); + ### Bugfixes diff --git a/forms/UploadField.php b/forms/UploadField.php index 227b37f41..97c75261e 100644 --- a/forms/UploadField.php +++ b/forms/UploadField.php @@ -163,6 +163,15 @@ class UploadField extends FileField { 'overwriteWarning' => true ); + /** + * @var String Folder to display in "Select files" list. + * Defaults to listing all files regardless of folder. + * The folder path should be relative to the webroot. + * See {@link FileField->folderName} to set the upload target instead. + * @example admin/folder/subfolder + */ + protected $displayFolderName; + /** * FieldList $fields or string $name (of a method on File to provide a fields) for the EditForm * @example 'getCMSFields' @@ -301,6 +310,21 @@ class UploadField extends FileField { return $this->setConfig('overwriteWarning', $overwriteWarning); } + /** + * @param String + */ + public function setDisplayFolderName($name) { + $this->displayFolderName = $name; + return $this; + } + + /** + * @return String + */ + public function getDisplayFolderName() { + return $this->displayFolderName; + } + /** * Force a record to be used as "Parent" for uploaded Files (eg a Page with a has_one to File) * @param DataObject $record @@ -1537,8 +1561,8 @@ class UploadField_SelectHandler extends RequestHandler { public function Form() { // Find out the requested folder ID. $folderID = $this->parent->getRequest()->requestVar('ParentID'); - if (!isset($folderID)) { - $folder = Folder::find_or_make($this->folderName); + if ($folderID === null && $this->parent->getDisplayFolderName()) { + $folder = Folder::find_or_make($this->parent->getDisplayFolderName()); $folderID = $folder ? $folder->ID : 0; } @@ -1571,19 +1595,19 @@ class UploadField_SelectHandler extends RequestHandler { $config = GridFieldConfig::create(); $config->addComponent(new GridFieldSortableHeader()); $config->addComponent(new GridFieldFilterHeader()); - $config->addComponent($columns = new GridFieldDataColumns()); - $columns->setDisplayFields(array( - 'StripThumbnail' => '', - 'Name' => 'Name', - 'Title' => 'Title' + $config->addComponent($colsComponent = new GridFieldDataColumns()); + $colsComponent->setDisplayFields(array( + 'Title' => singleton('File')->fieldLabel('Name'), + 'Filename' => singleton('File')->fieldLabel('Filename'), + 'Size' => singleton('File')->fieldLabel('Size') )); - $config->addComponent(new GridFieldPaginator(8)); // If relation is to be autoset, we need to make sure we only list compatible objects. $baseClass = $this->parent->getRelationAutosetClass(); // Create the data source for the list of files within the current directory. - $files = DataList::create($baseClass)->filter('ParentID', $folderID); + $files = DataList::create($baseClass); + if($folderID) $files = $files->filter('ParentID', $folderID); $fileField = new GridField('Files', false, $files, $config); $fileField->setAttribute('data-selectable', true); diff --git a/tests/forms/uploadfield/UploadFieldTest.php b/tests/forms/uploadfield/UploadFieldTest.php index 51b9b6cfb..bdad6147e 100644 --- a/tests/forms/uploadfield/UploadFieldTest.php +++ b/tests/forms/uploadfield/UploadFieldTest.php @@ -646,13 +646,29 @@ class UploadFieldTest extends FunctionalTest { $record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); $file4 = $this->objFromFixture('File', 'file4'); - $file5 = $this->objFromFixture('File', 'file5'); $fileSubfolder = $this->objFromFixture('File', 'file-subfolder'); - $fileNoEdit = $this->objFromFixture('File', 'file-noedit'); $response = $this->get('UploadFieldTest_Controller/Form/field/ManyManyFiles/select/'); $this->assertFalse($response->isError()); + // A bit too much coupling with GridField, but a full template overload would make things too complex + $parser = new CSSContentParser($response->getBody()); + $items = $parser->getBySelector('.ss-gridfield-item'); + $itemIDs = array_map(create_function('$el', 'return (int)$el["data-id"];'), $items); + $this->assertContains($file4->ID, $itemIDs, 'Contains file in assigned folder'); + $this->assertContains($fileSubfolder->ID, $itemIDs, 'Contains file in subfolder'); + } + + public function testSelectWithDisplayFolderName() { + $this->loginWithPermission('ADMIN'); + + $record = $this->objFromFixture('UploadFieldTest_Record', 'record1'); + $file4 = $this->objFromFixture('File', 'file4'); + $fileSubfolder = $this->objFromFixture('File', 'file-subfolder'); + + $response = $this->get('UploadFieldTest_Controller/Form/field/HasManyDisplayFolder/select/'); + $this->assertFalse($response->isError()); + // A bit too much coupling with GridField, but a full template overload would make things too complex $parser = new CSSContentParser($response->getBody()); $items = $parser->getBySelector('.ss-gridfield-item'); @@ -907,6 +923,10 @@ class UploadFieldTestForm extends Form implements TestOnly { $fieldHasManyNoView = UploadField::create('HasManyNoViewFiles') ->setFolderName('UploadFieldTest'); + + $fieldHasManyDisplayFolder = UploadField::create('HasManyDisplayFolder') + ->setFolderName('UploadFieldTest') + ->setDisplayFolderName('UploadFieldTest'); $fieldReadonly = UploadField::create('ReadonlyField') ->setFolderName('UploadFieldTest') @@ -938,6 +958,7 @@ class UploadFieldTestForm extends Form implements TestOnly { $fieldHasManyMaxTwo, $fieldManyMany, $fieldHasManyNoView, + $fieldHasManyDisplayFolder, $fieldReadonly, $fieldDisabled, $fieldSubfolder,