add Tab via DataExtension Silverstripe3 - silverstripe

I was wondering if it is possible to add a Tab via a DataExtension? The FieldList argument passed to updateCMSFields appears to output a DataExtensions new fields onto the Details Tab. So my first attempt was to push my Fields there:
public function updateCMSFields(FieldList $fields) {
$secureFilesTab = $fields;
$secureFilesTab->push(new HeaderField(_t('SecureFiles.GROUPACCESSTITLE', 'Group Access')));
$secureFilesTab->push(new TreeMultiselectField('GroupPermissions', _t('SecureFiles.GROUPACCESSFIELD', 'Group Access Permissions')));
}
This works fine, but when I save a value, the CMS loads the data from the Tree_View and List_View tabs onto the Details Tab. Mentioned in comments below is the fact that this is a DataExtension for Folder.
I then tried using code from the FormScaffolder to add a new Tab:
public function UpdateCMSFields(FieldList $fields) {
$fields->push(new TabSet('Root', $secureFilesTab = new Tab('Security')));
$secureFilesTab->setTitle(_t('SiteTree.TABSECURITY', 'Security'));
}
This changes the tab icons all to the Tree_View icons and places my new DataExtension fields on all tabs.
What is the proper way to add tabs via a DataExtension?

You can use addFieldsToTab() to add new fields to an existing tab or a new one, as you would when using getCMSFields() on DataObjects.
public function updateCMSFields(FieldList $fields) {
if ($this->owner->ClassName != 'Folder' ){
$fields->addFieldsToTab('Root.Security', array(
new HeaderField(_t('SecureFiles.GROUPACCESSTITLE', 'Group Access')),
new TreeMultiselectField('GroupPermissions', _t('SecureFiles.GROUPACCESSFIELD', 'Group Access Permissions'))
));
}
if ($this->owner->ClassName == 'Folder' ){
$fields->push(new HeaderField(_t('IMAFOLDER', 'I am a folder')));
}
}
The Security tab, if not found, will be created by passing 'Root.Security' as the first argument. The dot notation is used to create the nested structure of tabset and tabs. It is not possible to simple push a new tabset with the same name ("Root"), as there's already such a tabset. If you need to do further manipulations on your tabs, you can access their instance using $fields->findOrMakeTab('TabSet.Tab'), ie $fields->findOrMakeTab('Root.Content');

I also had problem to enable tabs in Member extension, in function
public function updateCMSFields(FieldList $fields)
finally it started to work after I removed fields order
$fields->changeFieldOrder($order);
try it, maybe it helps you.

Related

Silverstripe 4 - Adding a FormAction via getCMSFields

Goal:
I have a DataObject called "Event". This is in a managed_model for "EventsAdmin" (extending ModelAdmin). When editing an Event, I want a tab on the record called "Moderation" that has a few fields and two buttons: "Approve" and "Reject". These two buttons call an action each that performs relevant actions.
Event extends DataObject
public function getCMSFields() {
$fields = parent::getCMSFields();
$eventStatus = $fields->dataFieldByName("EventStatus")
->setTitle('Current Status')
->setDisabled(true);
$approveButton = FormAction::create('doApproveEvent', _t('SiteBlockAdmin.Approve', 'Approve'))
->setUseButtonTag(true)
->addExtraClass('btn-outline-success font-icon-check-mark-circle');
$rejectButton = FormAction::create('doRejectEvent', _t('SiteBlockAdmin.Reject', 'Reject'))
->setUseButtonTag(true)
->addExtraClass('btn-outline-danger font-icon-cancel-circled');
$fields->addFieldsToTab('Root.Moderation', [
$eventStatus,
$approveButton,
$rejectButton
]);
return $fields;
}
This displays the buttons just fine. But they don't do anything. So I am trying to work out how they can plug into action methods doApproveEvent and doRejectEvent (And where they should go)
I did find docs that led me to adding the buttons to the action bar at the bottom of the CMS page via updateFormActions(). But this isn't what I want as the other fields I am adding above the buttons are part of the Approve/Reject process. Here is the code for this method. This works fine barring the buttons are not in a logical place for the process I'm trying to create.
class CMSActionButtonExtension extends DataExtension
{
public function updateFormActions(FieldList $actions)
{
$record = $this->owner->getRecord();
if (!$record instanceof Event || !$record->exists()) {
return;
}
$approveButton = FormAction::create('doApproveEvent', _t('SiteBlockAdmin.Approve', 'Approve'))
->setUseButtonTag(true)
->addExtraClass('btn-outline-success font-icon-check-mark-circle');
$rejectButton = FormAction::create('doRejectEvent', _t('SiteBlockAdmin.Reject', 'Reject'))
->setUseButtonTag(true)
->addExtraClass('btn-outline-danger font-icon-cancel-circled');
$actions->push($approveButton);
$actions->push($rejectButton);
}
public function doApproveEvent($data, $form) {
$record = $this->owner->getRecord();
// Approve logic
}
public function doRejectEvent($data, $form) {
$record = $this->owner->getRecord();
// Reject logic
}
}
The above Extension is attached to GridFieldDetailForm_ItemRequest
extension.yml
SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest:
extensions:
- My\Namespace\CMSActionButtonExtension
Interestingly, if I have both sets of buttons on the page at the same time, the updateFormActions option works while my desired option still doesn't. Despite the buttons being of identical markup and sitting inside the exact same form tag. I assume that has something to do with how Silverstripe loads the main content panel and the DOM.
Any thoughts on achieving this? Anyone seen a button added to the main CMS panel in a module that I could take a look at? I found this post from 5 years ago, but it's for SS3 and the answer doesn't work for me.
Short answer:
you have to add custom FormActions through an Extension on the Controller that controls the form (or on the form itself
Long Answer:
A bit of background on how SilverStripe does forms:
Generally speaking, forms are always served through Controllers/RequestHandlers (they need to be accessible on some route, usually that's an Action on a Controller that is often named Form, EditForm, ItemEditoForm, ...).
Fields
Inside the CMS you rarely ever have to create your own form, that's done by the CMSs built in Controllers/RequestHandlers for the admin area (GridFieldDetailForm_ItemRequest in this case).
Basically (pseudo code here), what those controllers do is:
public function EditForm() {
$fields = $myCurrentlyEditingDataObject->getCMSFields();
$actions = ...;
$validator = ...;
$this->updateFormActions(&$actions);
$form = new Form('ItemRequestForm', $fields, $actions, $validator);
$this->updateItemEditForm(&$form); // or $this->updateEditForm()
return $form;
}
So, getCMSFields() and in some cases getCMSActions()/getCMSValidator() (not sure if those 2 are still used in SilverStripe 4.x), you can add things to the form, without ever seeing the form object.
Also, the getCMSFields() will always be put into the ``` section of the Form, that's why your button is somewhere in the middle with all the fields and not with the other actions.
Submission
When a form is submitted (eg to /admin/pages/edit/EditForm/265/field/NameOfMyGridField/item/542/ItemEditForm), it will call the action GridFieldDetailForm_ItemRequest->ItemEditForm() which returns the Form object where subsequently FormRequestHandler->httpSubmission() is called. This will then look at the submitted data to figure out what action was clicked (eg $_REQUEST['action_doApproveEvent']) and try to find that action.
The way it tries to find that, is checking if it itself has a method called doApproveEvent, if that fails, it will try Form->getController()->doApproveEvent() or something like that. In the case of a GridField, that controller is GridFieldDetailForm_ItemRequest which means it will try to call GridFieldDetailForm_ItemRequest->doApproveEvent()
So, that means DataObject->getCMSFields() lets you easily add FormFields (and FormActions) into your form body.
But it does not provide a means of adding a method to handle the submission.
That's why, for custom actions you need to modify the Controller (GridFieldDetailForm_ItemRequest in this case).
You are doing this by creating a Extension which you attached to GridFieldDetailForm_ItemRequest.
Any method in your Extension is added to the thing it's attached to, so if you add a method called updateFormActions, it will kind of become GridFieldDetailForm_ItemRequest->updateFormActions().
And if you recall from earlier, the controller will call $this->updateFormActions() during the creation of the form.
Additionally, as I explained earlier, when a FormAction is named doApproveEvent it will look for a GridFieldDetailForm_ItemRequest->doApproveEvent(), which now exists because you added it through that Extension.
So, in summary: you have to add custom FormActions through an Extension on the Controller that controls the form (or on the form itself
PS: the old post from
bummzack you linked to worked in 3.x, because the Controller in his example that created the form was an instance of LeftAndMain.

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

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.

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 3.0:Add Sub-tabs under settings->main tab keeping the default fields in a separate tab

I have added new Sub Tabs Under The Settings->Main Tab in The Silverstripe 3.0 Back-End using the following code:
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Main', new TabSet('Tab1'),'Theme');
$fields->addFieldToTab('Root.Main.Tab1', new Tab('SubTab1',new HTMLEditorField("Text", "Text")));
$fields->addFieldToTab('Root.Main.Tab1', new Tab('SubTab2', new DropdownField("DropDown1","DropDown",
array(
'-1' => 'Option1',
'1' => 'Option2',
))));
}
But The Main Tab already has a field named theme under it which is generated by default. I need to keep this field separated from the tabs which I have added...maybe creating a new sub-tab for it...Please tell me how I can achieve this...Currently it appears under every tab...
You could remove the default 'Theme' field altogether if you'd like.
Put this line before all the code you posted:
$fields->removeByName('Theme');

Resources