Silverstripe additional More options action - silverstripe

Wondering if it was possible to add additional actions when editing a page to the More options menu in Silverstripe 3.1
Using the below sample, I have added an additional FormAction which I can see when editing an page. But where do I actually add the "custom_action" method?
class CustomPage extends SiteTree {
function getCMSActions(){
$actions = parent::getCMSActions();
$actions->addFieldToTab(
'ActionMenus.MoreOptions',
new FormAction('custom_action', 'Custom Action')
);
return $actions;
}
}
class CustomPage_Controller extends ContentController {
public function init() {
parent::init();
if(!Member::currentUserID()) {
die();
}
}
}

Related

Remove default HtmlEditorField from Page

Is there a way to remove/disable the default HtmlEditorField (Content) from a Page in back-end from SilverStripe 4.2.2?
From your Page or subclass of Page:
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->removeByName('Root.Main.Content');
return $fields;
}
Or as an extension:
class RemoveContentExtension extends \SilverStripe\ORM\DataExtension
{
public function updateCMSFields(\SilverStripe\Forms\FieldList $fields)
{
$fields->removeByName('Root.Main.Content');
}
}
And apply the extension to your page with YAML config:
# File: app/_config/content.yml
MyPage:
extensions:
- RemoveContentExtension

Calling the default exceptionHandler equivalent in SilverStripe 4

I've searched through the documentation and the API for SilverStripe 4 but I'm having trouble using the appropriate class to call the default exception handler.
How it worked before SilverStripe 4 within my Page.php controller's init()
parent::init();
set_exception_handler(function($exception) {
if( get_class($exception) == 'CustomException' ) {
Controller::curr()->handleCustomException($exception);
}
return exceptionHandler($exception);
});
How I expect it to work with SilverStripe 4
parent::init();
set_exception_handler(function($exception) {
if( get_class($exception) == 'CustomException' ) {
Controller::curr()->handleCustomException($exception);
}
return <SomeMonologClass>::handleException($exception);
});
I'm aware of SilverStripe 4 now using Monolog and I've come across the handleException function which I believe is what I need to call instead of exceptionHandler($exception).
Any advice would be much appreciated.
use SilverStripe\Control\Controller;
class MyController extends Controller
{
private static $dependencies = [
'logger' => '%$Psr\Log\LoggerInterface',
];
// This will be set automatically, as long as MyController is instantiated via Injector
public $logger;
protected function init()
{
$this->logger->debug("MyController::init() called");
parent::init();
}
}
As described here:
https://docs.silverstripe.org/en/4/developer_guides/debugging/error_handling/#accessing-the-logger-via-dependency-injection
And even more info here: https://www.silverstripe.org/learn/lessons/v4/advanced-environment-configuration-1

SilverStripe remove CSV Export button

I'm trying to remove the Export to CSV button in the top of a GridField in ModelAdmin.
I can't seem to find the class that creates the button (GridFieldExportButton right?). I'm guessing there is a function that populates the GridField with buttons / "actions" which I'm not familiar with.
To remove the scaffolded GridField for relationships...
class MyDataObject extends DataObject {
...
private static $has_many= array(
'OtherDataObjects' => 'OtherDataObject'
);
...
function getCMSFields() {
$fields = parent::getCMSFields();
if($grid = $fields->dataFieldByName('OtherDataObjects'))
$grid->getConfig()
->removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton');
return $fields;
}
...
}
If you are making the GridField then just add this when you create the field...
$gridField->getConfig()->removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton');
If you are looking for a gridfield that isn't within a data object edit form and is actually...
class MyAdmin extends ModelAdmin {
...
function getEditForm($id = null, $fields = null) {
$form = parent::getEditForm($id, $fields);
if($this->modelClass == 'MyDataObjectName') {
$form->Fields()
->fieldByName($this->sanitiseClassName($this->modelClass))
->getConfig()
->removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton');
}
return $form;
}
...
}
Setting model_importers to empty will do the reverse and remove the import ...
class MyAdmin extends ModelAdmin {
...
static $model_importers = array();
...
}

Upload and use custom JavaScript file in SilverStripe

My project has a crossword. The client would like to upload JavaScript files and change the pattern.
How do I make the upload file and include the JavaScript in the front end of the website? The file is getting uploaded but it is not showing up in source code.
The following is for SilverStripe 3.1.
This example adds a JavaScript file upload to each page. In the Controller's init() function if a file has been uploaded we load it into the page:
Page.php
class Page extends SiteTree {
private static $has_one = array(
'CustomJavascriptFile' => 'File'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$customJavascriptFile = UploadField::create('CustomJavascriptFile', 'Custom Javascript File');
$customJavascriptFile->setFolderName('javascript');
$customJavascriptFile->setAllowedExtensions(array('js'));
$fields->addFieldToTab('Root.Javascript', $customJavascriptFile);
return $fields;
}
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
if ($this->CustomJavascriptFileID) {
Requirements::javascript($this->CustomJavascriptFile()->RelativeLink());
}
}
}
This method gives us a custom JavaScript file per page. We could alternatively have the file uploaded to the Config Settings instead so we can upload one file that is then loaded for the whole site.
First we need to extend SiteConfig. We need to declare the extension in our config.yml:
mysite/_config/config.yml
---
Name: site
After: 'framework/*','cms/*'
---
SiteConfig:
extensions:
- CustomSiteConfig
CustomSiteConfig.php
class CustomSiteConfig extends DataExtension {
private static $has_one = array(
'CustomJavascriptFile' => 'File'
);
public function updateCMSFields(FieldList $fields) {
$customJavascriptFile = UploadField::create('CustomJavascriptFile', 'Custom Javascript File');
$customJavascriptFile->setFolderName('javascript');
$customJavascriptFile->setAllowedExtensions(array('js'));
$fields->addFieldToTab('Root.Javascript', $customJavascriptFile);
}
}
Page.php
class Page extends SiteTree {
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
$siteConfig = SiteConfig::current_site_config();
if ($siteConfig->CustomJavascriptFileID) {
Requirements::javascript($siteConfig->CustomJavascriptFile()->RelativeLink());
}
}
}

Custom Report SilverStripe 3.1

I followed the docs to create a custom report but keep failing to generate the report in the CMS. Has anyone else had this problem? I noticed that with older versions, the config had to include the report, but I see no sign of this in 3.1.
Here is the contents of CustomSideReport.php
class CustomSideReport_Day extends SideReport {
public function title() {
return "Event Calendar";
}
public function records() {
return Page::get()->sort("Title");
}
public function fieldsToShow() {
return array(
"Title" => array("NestedTitle", array("2"))
);
}
}
I have the done the usual dev/build and flush, but still nothing appears.
The documentation has now been updated to correctly show how to make custom site reports.
In SilverStripe 3.1 the class should extend SS_Report instead of SideReport.
Try this:
class CustomSideReport_Day extends SS_Report {
public function title() {
return 'Event Calendar';
}
public function sourceRecords($params = null) {
return Page::get()->sort('Title');
}
public function columns() {
$fields = array(
'Title' => 'Title'
);
return $fields;
}
}
Also note that records() has changed to sourceRecords($params = null) and fieldsToShow() has changed to columns().

Resources