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
Related
I'm looking to create a framework only SilverStripe website, however I have been unable to correctly set up the routing for it.
I want to have a single controller handling a handful of URLs. I want it to handle an empty URL too, i.e. '/'.
I've been unable to get my controller to differentiate between the different urls.
My routes are as follows:
---
Name: rootroutes
---
Director:
rules:
'$Action/$ID/$OtherID': 'MainController'
'': 'MainController'
and my controller:
class MainController extends Controller {
private static $url_handlers = array(
'$Action//$ID/$OtherID' => 'handleAction',
);
public function index() {
return "index";
}
public function login() {
return "login";
}
public function handleAction($request, $action) {
var_dump($action); // always 'index'
if($this->hasMethod($action)) {
return $this->$action();
}
}
}
You need to define the $allowed_actions array on your controller before actions other than index() will work.
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());
}
}
}
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().
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();
}
}
}
I have a boolean variable(0, 1) in my database and I want to filter it to a word 0 for 'NO', and 1 for 'Yes'. how can I do that in a twig template
I want something like {{ bool_var | '??' }} where the '??' is the filter
Quick way to achieve that is to use the ternary operator:
{{ bool_var ? 'Yes':'No' }}
http://twig.sensiolabs.org/doc/templates.html#other-operators
You could also create a custom filter that would do this. Read about custom TWIG extensions - http://symfony.com/doc/current/cookbook/templating/twig_extension.html
To build on what #dmnptr said in his last paragraph, in your app bundle, create a /Twig folder and create an AppExtension class inside.
class AppExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('boolean', array($this, 'booleanFilter')),
);
}
public function booleanFilter($value)
{
if ($value) {
return "Yes";
} else {
return "No";
}
}
public function getName()
{
return 'app_extension';
}
}
Then, in your bundle's Resources/config/ folder, add the following to your services.yml where class is the class of the new class:
app.twig_extension:
class: [YourAppBundleNamespace]\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
The filter will be available in Twig by simply appending a |boolean to any variable.
Or even better you could make a boolean to string transformer and add it to your form.
It might be 'more' code but the upside is reusability. You wouldn't have to make your templates dirty with logic and you could reuse it to all the forms you want :)
Pros:
Not tied to the form component so you can still use it.
Use it anywhere, more functionality than a twig extension.
No need to mess with twig or symfony configuration.
Can use it in forms themselves.
Documentation:
http://symfony.com/doc/current/cookbook/form/data_transformers.html
Example from:
Symfony2 Forms BooleanToStringTransformer Issue
<?php
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class BooleanToStringTransformer implements DataTransformerInterface
{
private $trueValue;
private $falseValue;
public function __construct($trueValue, $falseValue)
{
$this->trueValue = $trueValue;
$this->falseValue = $falseValue;
}
public function transform($value)
{
if (null === $value) {
return null;
}
if (!is_bool($value)) {
throw new TransformationFailedException('Expected a Boolean.');
}
return true === $value ? $this->trueValue : $this->falseValue;
}
public function reverseTransform($value)
{
if (null === $value) {
return null;
}
if (!is_string($value)) {
throw new TransformationFailedException('Expected a string.');
}
return $this->trueValue === $value;
}
}