The dataobject_manager module is an extension to ComplexTableField that allows robust management of DataObjects related to a Page. Features:
The DataObjectManager class further cascades into subclasses FileDataObjectManager and ImageDataObjectManager, which add more features specific to file-containing DataObjects, including:
* File importing
Place the dataobject_manager folder in the Silverstripe root and run a /dev/build. If you wish to take advantage of FileDataObjectManager or ImageDataObjectManager, install the swfuploadfield module, as well.
A subclass of ComplexTableField, the DataObjectManager takes the same arguments and configuration as its parent. In the example below we will use a DataObjectManager to manage Testimonial DataObjects on a given page type. mysite/code/Testimonial.php
class Testimonial extends DataObject { static $db = array ( 'Date' => 'Date', 'Author' => 'Text', 'Quote' => 'HTMLText' ); static $has_one = array ( 'TestimonialPage' => 'TestimonialPage' ); public function getCMSFields_forPopup() { return new FieldSet( new CalendarDateField('Date'), new TextField('Author'), new TextareaField('Quote') ); } }
mysite/code/TestimonialPage.php
class TestimonialPage extends Page { static $has_many = array ( 'Testimonials' => 'Testimonial' ); public function getCMSFields() { $f = parent::getCMSFields(); $f->addFieldToTab("Root.Content.Testimonials", new DataObjectManager( $this, 'Testimonials', 'Testimonial', array('Date' => 'Date','Author'=>'Author','Quote' => 'Quote'), 'getCMSFields_forPopup' )); return $f; } }
After running a /dev/build (assuming the relationship had not already been created), we should get something like this in the CMS:
To edit a record, simply click the edit button on any given row, and save the new information.
You may search the displayed records using the live search bar.
If you have any custom filters or sorting applied to the list, the search will retain its state.
If you would like to filter your records by a specific field on the DataObject, you can apply a custom filter field, which will appear as a dropdown menu above the list of records. In the below example, we will add a filter to the list of testimonials that will only show us records with a given Author.
$myDataObjectManager->setFilter( 'Author', // The field we're filtering 'Filter by Author', // The label for the filter field array( 'Bob' => 'Bob', 'Suzy' => 'Suzy', 'Lisa' => 'Lisa' ) // The dropdown map of values => display text. The values will be matched against the Author field. );
Because drag-and-drop sorting adds custom functionality to all eligible DataObject subclasses, the DataObjectManager requires that the user opt into sorting capability.
mysite/_config.php
SortableDataObject::add_sortable_class('Testimonial');
After running a /dev/build, the following will be modified to give Testimonials sorting capability.
* The $default_sort property of the Testimonial will be set to SortOrder. This will ensure that the frontend template displays the objects in the correct order.
Note: To add several sortable classes at once, use SortableDataObject::add_sortable_classes(array $classNames)
setAddTitle($title) The text that will be displayed on the Add button. Defaults to Title field (in this case, Testimonial ).
setSingleTitle($title) The text that will be displayed on saving of the record, e.g. Saved $SingleTitle successfully. . Defaults to Title field of the DataObjectManager.
A FileDataObjectManager takes one more argument than a standard DataObjectManager. It requires the name of the File relationship on the DataObject. In the following example, we'll create a Resource object that contains a PDF file. The Resource objects are contained in a ResourcePage page type.
mysite/code/Resource.php
class Resource extends DataObject { static $db = array ( 'Name' => 'Text', 'Description' => 'Text', 'Category' => "Enum('Industry, Finance, Education')" ); static $has_one = array ( 'Attachment' => 'File', 'ResourcePage' => 'ResourcePage' ); public function getCMSFields_forPopup() { return new FieldSet( new TextField('Name'), new TextareaField('Description'), new DropdownField('Category','Category', singleton('Resource')->dbObject('Category')->enumValues()), new FileIFrameField('Attachment') ); } }
mysite/code/ResourcePage.php
class ResourcePage extends Page { static $has_many = array ( 'Resources' => 'Resource' ); public function getCMSFields() { $f = parent::getCMSFields(); $manager = new FileDataObjectManager( $this, // Controller 'Resources', // Source name 'Resource', // Source class 'Attachment', // File name on DataObject array( 'Name' => 'Name', 'Description' => 'Description', 'Category' => 'Category' ), // Headings 'getCMSFields_forPopup' // Detail fields (function name or FieldSet object) // Filter clause // Sort clause // Join clause ); $f->addFieldToTab("Root.Content.Resources",$manager); return $f; } }
The primary difference between a FileDataObjectManager and a standard DataObjectManager is that its focus is on the File object related to the DataObject. By default, all the records in a FileDataObjectManager are displayed in a grid view, each represented by an icon of its related File object.
This view can be toggled by clicking “List view” or “Grid view” on the FileDataObjectManager.
Further, adding records to a FileDataObjectManager begins with a bulk upload or import of files.
After uploading a batch of files, the FileDataObjectManager walks through each one and allows the user to add attributes to its related DataObject. Notice that the FileField is automatically removed from this process. It is replaced when editing a record to allow file replacement.
setAllowedFileTypes(array $types) Sets the file types that are allowed to be uploaded through the bulk uploader. All invalid types will be greyed out in the dialog box. File types are passed as an array of extensions, with or without a leading period, e.g. array('pdf','doc') .
setBrowseButtonText($text) Sets the text that will appear on the upload button. e.g. “Upload (PDF of DOC only)”.
setGridLabelField($field) Sets the name of the field that will label each Object in the grid view. Defaults to the Filename of the related File object, but can also be a property of the DataObject itself, e.g. $myFileDataObjectManager→setGridLabelField('Name')
setPluralTitle($title) Sets the plural form of the DataObjects being managed. Used for display in the upload popup window, e.g. “Upload MyObjects”
setButtonAddTitle($title) The text that will appear on the Add button of the FileDataObjectManager. Defaults to PluralTitle.
setUploadLimit($limit) Sets the maximum number of files that can be added to the upload queue. Default is 20.
allowUploadFolderSelection() Allows the user to select the destination folder of all uploaded files. Default is /assets/Uploads .
enableUploadDebugging() Activates a debugging window for the bulk upload tool.
The ImageDataObjectManager takes the same arguments and configuration as a FileDataObjectManager. The only difference is that its only allowed file classes are Image or subclasses thereof.
To manage $has_one, $has_many, or $many_many relationships of Pages to DataObjects, the following classes are available as subclasses of their respective ComplexTableField:
The DataObjectManager comes bundled with a few extra useful goodies:
The best way to get support is through the Silverstripe forum. The following thread has been very active:
http://www.silverstripe.org/all-other-modules/show/253878?start=96#post255678
Please use comments for notes, tips and corrections about the described
functionality.
Use the Silverstripe Forum to
ask questions.