Silverstripe 3 - extend sitetree and layout - silverstripe

I'm a newbie in SilverStripe. I need to create a new page by extending a new class sitetree. My question is how to retrieve in the template something like $Layou which is used in the classic page.php. For example, in my template folder I would like to have a folder like "Layout" organized for new pages created from this.
this is the controller:
class WhitePage extends SiteTree {
private static $db = array(
);
private static $has_one = array(
);
}
class WhitePage_Controller extends ContentController {
private static $allowed_actions = array(
);
public function init() {
parent::init();
}
function index() {
return $this->renderWith("WhitePage");
}
}
i would like in the template directory create a folder name "whitepage" within the ".ss" file, and in the template use something like $whitepage instate of $Layout ...
How to do this?
thk, a lot
Francesco

You can use the master WhitePage.ss template with several page types by extending your WhitePage class. Then you can use the $Layout as normal to call your custom layout template.
WhitePage
class WhitePage extends SiteTree {
}
class WhitePage_Controller extends ContentController {
private static $allowed_actions = array(
);
public function init() {
parent::init();
}
}
CustomWhitePage
class CustomWhitePage extends WhitePage {
}
class CustomWhitePage_Controller extends WhitePage_Controller {
private static $allowed_actions = array(
);
public function init() {
parent::init();
}
}
In your themes/mytheme folder or mysite folder create your templates like so:
templates/Page.ss
templates/WhitePage.ss
templates/Layout/Page.ss
templates/Layout/WhitePage.ss
templates/Layout/CustomWhitePage.ss
Your Layout/WhitePage.ss and Layout/CustomWhitePage.ss will use the templates/WhitePage.ss parent template while any page that extends Page will use templates/Page.ss.
Make sure you call ?flush=all for your templates to be loaded the first time through.

Related

Unable to customize filter/ search form of ModelAdmin in SilverStripe 4.4.4

I am working on a SilverStripe project. Basically, I updated my project to SilverStripe version 4.4.4. After the upgrade, I found out that the search/ filter forms of the ModelAdmin were changed as in the screenshot below.
What I am trying to do now is that I am trying to customize the fields of the search/ filter form of the ModelAdmin following this lesson. https://www.silverstripe.org/learn/lessons/v4/introduction-to-modeladmin-1.
I have a data object class that is linked to a model admin class. Following is the dummy code of the model admin class.
class EnquirySubmission extends DataObject
{
private static $db = [
//some hidden fields are here
];
private static $has_one = [
'Member' => Member::class
];
private static $summary_fields = [
'Member.Name' => 'Member',
//some hidden fields are here
];
//some hidden code goes here
public function searchableFields()
{
return [
'Member.Name' => [
'filter' => 'PartialMatchFilter',
'title' => 'Member',
'field' => \SilverStripe\Forms\DropdownField::create('Member.Name')
->setSource(
Member::get()->map('ID','Email')
)->setEmptyString('-- Member --')
],
];
}
}
As you can see in the code, I am customizing the filter/ search form by overriding the searchableFields method. But it does not work in the upgraded version of SilverStripe. What am I missing and how can I fix it?
Silverstripe and ModelAdmin are ace, but it is confusing why this date range search issue has required a tweak in every version so far. This is a complete example that I've just got working on 4.7.2 (latest stable at time of post)...
app/src/Test/MyDataObject.php
namespace MyVendor\MyNamespace;
use SilverStripe\Forms\DateField;
use SilverStripe\ORM\DataObject;
class MyDataObject extends DataObject {
private static $db = [
'Title' => 'Varchar',
'MyDateTimeField' => 'DBDatetime'
];
private static $summary_fields = ['Title','MyDateTimeField'];
public function updateAdminSearchFields($fields) {
$fields->removeByName('MyDateTimeField');//needed as added in summary field
$fields->push(DateField::create('MyDateTimeField:GreaterThanOrEqual', 'MyDateTimeField (Start)'));
$fields->push(DateField::create('MyDateTimeField:LessThanOrEqual', 'MyDateTimeField (End)'));
}
}
app/src/Test/MyAdmin.php
namespace MyVendor\MyNamespace;
use SilverStripe\Admin\ModelAdmin;
class MyAdmin extends ModelAdmin {
private static $menu_title = 'MyAdmin';
private static $url_segment = 'myadmin';
private static $managed_models = [MyDataObject::class];
public function getList() {
$list = parent::getList();
if ($params = $this->getRequest()->requestVar('filter'))
if ($filters = $params[$this->sanitiseClassName($this->modelClass)])
return $list->filter($filters);
return $list;
}
}
app/src/Test/MyAdminExtension.php
namespace MyVendor\MyNamespace;
use SilverStripe\ORM\DataExtension;
class MyAdminExtension extends DataExtension {
public function updateSearchContext($context) {
$class = $context->getQuery([])->dataClass();
if (method_exists($class, 'updateAdminSearchFields'))
(new $class)->updateAdminSearchFields($context->getFields());
return $context;
}
}
app/_config/mysite.yml
MyVendor\MyNamespace\MyAdmin:
extensions:
- MyVendor\MyNamespace\MyAdminExtension

Customize ModelAdmin listing in SilverStripe

Is it possible to change or add custom summary_fields into a listing from a ModelAdmin extends? Actually I'm able to filter a custom field named Type but I don't know how to customize the summary_fields. This is my actual code:
class Profiles3ModelAdmin extends ModelAdmin {
public static $menu_icon = 'mysite/images/peoples.png';
public static $managed_models = array('Member');
public static $url_segment = 'membres';
public static $menu_title = 'Membres';
public function getList() {
$group = Group::get()->filter('Code', array('Membres'))->first();
$list = $group->Members()->filter('Type', 1 );
return $list;
}
}
Your current query should be able to use $list = Member::get()->filter(array('Groups.Code' => 'Membres', 'Type' => 1)); if I recall correctly. Just gets it down to one line.
Usually to add to the summary you would add it to your class's model. So in this case on Member you would apply a DataExtension that had:
<?php
class MyMemberDataExtension extends DataExtension{
private static $summary_fields = array(
'Type'
);
}

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());
}
}
}

Silverstripe additional More options action

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();
}
}
}

Add new Page using GridField - creates the child in root folder

I would like to use GridField to view and create new child pages. Parent is DocumentHolder, child is Document. Both extend SiteTree. When I click to "Add Document" (button generated by grid), fill in the fields and confirm the form, the parent page is ignored and the page is created in root. It works well when I use DataObject. The code looks like this:
class DocumentHolder extends SiteTree
{
private static $allowed_children = array(
'Document'
);
private static $default_child = "Document";
public function getCMSFields()
{
$fields = parent::getCMSFields();
$gridField = new GridField('Documents', 'Documents', SiteTree::get('Document')->filter('ParentID', $this->ID), GridFieldConfig_RecordEditor::create());
$fields->addFieldToTab("Root.Uploads", $gridField);
return $fields;
}
}
class Document extends SiteTree
{
private static $db = array(
);
private static $has_one = array(
);
}
Thanks for help.
Since SiteTree already has a relationship to its Children pages set up, you may as well use it! Since allowed_children will only ever be documents, try this instead:
$gridField = new GridField('Documents', 'Documents', $this->Children(), GridFieldConfig_RecordEditor::create());
I ran into this problem earlier working on my holderpage module. You need to set the ParentID by default. Here's two strategies;
You can use populateDefaults on the child class. E.g.
class Document extends SiteTree
{
private static $default_parent = 'DocumentHolder';
private static $can_be_root = false;
public function populateDefaults(){
parent::populateDefaults();
$this->ParentID = DataObject::get_one(self::$default_parent)->ID;
}
...
Or you can manipulate the record in the gridfield with a custom GridFieldDetailForm implementation or via the updateItemEditForm callback.
<?php
class MyGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
public function ItemEditForm()
{
$form = parent::ItemEditForm();
if (! $this->record->exists() && $this->record->is_a('SiteTree')) {
$parent_page = $this->getController()->currentPage();
if ($parent_page && $parent_page->exists()) {
$this->record->ParentID = $parent_page->ID;
// update URLSegment #TODO perhaps more efficiently?
$field = $this->record->getCMSFields()->dataFieldByName('URLSegment');
$form->Fields()->replaceField('URLSegment', $field);
}
}
return $form;
}
}
This is more complicated although it allowed me to create an effortless module / addon ( https://github.com/briceburg/silverstripe-holderpage )

Resources