PHP | Working Example -
<?php
class Contact extends DataObject {
// Contact object's fields
public static $db = array(
'Name' => 'Varchar(255)',
'Description' => 'Text',
'Website' => 'Varchar(255)'
);
// One-to-one relationship with profile picture and contact list page
public static $has_one = array(
'ProfilePicture' => 'Image',
'ContactListPage' => 'ContactListPage'
);
// Summary fields
public static $summary_fields = array(
'Name' => 'Name',
'Description' => 'Description',
'Website' => 'Website URL'
);
public function getCMSFields_forPopup() {
// Profile picture field
$thumbField = new UploadField('ProfilePicture', 'Profile picture');
$thumbField->allowedExtensions = array('jpg', 'png', 'gif');
// Name, Description and Website fields
return new FieldList(
new TextField('Name', 'Name'),
new TextareaField('Description', 'Contact description'),
new TextField('Website', 'Website URL (including http://)'),
$thumbField
);
}
}
<?php
class ContactListPage extends Page {
// One to many relationship with Contact object
public static $has_many = array(
'Contacts' => 'Contact'
);
// Create Grid Field
public function getCMSFields() {
$fields = parent::getCMSFields();
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm()
);
$gridField = new GridField("Contacts", "Contact list:", $this->Contacts(), $gridFieldConfig);
$fields->addFieldToTab("Root.Contacts", $gridField);
return $fields;
}
}
class ContactListPage_Controller extends Page_Controller {
public static $allowed_actions = array (
);
public function init() {
parent::init();
}
}