Trying to remove/hide fields in custom tab in SilverStripe - php

I am trying to figure out a way (if possible) to remove or hide certain fields in a custom tab. The custom tab is labeled "Rotator" and it holds images that can be used for a rotating banner on a page. The home page banner is a little different in that it has 2 extra fields that aren't needed on the subpages: BackgroundImage and Body(which is meant to hold a variety of text). I want to make things simple for the content manager, so I want to hide these fields on the subpages.
I am aware of removeFieldFromTab and how it works, and I was thinking of using it on the Page.php file (since that is basically the main template for all page types in my SilverStripe file):
public function getCMSFields() {
$fields = parent::getCMSFields();
$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->addComponent(new GridFieldBulkImageUpload());
$gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
// field from drawer class => label in UI
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail',
'InternalURL.Link' => 'Internal URL',
));
$gridfield = new GridField(
"Rotator",
"Rotator",
$this->Rotator()->sort("SortOrder"),
$gridFieldConfig
);
$fields->addFieldToTab('Root.Rotator', $gridfield);
$fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");
$fields->addFieldToTab("Root.Main", new TextField("Subheader"), "Content");
$fields->addFieldToTab('Root.Main', new TextField('PageTitle', 'Page Title'), 'MetaDescription');
$fields->removeFieldFromTab('Root.Rotator', 'Body');
$fields->removeFieldFromTab('Root.Rotator', 'BackgroundImage');
return $fields;
}
Here is the code for the Rotator class:
<?php
class RotatorImage extends DataObject {
public static $db = array(
'SortOrder' => 'Int',
'Header' => 'varchar',
'Body' => 'HTMLText',
);
// One-to-one relationship with gallery page
public static $has_one = array(
'Image' => 'Image',
'BackgroundImage' => 'Image',
'Page' => 'Page',
'InternalURL' => 'SiteTree',
);
// tidy up the CMS by not showing these fields
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeFieldFromTab("Root.Main","PageID");
$fields->removeFieldFromTab("Root.Main","SortOrder");
return $fields;
}
// Tell the datagrid what fields to show in the table
public static $summary_fields = array(
'ID' => 'ID',
'Title' => 'Title',
'Thumbnail' => 'Thumbnail',
'InternalURLID' => 'Internal URL',
);
// this function creates the thumnail for the summary fields to use
public function getThumbnail() {
return $this->Image()->CMSThumbnail();
}
public function canEdit() {
return true;
}
public function canDelete() {
return true;
}
public function canCreate(){
return true;
}
public function canPublish(){
return true;
}
public function canView(){
return true;
}
}
However this does not work, and I am sure that I have the fields names correct. I tried 'Root.Rotator.Main' and 'Root.Rotator.Content' just to see what would happen and those also did not work. What am I missing? Is it possible to hide fields on a custom tab this way, or do I need to try something else?

well, you want to hide the fields in the gridfield detail form? that cannot be done in your pages getCMSFields(), as the grid is responsible for generating the detail form. Two possible solutions:
1) tell the grid to hide that fields with a custom component. I dunno how to do it
2) tell your Rotator class to show the fields ONLY if the related page is a homepage:
public function getCMSFields() {
$fields = parent::getCMSFields();
//...other stuff....
$isOnHomePage = ($this->Page() && $this->Page()->ClassName == 'HomePage'); //put in your own classname or conditions
if(!$isOnHomePage) {
//remove the fields if you're not on the HomePage
$fields->removeByName('Body');
//we need to suffix with "ID" when we have a has_one relation!
$fields->removeByName('BackGroundImageID');
}
return $fields;
}

This will work...
$fields->removeByName('FieldName');

Related

How to change order of items in SilverStripe GridField

I use a GridField in SilverStripe to create HTML section items that are rendered on a page. This works so far but it always displays the sections in the order that I added them to the CMS or rather by the ID it gets when it's created.
So my question is: How can i change that order. I don't want to manually change the IDs but would rather do a simple drag and drop.
Edit: Could the use of Elemental be a solution to this problem?
Screenshot of the CMS view
The Page:
class HomePage extends Page
{
private static $has_many = [
'SectionObjects' => SectionObject::class
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Sections',
$grid = GridField::create('SectionObjects', 'Sections', $this->SectionObjects(), GridFieldConfig_RecordEditor::create())
);
$config = $grid->getConfig();
$dataColumns = $config->getComponentByType(GridFieldDataColumns::class);
$dataColumns->setDisplayFields([
'ID' => 'ID',
'LastEdited' => 'Changed'
]);
return $fields;
}
}
The Section object
class SectionObject extends DataObject{
private static $db = [
'Content' => 'Text',
'BgColor' => Color::class
];
private static $has_one = [
'HomePage' => HomePage::class
];
public function getCMSFields(){
return new FieldList(
TextareaField::create('Content','SectionContent'),
ColorField::create('BgColor', 'Hintergrundfarbe')
);
}
}
Definitely using the elemental module would allow you to have the kind of behaviour you want - but if you don't want to go to the effort of refactoring your code to comply with that module, you could use either the gridfieldextensions or sortablegridfield module to allow you to sort the gridfield (with drag and drop) to your heart's content.

TreeDropdownField Silverstripe 4 Navigation Labels

I work on a SilverStripe 4 project where I use the TreeDropdownfield. In 3.6 it got the MenuTitle (Navigation Label) by default, but I noticed that in SilverStripe the default page titles are being displayed.
Since my customer changed the page titles the TreeDropdownField shows long page titles. I would like to display the Navigation labels instead of those long page titles because the structure isn't clear with those long titles.
I have the following code:
<?php
use SilverStripe\ORM\DataObject;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TreeDropdownField;
class InternalLink extends DataObject {
private static $db = [
'Title' => 'Varchar',
];
private static $has_one = [
'LinkTarget' => SiteTree::class,
'InternalLinkCategory' => 'InternalLinkCategory'
];
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', TextField::create( 'Title', 'Titel link' ) );
$fields->addFieldToTab('Root.Main', TreeDropdownField::create( 'LinkTargetID', 'Doeladres', SiteTree::class ) );
return $fields;
}
}
you can use TreeDropdownField::setTitleField(). It sets the field to use for item title.
$treeField = TreeDropdownField::create('LinkTargetID', 'Doeladres', SiteTree::class);
$treeField->setTitleField('MenuTitle');
$fields->addFieldToTab('Root.Main', $treeField);

silverstripe-translatable: how to make dataobjects translatable (slideshow)?

I am creating a multilingual Website in Silverstripe, the goal is to have the whole content available in three languages. I am using the translatable module, works fine for now.
Some pages contains a slideshow, meaning that I am associating a bunch of images and captions to these pages. Looks like this:
class Slide extends DataObject {
private static $db = array(
'Caption' => 'Varchar(255)',
'SortIndex' => 'Int'
);
private static $has_one = array(
'ParentPage' => 'Page',
'Image' => 'Image'
);
public static $default_sort = 'SortIndex';
public function getCMSFields() {
// parent::getCMSFields() does all the hard work and creates the fields for Title, IsActive and Content.
$fields = parent::getCMSFields();
$fields->dataFieldByName('Caption')->setTitle('Titel');
$fields->dataFieldByName('Image')->setTitle('Bild');
$fields->dataFieldByName('Index')->setTitle('Reihenfolge');
$fields->push(new TextField('Caption', 'Titel'));
$fields->push(new UploadField('Image', 'Profile Image'));
// Apply Translatable modifications
$this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
return $fields;
}
}
class SlideShowPage extends Page {
private static $has_many = array(
'Slides' => 'Slide'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$conf = GridFieldConfig_RelationEditor::create(10);
$conf->addComponent(new GridFieldSortableRows('SortIndex'));
$fields->addFieldToTab('Root.Slideshow', new GridField('Slide', 'Slides', $this->Slides(), $conf));
// Apply Translatable modifications
$this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
return $fields;
}
}
I want to make these slideshows translatable as well. Meaning that I want to prepare the slide show once in the primary language, then hit the "create new translation" button, and get the slideshow available in the translated version as well, with the captions ready to be translated (similar as the main text content). According to the silverstripe-translatable docs this shall be possible. I have added these calls to applyTranslatableFieldsUpdate() (see in the code above) and added these lines to my _config.php file as well:
SlideShowPage::add_extension('Translatable');
Slide::add_extension('Translatable');
The table Slide_translationgroups is even created successfully and filled with new entries for each translation. But the content of table Slide is not copied.
What am I missing?
Thanks!
Sacher

Background Image UploadField doesn't show in the CMS

On a SilverStripe CMS website I have a custom footer add-on code that I'd like to extend so I can upload a background image in the CMS. The code seems fine, but the added field is no where to be found in the CMS.
This is my code:
class CustomFooter extends DataExtension {
static $db = array(
'FooterContent' => 'HTMLText'
);
public static $has_one = array(
'Logo' => 'Image',
'BGImage' => 'Background Image'
);
public function getCMSFields() {
$this->extend('updateCMSFields', $fields);
return $fields;
}
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab('Root.Footer', new HTMLEditorField('FooterContent', 'Footer Content'));
$fields->addFieldToTab('Root.Main', new UploadField('Logo', 'Company Logo'));
$fields->addFieldToTab('Root.Main', new UploadField('BGImage', 'Background image'));
}
}
Weirdly enough, both image upload fields don't show where as the Content tab show and functions as expected.
Why aren't the UploadFields displaying?
Your
public static $has_one = array(
"Logo"=>"Image",
"BGImage"=>"Background Image"
);
looks pretty weird. The $has_onearray has the relation name as key and the class name of the relation as value, I doubt a classname with space in the name is allowed. So I'd try something like
public static $has_one = array(
"Logo"=>"Image",
"BGImage"=>"Image"
);
then run a dev/build?flush and check in the database if your table has a LogoID column and a BGImageIDcolumn for the has one relations.
The field generation looks ok, it should work as is.

How to get Silverstripe TableField to save records

I'm trying to use TableField to manage a list of related links to a page. I have the control working, except it isn't saving the id to the db (a hidden field), just the title and url
Is this the right way to be using it? think so. I've been using this: http://doc.silverstripe.org/sapphire/en/reference/tablefield as a guide
some code:
static $has_many = array (
'Linketys' => 'Linkety',
);
$myTableField = new TableField(
'MyTableField', // fieldName
'Linkety', // sourceType
array(
'Title'=>'Title',
'URL'=>'URL'
), // fieldList
array(
'Title'=>'TextField',
'URL'=>'TextField'
), // fieldTypes
null, // filterField (legacy)
"Linkety.PageID",
$this->ID
);
// add some HiddenFields thats saved with each new row
$myTableField->setExtraData(array(
'PageID' => $this->ID ? $this->ID : '$RecordID'
));
$fields->addFieldToTab("Root.Content.Options", $myTableField);
Hmm, the documentation seems to be a bit off.
This is how I use table fields, not using extraData
Linkety.php
class Linkety extends DataObject {
public static $db = array(
'Title'=>'Varchar',
'URLSegment'=>'Varchar',
);
public static $has_one = array(
'Page' => 'Page'
);
}
In Page.php
$myTableField = new TableField(
'Linkety',
'Linkety',
array('Title'=>'Link title', 'URLSegment'=>'URL'),
array('Title'=>'TextField','URLSegment'=>'TextField'),
'PageID',
$this->ID,
$editExisting=true
);
$fields->addFieldToTab("Root.Content.Options", $myTableField);

Categories