Last active
December 15, 2015 09:49
-
-
Save stojg/5240804 to your computer and use it in GitHub Desktop.
This is an example of using the siteconfig decorator for adding a sitewide quicklinks navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Object::add_extension('SiteConfig', 'QuickLinksSiteConfig'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class QuickLink extends DataObject { | |
public static $has_one = array( | |
'SiteConfig' => 'SiteConfig' | |
); | |
/** | |
* | |
* @var array | |
*/ | |
public static $db = array( | |
'Order' => 'Int', | |
'AccessKey' => 'Int', | |
'OnClick' => 'Varchar(255)' | |
); | |
/** | |
* | |
* @var array | |
*/ | |
public static $has_one = array( | |
'Page' => 'Page' | |
); | |
/** | |
* | |
* @var array | |
*/ | |
public static $summary_fields = array( | |
'PageID' => 'Page', | |
'AccessKey' => 'Access key', | |
'OnClick' => 'OnClick' | |
); | |
/** | |
* Sort the links by the Order fields | |
* | |
* @var string | |
*/ | |
public static $default_sort = 'Order'; | |
/** | |
* | |
* @return FieldList | |
* @codeCoverageIgnoreStart | |
*/ | |
public function getCMSFields() { | |
$fields = new FieldList(); | |
$fields->push(new TabSet("Root", $mainTab = new Tab("Menu"))); | |
$fields->addFieldToTab("Root.Menu", new TreeDropdownField('PageID', 'Link to', 'Page')); | |
$fields->addFieldToTab('Root.Menu', $accessKey = new TextField('AccessKey', 'AccessKey (optional)')); | |
$fields->addFieldToTab('Root.Menu', $onclick = new TextField('OnClick', 'On click event (optional)')); | |
$onclick->setDescription("Ex. _gaq.push(['_trackEvent', window.location.pathname, 'Footer-BankingInfo', 'text-link/Mortgage calculators']);"); | |
return $fields; | |
} | |
// @codeCoverageIgnoreStop | |
/** | |
* | |
* @return string | |
*/ | |
public function getTitle() { | |
return $this->Page()->Title; | |
} | |
/** | |
* | |
* @return string | |
*/ | |
public function getMenuTitle() { | |
return $this->Page()->MenuTitle; | |
} | |
/** | |
* | |
* @return string | |
*/ | |
public function getLink() { | |
return $this->Page()->Link(); | |
} | |
/** | |
* | |
* @return string | |
*/ | |
public function getAltText() { | |
return $this->Page()->MenuTitle; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds fields to the global settings | |
* | |
* @package SiteConfig | |
*/ | |
class QuickLinksSiteConfig extends DataExtension { | |
/** | |
* Extra static fields to add to the SiteConfig object | |
* | |
* @var Array | |
*/ | |
public static $has_many = array( | |
'QuickLinks' => 'QuickLink' | |
); | |
/** | |
* Adds form fields to the CMS settings page for each of the extra static fields | |
* | |
* @param FieldList $fields The list of fields defined in the parent class SiteConfig | |
*/ | |
public function updateCMSFields(FieldList $fields) { | |
$config = new GridFieldConfig(); | |
$config->addComponent(new GridFieldToolbarHeader()); | |
$config->addComponent($sort = new GridFieldSortableHeader()); | |
$config->addComponent(new GridFieldInlineEditing()); | |
$config->addComponent(new GridFieldButtonRow('before')); | |
$config->addComponent($addButton = new GridFieldAddNewButton('buttons-before-left')); | |
$addButton->setButtonName('Add quicklink'); | |
$config->addComponent(new GridFieldDeleteAction()); | |
$config->addComponent(new GridFieldEditButton()); | |
$config->addComponent(new GridFieldDetailForm()); | |
$config->addComponent(new GridFieldSortableRows('Order')); | |
$gridField = new GridField('Quicklink', 'QuickLinks', $this->owner->QuickLinks(), $config); | |
$fields->addFieldToTab('Root.QuickLinks', $gridField); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment