Skip to content

Instantly share code, notes, and snippets.

@Zauberfisch
Created March 10, 2014 06:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zauberfisch/9460436 to your computer and use it in GitHub Desktop.
Save Zauberfisch/9460436 to your computer and use it in GitHub Desktop.
SilverStripe 3.x GridField with Orderable/Sortable ManyMany List
<?php
class Page extends SiteTree {
private static $many_many = array(
'RelatedPages' => 'Page',
);
// be careful to name the many_many_extraField something that does not conflict with existing DB fields.
// eg: 'Sort' already exists on class SiteTree, so using that will get you into trouble.
private static $many_many_extraFields = array(
'RelatedPages' => array(
'SortOnPage' => 'Int',
),
);
public function getCMSFields() {
$return = parent::getCMSFields();
$return->addFieldToTab('Root', Tab::create('RelatedPages', $this->fieldLabel('RelatedPagesTab')));
$return->addFieldToTab(
'Root.RelatedPages',
GridField::create(
'RelatedPages',
$this->fieldLabel('RelatedPages'),
$this->getManyManyComponents('RelatedPages'),
GridFieldConfig_RelationEditor::create()
->removeComponentsByType('GridFieldAddNewButton')
->addComponent(new GridFieldOrderableRows('SortOnPage'))
)
);
return $return;
}
public function fieldLabels($includerelations = true) {
$return = parent::fieldLabels($includerelations);
$return['RelatedPagesTab'] = _t('Page.RelatedPagesTab', 'Related Pages');
$return['RelatedPages'] = _t('Page.RelatedPages', 'Related Pages');
return $return;
}
public function RelatedPages() {
return $this->isInDB() ? $this->getManyManyComponents('RelatedPages')->sort('SortOnPage', 'ASC') : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment