How to put an Elemental field under a tab in admin CMS form - silverstripe

Starting out with the Elemental module for Silverstripe 4 and by default it lists the Elemental area(s) under the Main "Content" tab. I'd like to put them under their own tab.
How do I do that in my Page class getCMSField function?
What I have is:
A specific page (ElementPage) for using the module
ElementPage:
extensions:
- DNADesign\Elemental\Extensions\ElementalPageExtension
In ElementPage.php I have two $has_one like this:
private static $has_one = [
'LeftElemental' => ElementalArea::class,
'RightElemental' => ElementalArea::class
];
Those work fine, fields display and can render them in the template.
Trying to put them under their own tab, the getCMSFields:
public function getCMSFields()
{
$fields = parent::getCMSFields();
// To remove the default added one
$fields->removeByName('ElementalArea');
$fields->addFieldToTab('Root.LeftContentBlocks', ElementalArea::create('LeftElementalID'));
return $fields;
}
Resulting error:
[User Warning] DataObject::__construct passed The value
'LeftElementalID'. It's supposed to be passed an array, taken straight
from the database. Perhaps you should use DataList::create()->First();
instead?
I didn't really expect that to work but I can't see the create signature it needs.
EDIT:
This seems to get it done:
public function getCMSFields()
{
$fields = parent::getCMSFields();
// To remove the default added one
$fields->removeByName('ElementalArea');
$fields->addFieldToTab('Root.LeftContentBlocks', ElementalAreaField::create('LeftElemental', $this->LeftElemental(), $this->getElementalTypes()));
$fields->addFieldToTab('Root.RightContentBlocks', ElementalAreaField::create('RightElemental', $this->RightElemental(), $this->getElementalTypes()));
return $fields;
}
I'm not entirely sure $this->getElementalTypes() is what I should be doing. Any improvements/corrections are welcomed.

Related

Silverstripe 4 save variable in database

Is posible to save SS template variable in database from CMS and after execute it in template?
Okay lets see example:
In CMS i have settings where i put social media links and contact informatios.
Also in CMS i have module where i create HTML block-s which after that i loop in website.
In that html block i want to put existing $SiteConfig.Email variable.
I Try that but that is rendered in template like $SiteConfig.Email not show real email?
Is this posible to do or i need some extra modification?
Check photo
The question you have written makes no sense to me, but I understand the screenshot.
So, SilverStripe renders .ss files with a class called SSViewer. Basically it reads the file as string and then runs it through SSViewer to generate the HTML output.
But, as you saw, the output of variables is not processed.
I can think of 3 ways to get what you want:
Run the variables through SSViewer aswell (in this example, use $RenderedHTMLContent in the template)
class MyDataObject extends DataObject {
private static array $db = [
'Title' => DBVarchar::class,
'HTMLContent' => DBText::class,
];
public function Foobar() { return "hello from foobar"; }
public function RenderedHTMLContent() {
$template = \SilverStripe\View\SSViewer::fromString($this->HTMLContent);
// using $this->renderWith() will allow you access to all things of $this in the template. so eg $ID, $Title or $Foobar. Probably also $SiteConfig because it's global
return $this->renderWith($template);
// if you want to add extra variables that are not part of $this, you can also do:
return $this->renderWith($template, ["ExtraVariable" => "Hello from extra variable"]);
// if you do not want $this, you can do:
return (new ArrayData(["MyVariable" => "my value"]))->renderWith($template);
}
}
Please be aware of the security implications this thing brings. SilverStripe is purposely built to not allow content authors to write template files. A content author can not only call the currently scoped object but also all global template variables. This includes $SiteConfig, $List, .... Therefore a "bad" content author can write a template like <% loop $List('SilverStripe\Security\Member') %>$ID $FirstName $LastName $Email $Salt $Password<% end_loop %> or perhaps might access methods that have file access. So only do this if you trust your content authors
Use shortcodes instead of variables. But I never liked shortcodes, so I don't remember how they work. You'll have to lookup the docs for that.
Build your own mini template system with str_replace.
class MyDataObject extends DataObject {
private static array $db = [
'Title' => DBVarchar::class,
'HTMLContent' => DBText::class,
];
public function Foobar() { return "hello from foobar"; }
public function RenderedHTMLContent() {
return str_replace(
[
'$SiteConfig.Title',
'$SiteConfig.Tagline',
'$Title',
'$Foobar',
],
[
SiteConfig::current_site_config()->Title,
SiteConfig::current_site_config()->Tagline,
$this->Title,
$this->Foobar(),
],
$this->HTMLContent
);
}
}

How can I allow the user to set a custom global value in the silverstripe CMS?

I would like to be able to set custom values in the CMS, such as with the site name and tagline. I can't currently find any way of doing this other than on individual pages.
You can do so by extending SiteConfig. Your Extension might look like this:
class CustomSiteConfig extends DataExtension
{
private static $db = array(
'CustomContent' => 'Varchar(255)'
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab('Root.Main',
TextField::create('CustomContent', 'Custom content')
);
}
}
Then you need to apply the extension to SiteConfig. Add the following to mysite/_config/config.yml
SiteConfig:
extensions:
- CustomSiteConfig
And that's it. Run dev/build and your new field should be editable in the CMS as well as accessible in the Template using: $SiteConfig.CustomContent

Custom fields not appearing

