Skip to content

Instantly share code, notes, and snippets.

@sminnee
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sminnee/c3ac2ffecf1c42bbe421 to your computer and use it in GitHub Desktop.
Save sminnee/c3ac2ffecf1c42bbe421 to your computer and use it in GitHub Desktop.
Simple custom GridField plugin
<?php
class Client extends DataObject {
private static $db = array(
"Name" => "Varchar",
"Website" => "Varchar",
);
private static $has_many = array(
"Contacts" => "Contact",
"Job" => "Job",
);
function getCMSFields() {
$fields = parent::getCMSFields();
if($jobs = $fields->dataFieldByName('Job')) {
$config = $jobs->getConfig();
$config->addComponent(new GridFieldSortableRows('JobSort'));
$config->addComponent(
new GridFieldSimpleHeader,
'GridFieldSortableHeader');
$config->removeComponentsByType(
'GridFieldSortableHeader');
}
return $fields;
}
}
<?php
class Contact extends DataObject {
private static $db = array(
"FirstName" => "Varchar",
"Surname" => "Varchar",
"Email" => "Varchar",
"Phone" => "Varchar",
);
private static $has_one = array(
"Client" => "Client",
);
private static $belongs_many_many = array(
"Jobs" => "Job",
);
private static $summary_fields = array(
"FirstName",
"Surname",
"Email",
);
function getTitle() {
return $this->FirstName . ' ' . $this->Surname;
}
}
<?php
class CRMAdmin extends ModelAdmin {
private static $url_segment = "crm";
private static $menu_title = "CRM";
private static $managed_models = array(
"Client",
"Contact",
"Job",
);
}
<?php
/**
* A simple, non-sortable header
*/
class GridFieldSimpleHeader implements GridField_HTMLProvider {
public function getHTMLFragments($gridField) {
$forTemplate = new ArrayData(array());
$forTemplate->Fields = new ArrayList;
foreach($gridField->getColumns() as $columnField) {
$metadata = $gridField->getColumnMetadata($columnField);
$fieldName = str_replace('.', '-', $columnField);
$title = $metadata['title'];
$field = new LiteralField($fieldName,
'<span class="non-sortable">'
. $title . '</span>');
$forTemplate->Fields->push($field);
}
return array(
// Will generate a TR, gets placed in the THEAD
'header' => $forTemplate
->renderWith('GridFieldSortableHeader_Row'),
);
}
}

Getting started

  • Create a SilverStripe 3.1 project
  • composer require undefinedoffset/sortablegridfield:*
  • Put the above files into mysite/code/
  • dev/build and open admin!

Challenges

  1. Move the GridFieldSimpleHeader so that it's on the bottom of the table, rather than the top.

  2. Add a parameter to GridFieldSimpleHeader so that you can say where it's placed, and add two of them, so that there is 1 at the top and 1 at the bottom.

<?php
class Job extends DataObject {
private static $db = array(
"Name" => "Varchar",
"StartDate" => "Date",
"JobSort" => "Int",
);
private static $has_one = array(
"Client" => "Client",
);
private static $many_many = array(
"Contacts" => "Contact",
);
private static $summary_fields = array(
"Name",
"StartDate",
);
private static $default_sort = "StartDate";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment