How to add many files with silverstripe uploadfield - php

Hello everyone,
I'm trying to add more than 1 file to my uploadfield with this code ->
class FileDo extends File {
static $has_one = array(
'DocumentsFile' => 'DocumentsFile',
);
}
class DocumentsFile extends DataObject {
static $has_one = array(
'DocumentPageAcces1' => 'DocumentPageAcces1'
);
static $has_many = array(
'Files' => 'FileDo'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('DocumentPageAcces1ID');
return $fields;
}
public function onBeforeWrite() {
parent::onBeforeWrite();
$page = DataObject::get_one('DocumentPageAcces1');
if($page) {
$this->DocumentPageAcces1ID = $page->ID;
}
}
}
class DocumentPageAcces1 extends Page {
static $has_many = array(
'DocumentsFiles' => 'DocumentsFile',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new TextareaField('DocumentsIntro_en', "Document Introduction"));
$fields->addFieldToTab('Root.Main', new TextareaField('PublicationsIntro_en', "Publication Introduction"));
$fields->addFieldToTab('Root.FR', new TextareaField('DocumentsIntro_fr', "Document Introduction"));
$fields->addFieldToTab('Root.FR', new TextareaField('PublicationsIntro_fr', "Publication Introduction"));
$fields->addFieldToTab('Root.NL', new TextareaField('DocumentsIntro_nl', "Document Introduction"));
$fields->addFieldToTab('Root.NL', new TextareaField('PublicationsIntro_nl', "Publication Introduction"));
$upload = new UploadField(
'DocumentsFile',
'Document',
$this->DocumentsFiles()
);
$fields->addFieldToTab('Root.DocumentsFile', $upload);
$fields->removeByName('Content');
$fields->removeByName('Metadata');
return $fields;
}
}
class DocumentPageAcces1_Controller extends Page_Controller {
}
So to make it clear: i'm trying to add some DocumentFile in my DocumentPageAcces1. When i execute this code, i have in my DocumentPageAcces1 the tab DocumentsFiles and in this tab i have the uploadfield.
THE PROBLEM is that the uploadfield doesn't want to keep my file so when i chose some file, i click OK in my finder and nothing happens......Could anyone help me?
Thanks Thomas.

UploadField isn't made to work with DataObjects but like you found with File or it's subclasses.
Although in your case, I don't think you need a subclass, so you could just remove your DocumentFileDo class that extend File and use this in your DocumentPageAcces1.php
static $has_many = array(
'DocumentFiles' => 'File',
);
In the case you want to have more control/details on the files you upload (i.e. add descriptions, titles etc...), in that case you can create a DataObject that has a $has_one relation to a File and then use that DataObject in the relation on your page with a GridField:
DocumentFile.php
class DocumentFile extend DataObject
{
static $db = array(
'Description' => 'HTMLText'
);
static $has_one = array(
'File' => 'File'
);
}
DocumentPageAcces1.php
static $has_many = array(
'DocumentFiles' => 'DocumentFile'
);
function getCMSFields()
{
$fields = parent::getCMSFields();
$c = new GridFieldConfig_RelationEditor();
$f = new GridField('Documents', 'DocumentFiles', $this->DocumentFiles(), $c);
$fields->addFieldToTab('Root.Documents', $f);
return $fields;
}

After some hours I found a solution, maybe it's not the best one but it works for me ->
I created a class that extends the File class for each document type i need so I have for example
class DocumentFileDo extends File {
static $has_one = array(
'DocumentPageAcces1' => 'DocumentPageAcces1'
);
}
In my DocumentPageAcces1.php I have a has_many relationship like this :
static $has_many = array(
'DocumentFileDos' => 'DocumentFileDo',
);
And I the getCMSFields function I have :
$fields->addFieldToTab('Root.Document', new UploadField('DocumentFileDos'));
It works well, you can add many file with just a has_many relationship and an UploadField. But it's not the best i think if you have a hundred different documents type..
PS: With UploadField you can drag and drop files but this function which works well on Chrome does not on Firefox!

I just struggled with the same thing. SilverStripe even has a (faulty*) developer guide on this matter (though it took me hours to find it) … Anyway, I found a solution. (assuming SilverStripe 4)
mysite/page/MyCustomPage.php
class MyCustomPage extends Page
{
private static $many_many = array(
'DocumentFiles' => File::class
);
public function getCMSFields(){
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main',
UploadField::create('DocumentFiles', 'My Documents')
);
return $fields;
}
}
mysite/extensions/DocumentFileExtension.php
class DocumentFileExtension extends DataExtension
{
private static $belongs_many_many = array(
'DocumentContainers' => MyCustomPage::class
);
// I actually don't know the purpose of the name "DocumentContainers"
// ("Galleries" in the guide); it is never referenced again.
}
mysite/_config/app.yml
SilverStripe\Assets\File:
extensions:
- DocumentFileExtension
themes/mytheme/templates/Layout/MyCustomPage.ss
<% loop $DocumentFiles %>
$Link
<% end_loop %>
You basically extend the native File class into a custom version of it, and "suddenly" the unchanged UploadField (that also does the same for single files) now allows multiple files; complete with a GUI and everything.
Hope this works for you.
* They keep using 'Foo' => 'Bar' all over their official documentation pages instead of 'Foo' => Bar::class, which you have to use in ss4 in order to avoid an Exception:
Uncaught InvalidArgumentException: many_many relation SilverStripe\Blog\Model\BlogPost.DownloadFiles references class File which doesn't exist

Related

How to change order of items in SilverStripe GridField

I use a GridField in SilverStripe to create HTML section items that are rendered on a page. This works so far but it always displays the sections in the order that I added them to the CMS or rather by the ID it gets when it's created.
So my question is: How can i change that order. I don't want to manually change the IDs but would rather do a simple drag and drop.
Edit: Could the use of Elemental be a solution to this problem?
Screenshot of the CMS view
The Page:
class HomePage extends Page
{
private static $has_many = [
'SectionObjects' => SectionObject::class
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Sections',
$grid = GridField::create('SectionObjects', 'Sections', $this->SectionObjects(), GridFieldConfig_RecordEditor::create())
);
$config = $grid->getConfig();
$dataColumns = $config->getComponentByType(GridFieldDataColumns::class);
$dataColumns->setDisplayFields([
'ID' => 'ID',
'LastEdited' => 'Changed'
]);
return $fields;
}
}
The Section object
class SectionObject extends DataObject{
private static $db = [
'Content' => 'Text',
'BgColor' => Color::class
];
private static $has_one = [
'HomePage' => HomePage::class
];
public function getCMSFields(){
return new FieldList(
TextareaField::create('Content','SectionContent'),
ColorField::create('BgColor', 'Hintergrundfarbe')
);
}
}
Definitely using the elemental module would allow you to have the kind of behaviour you want - but if you don't want to go to the effort of refactoring your code to comply with that module, you could use either the gridfieldextensions or sortablegridfield module to allow you to sort the gridfield (with drag and drop) to your heart's content.

TreeDropdownField Silverstripe 4 Navigation Labels

I work on a SilverStripe 4 project where I use the TreeDropdownfield. In 3.6 it got the MenuTitle (Navigation Label) by default, but I noticed that in SilverStripe the default page titles are being displayed.
Since my customer changed the page titles the TreeDropdownField shows long page titles. I would like to display the Navigation labels instead of those long page titles because the structure isn't clear with those long titles.
I have the following code:
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
class InternalLink extends DataObject {
private static $db = [
'Title' => 'Varchar',
];
private static $has_one = [
'LinkTarget' => SiteTree::class,
'InternalLinkCategory' => 'InternalLinkCategory'
];
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', TextField::create( 'Title', 'Titel link' ) );
$fields->addFieldToTab('Root.Main', TreeDropdownField::create( 'LinkTargetID', 'Doeladres', SiteTree::class ) );
return $fields;
}
}
you can use TreeDropdownField::setTitleField(). It sets the field to use for item title.
$treeField = TreeDropdownField::create('LinkTargetID', 'Doeladres', SiteTree::class);
$treeField->setTitleField('MenuTitle');
$fields->addFieldToTab('Root.Main', $treeField);

How to add sortable column for member list in Silverstripe admin?

I am struggling to add sort functionality on one of my member summary fields in admin.
I have extended the Silverstripe member class using:
class MyMemberExtension extends DataExtension
I have added a few fields to the default gridfield in admin:
private static $db = array(
'Organisation' => 'Varchar(100)'
);
private static $summary_fields = array(
'FirstName' => 'First Name',
'Surname' => 'Surname',
'Email' => 'Email',
'OrganisationName' => 'Organisation Name',
'LastVisited' => 'Last Visited',
'NumVisit' => 'Num Visits'
);
private static $casting = array(
'OrganisationName' => 'Varchar(100)'
);
public function getOrganisationName() {
return $this->owner->Organisation;
}
...and that all works nicely.
However, only the core fields like LastVisited are giving me sort arrows on the column headers.
I'm currently stuck as to how to implement the sort on my Organisation field. I tried adding :
public function getCMSFields()
{
$fields = parent::getCMSFields();
$grid = $fields->dataFieldByName('Organisation');
$gridConfig = $grid->getConfig();
$gridConfig->addComponent(new GridFieldSortableHeader());
return $fields;
}
public function getEditForm($id = null, $fields = null) {
$form=parent::getEditForm($id, $fields);
$model = singleton($this->modelClass);
// add sorting if we have a field for...
if (class_exists('GridFieldSortableRows')
&& $model->hasField('Organisation')
&& $gridField=$form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
if($gridField instanceof GridField) {
$gridField->getConfig()->addComponent(new GridFieldSortableRows('Organisation'));
}
}
return $form;
}
...to my class, but I'm not convinced these are even being called, as even if I just return null from these two functions nothing changes.
I have found a few answers that deal with extensions to ModelAdmin, but not for the core Member list. Thanks!
First of all, I'm not sure why you chose to have a getter named OrganisationName, where you could just as well use Organisation directly? That being said, I think your question is valid and might apply to different scenarios and/or field-types.
The Form-field that is being used to edit members is the Members GridField within SecurityAdmin. Luckily, there's an extension hook (updateEditForm) to modify the form fields of SecurityAdmin.
So in order to modify the sorting of the Members GridField, create an Extension like the following:
<?php
class MemberAdminExtension extends Extension
{
public function updateEditForm(Form $form)
{
/** #var GridField $memberGridField */
if ($memberGridField = $form->Fields()->dataFieldByName('Members')) {
/** #var GridFieldSortableHeader $sortHeader */
if ($sortHeader = $memberGridField->getConfig()->getComponentByType('GridFieldSortableHeader')) {
// Map OrganisationName to the Organisation field
$sortHeader->setFieldSorting([
'OrganisationName' => 'Organisation'
]);
}
}
}
}
And apply the extension via config to SecurityAdmin:
# Within _config/config.yml
SecurityAdmin:
extensions:
- MemberAdminExtension
After a dev/build your Member table should be sortable by Organisation Name as well…

silverstripe-translatable: how to make dataobjects translatable (slideshow)?

I am creating a multilingual Website in Silverstripe, the goal is to have the whole content available in three languages. I am using the translatable module, works fine for now.
Some pages contains a slideshow, meaning that I am associating a bunch of images and captions to these pages. Looks like this:
class Slide extends DataObject {
private static $db = array(
'Caption' => 'Varchar(255)',
'SortIndex' => 'Int'
);
private static $has_one = array(
'ParentPage' => 'Page',
'Image' => 'Image'
);
public static $default_sort = 'SortIndex';
public function getCMSFields() {
// parent::getCMSFields() does all the hard work and creates the fields for Title, IsActive and Content.
$fields = parent::getCMSFields();
$fields->dataFieldByName('Caption')->setTitle('Titel');
$fields->dataFieldByName('Image')->setTitle('Bild');
$fields->dataFieldByName('Index')->setTitle('Reihenfolge');
$fields->push(new TextField('Caption', 'Titel'));
$fields->push(new UploadField('Image', 'Profile Image'));
// Apply Translatable modifications
$this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
return $fields;
}
}
class SlideShowPage extends Page {
private static $has_many = array(
'Slides' => 'Slide'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$conf = GridFieldConfig_RelationEditor::create(10);
$conf->addComponent(new GridFieldSortableRows('SortIndex'));
$fields->addFieldToTab('Root.Slideshow', new GridField('Slide', 'Slides', $this->Slides(), $conf));
// Apply Translatable modifications
$this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
return $fields;
}
}
I want to make these slideshows translatable as well. Meaning that I want to prepare the slide show once in the primary language, then hit the "create new translation" button, and get the slideshow available in the translated version as well, with the captions ready to be translated (similar as the main text content). According to the silverstripe-translatable docs this shall be possible. I have added these calls to applyTranslatableFieldsUpdate() (see in the code above) and added these lines to my _config.php file as well:
SlideShowPage::add_extension('Translatable');
Slide::add_extension('Translatable');
The table Slide_translationgroups is even created successfully and filled with new entries for each translation. But the content of table Slide is not copied.
What am I missing?
Thanks!
Sacher

Background Image UploadField doesn't show in the CMS

On a SilverStripe CMS website I have a custom footer add-on code that I'd like to extend so I can upload a background image in the CMS. The code seems fine, but the added field is no where to be found in the CMS.
This is my code:
class CustomFooter extends DataExtension {
static $db = array(
'FooterContent' => 'HTMLText'
);
public static $has_one = array(
'Logo' => 'Image',
'BGImage' => 'Background Image'
);
public function getCMSFields() {
$this->extend('updateCMSFields', $fields);
return $fields;
}
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Footer', new HTMLEditorField('FooterContent', 'Footer Content'));
$fields->addFieldToTab('Root.Main', new UploadField('Logo', 'Company Logo'));
$fields->addFieldToTab('Root.Main', new UploadField('BGImage', 'Background image'));
}
}
Weirdly enough, both image upload fields don't show where as the Content tab show and functions as expected.
Why aren't the UploadFields displaying?
Your
public static $has_one = array(
"Logo"=>"Image",
"BGImage"=>"Background Image"
);
looks pretty weird. The $has_onearray has the relation name as key and the class name of the relation as value, I doubt a classname with space in the name is allowed. So I'd try something like
public static $has_one = array(
"Logo"=>"Image",
"BGImage"=>"Image"
);
then run a dev/build?flush and check in the database if your table has a LogoID column and a BGImageIDcolumn for the has one relations.
The field generation looks ok, it should work as is.

Categories