I am trying to add some custom fields to my page in the CMS, but the fields are not appearing.
I have run dev/build but they are still not appearing.
Why are my custom fields not appearing in the CMS?
<?php
class FieldPage extends Page {
private static $db = array (
'Field1' => 'Varchar(32)',
'Field2' => 'Varchar(32)'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', TextField::create('Field1', 'Field 1'));
$fields->addFieldToTab('Root.Main', TextField::create('Field2', 'Field 2'));
return $fields;
}
}
class FieldPage_Contoller extends Page_Controller {
}
Well, did you also flush on or before dev/build? Did dev/build end with the message "Database build completed!"? Scroll down at the very bottom of that page!
Can you confirm that in your database a new table with your classname (FieldPage in your example) with the database fields inside is created?
If yes, you still have to add a "FieldPage" to your CMS by hitting the "add new page" button. Then the fields should appear.
dev/build will break if you have any php errors in your code, then the database isn't changed for your need. The code pasted looks ok, but it could be a php error in any other class. So check if dev/build ends with the correct message.

decluttering UI, order Extension gets applied

A few years ago I made a SilverStripe website and added too many fields to Page.php. I'm reworking some of this at the moment but cannot afford do reinvent the Project - now on SilverStripe 3.1.10.
I thought to declutter the UI for Page Sub-Classes, that do not need all the inherited fields, with a few Extensions.
An example how this extension could look
class NoClutter extends Extension {
public function updateCMSFields(FieldList $fields) {
$fields->removeFieldFromTab("Root.Main", "MenuTitle");
$fields->removeFieldFromTab("Root.Main", "Workflow");
}
}
config.yml
RedirectorPage:
extensions:
- NoClutter
This works on all classes for fields added in SiteTree (such as the MenuTitle field), but not for fields added in Page (such as the Workflow field). If the Extension is on UserDefinedForm, Workflow is also removed. But it does not work if the extension is on RedirectorPage. MenuTitle on the other hand is removed in both classes. My guess it's about order. My project is After: 'framework/','cms/' and hope I can make an extension like NoClutter work within the project.
How can I achieve this or how else could I work around the problem?
You need to add $this->extend('updateCMSFields', $fields) at the end of your Page getCMSFields() function.
class Page extends SiteTree {
// ...
public function getCMSFields() {
// call updateCMSFields after adding your fields
SiteTree::disableCMSFieldsExtensions();
$fields = parent::getCMSFields();
SiteTree::enableCMSFieldsExtensions();
// ...
$this->extend('updateCMSFields', $fields);
return $fields;
}
}
$this->extend('updateCMSFields', $fields) declares where your code updateCMSFields() function will get called.
The problem you are having is updateCMSFields() is getting called before you add your custom fields in the Page getCMSFields() function. So you are trying to remove the Workflow field before it is added. This is because the updateCMSFields extension hook is declared in the parent SiteTree getCMSFields() function.
UserDefinedForm solves this by calling $this->extend('updateCMSFields', $fields) at the bottom of its getCMSFields(). SiteTree::disableCMSFieldsExtensions() is required before parent::getCMSFields() is called for the extension hook to work.

Silverstripe tumblr-like Post Types

I am trying to create a back-end interface for silverstripe that gives the CMS user the option to choose between a set of Post Types (like tumblr) in Silverstripe3. So they can choose to create a News Post, Video Post, Gallery Post, etc.
I initially started off giving all Posts the necessary fields for each Type and adding an enum field that allowed the user to choose the Post Type. I then used the forTemplate method to set the template dependent upon which Post Type was chosen.
class Post extends DataObject {
static $db = array(
'Title' => 'Varchar(255),
'Entry' => 'HTMLText',
'Type' => 'enum('Video, Photo, Gallery, Music')
);
static $many_many = array(
'Videos' => 'SiteVideo',
'Photos' => 'SitePhoto,
'Songs' => 'SiteMp3'
);
public function forTemplate() {
switch ($this->Type) {
case 'Video':
return $this->renderWith('VideoPost');
break;
case 'Photo':
return $this->renderWith('ImagePost');
break;
etc...
}
function getCMSFields($params=null) {
$fields = parent::getCMSFields($params);
...
$videosField = new GridField(
'Videos',
'Videos',
$this->Videos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
$photosField = new GridField(
'Photos',
'Photos',
$this->Photos()->sort('SortOrder'),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Videos', $photosField);
return $fields;
}
}
I would rather the user be able to choose the Post Type in the backend and only the appropriate tabs show up. So if you choose Video, only the Video GridField tab would show up. If you choose Photo Type only the Photo's GridField would show.Then I would like to be able to call something like
public function PostList() {
Posts::get()
}
and be able to output all PostTypes sorted by date.
Does anyone know how this might be accomplished? Thanks.
Well the first part can be accomplished using javascript. Check out this tutorial and the docs let me know if you have questions on it.
The second part would be trickier but I think you could do something with the page controller. Include a method that outputs a different template based on the enum value but you would have to set links somewhere.
I managed this with DataObjectManager in 2.4.7 as I had numerous DataObjects and all were included in one page but I'm not sure if that is feasible in SS3.
return $this->renderWith(array('CustomTemplate'));
This line of code will output the page using a different template. You need to include it in a method and then call that method when the appropriate link is clicked.

Resources