Silverstripe 4 getCMSFields_forPopup and GridField - php

Picking this up again after many years. Can I not use gridfield within the cms popup component? Here I have Ingredient entity and am wanting to add Ingredients from the db to a Recipe entity. Even a simple one doesn't appear.
Recipe.php
...
private static $db = [
'Title' => 'Varchar',
'Description' => 'Text',
];
private static $has_one = [];
private static $many_many = [
'Ingredients' => Ingredient::class,
];
public function getCMSFields_forPopup()
{
$gridConfig = GridFieldConfig_RelationEditor::create()->addComponents(
new GridFieldDeleteAction('unlinkrelation')
);
$grid = GridField::create(
'Ingredients',
'Ingredients',
$this->Ingredients(),
$gridConfig,
);
$fields = FieldList::create(
TextField::create('Title'),
TextareaField::create('Description'),
$grid
);
// or maybe something like..
// $fields->addFieldToTab('Main', 'Ingredients', 'Ingredients', $grid);
return $fields;
}

getCMSFields_forPopup does not exist in Silverstripe 4 or Silverstripe 3. This was in Silverstripe 2.
Try getCMSFields instead.
public function getCMSFields()
{
$fields = parent::getCMSFields();
$ingredientsFieldConfig = GridFieldConfig_RelationEditor::create();
$ingredientsField = GridField::create(
'Ingredients',
'Ingredients',
$this->Ingredients(),
$ingredientsFieldConfig
);
$fields->addFieldToTab('Root.Main', $ingredientsFieldConfig);
return $fields;
}

Related

How to make fields required in CMS

I try to make fields required in the CMS:
class Documents extends DataObject {
private static $db = array(
'DocType' => 'Text',
'DocTitle' => 'Text',
'DocNumber' => 'Text'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields = FieldList::create(TabSet::create('Root'));
$fields->addFieldsToTab('Root.Main', array(
DropdownField::create('DocType','Document Type'),
DropdownField::create('DocStatus','Document Status'),
TextField::create('DocNumber','Document Number'),
...
RequiredFields::create(array('DocType','DocTitle','DocNumber'));
));
return $fields;
}
But in my SilverStripe error log I get the following:
"Uncaught Exeption: the method 'getname' doesn't exist on RequiredFields or the method is not public".
How do I make fields required in the SilverStripe CMS?
In the CMS we can declare required fields by declaring a getCMSValidator function and returning RequiredFields:
public function getCMSValidator()
{
return RequiredFields::create(
'DocType',
'DocTitle',
'DocNumber'
);
}

has_many and has_one relation SilverStripe

I followed the tutorial on https://www.silverstripe.org/learn/lessons/working-with-data-relationships-has-many?ref=hub to create some featured items for my homepage. But somehow I missed one piece, because I get this error
[Error] Uncaught Exception: No has_one found on class 'HomePageFeatured', the has_many relation from 'HomePage' to 'HomePageFeatured' requires a has_one on 'HomePageFeatured'
HomePage.php
<?php
/**
* Defines the HomePage page type
*/
class HomePage extends Page {
// private static $db = array(
// );
// private static $has_one = array(
// );
private static $has_many = array (
'Featured' => 'HomePageFeatured'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Featured', GridField::create(
'Featured',
'Hervorgehobene Produkte',
$this->Featured(),
GridFieldConfig_RecordEditor::create()
));
return $fields;
}
private static $icon = "themes/hstheme/images/treeicons/home";
}
class HomePage_Controller extends Page_Controller {
}
HomePageFeatured.php
<?php
/**
* Holds the featured items from the Homepage
*/
class HomePageFeatured extends DataObject {
private static $db = array(
'Title' => 'Varchar',
'Description' => 'Text'
);
private static $has_one = array(
'Photo' => 'Image',
'HomePageFeatured' => 'HomePageFeatured'
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Title'),
TextareaField::create('Description'),
$uploader = UploadField::create('Photo')
);
$uploader->setFolderName('featured-photos');
$uploader->getValidator()->setAllowedExtensions(array('png','gif','jpeg','jpg'));
return $fields;
}
}
As I understand, the problem is the $has_one from HomePageFeatured.php. But it has a reference from HomePageFeatured.
HomePageFeatured needs a has_one of 'HomePage'
class HomePageFeatured extends DataObject {
private static $has_one = array(
'Photo' => 'Image',
'Parent' => 'HomePage',
);
}

SiverStripe 3: the method 'fortemplate' does not exist on 'File'

I'm creating a GridField for my page with a has_many relationship to my DataObjects but after adding data and saving the object it breaks the page in the CMS. I can't figure out why or find any answers after searching. Here is the error and my code:
[User Error] Uncaught Exception: Object->__call(): the method 'fortemplate' does not exist on 'File'
ProductPage.php
class ProductPage extends Page {
// Contact object's fields
public static $db = array(
'ProductPrice' => 'Text',
'ProductSubTitle' => 'Text',
'ProductModelNumber' => 'Text',
'ProductReleaseDate' => 'Date',
'AudioCode' =>'HTMLText',
// 'VideoCode' =>'HTMLText',
'ProductSold' =>'Boolean',
'NotAvailable' =>'Boolean',
'LimitedEdition' =>'Boolean',
'OneOff' =>'Boolean',
'Discontinued' =>'Boolean',
'DealerOnly' =>'Boolean',
'ComingSoon' =>'Boolean'
);
// One-to-one relationship with profile picture and contact list page
public static $has_one = array(
'ProductImage' => 'Image',
'ProductDownload' => 'File'
);
// One to many relationship with Contact object
public static $has_many = array(
'Videos' => 'Video',
'FirmwareDownloads' => 'FirmwareDownload'
);
private static $can_be_root = false;
private static $allowed_children = 'none';
private static $default_parent = 'Shop';
private static $description = 'Product for the Shop page';
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Audio', new TextAreaField('AudioCode','SoundClound Embed Code</br>Visit SoundCloud'));
// $fields->addFieldToTab('Root.Video', new TextAreaField('VideoCode','YouTube Embed Code</br>Visit YouTube'));
$fields->addFieldToTab('Root.Main', new TextField('ProductPrice','Price'),'Content');
$fields->addFieldToTab('Root.Main', new TextField('ProductSubTitle'),'Content');
$fields->addFieldToTab('Root.Main', new TextField('ProductModelNumber'),'Content');
$fields->addFieldToTab('Root.Main', $productDate = new DateField('ProductReleaseDate'),'Content');
$productDate->setConfig('showcalendar', true);
$productDate->setConfig('dateformat', 'd-MMMM-yyyy');
$fields->addFieldToTab('Root.Main', $manualuploadField = new UploadField('ProductDownload','Manual - PDF'),'Content');
$manualuploadField->setFolderName('manuals');
$fields->addFieldToTab('Root.Main', new CheckboxField('ProductSold','Sold Out'),'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('DealerOnly'),'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('NotAvailable'),'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('LimitedEdition'),'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('OneOff'),'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('Discontinued'),'Content');
$fields->addFieldToTab('Root.Main', new CheckboxField('ComingSoon'),'Content');
$fields->addFieldToTab('Root.Main', $uploadField = new UploadField('ProductImage','Featured Image'),'Title');
// $uploadField->setFolderName('.$parent->URLSegment'.$this->URLSegment);
$fields->renameField('Content','Product Description');
$fields->renameField('Title','Name');
$fields->renameField('NavigationLabel','Title');
$fields->fieldByName('Root.Main')->setTitle('Product Details');
$VideosGridField = new GridField(
'Videos',
'Videos',
$this->Videos(),
GridFieldConfig::create()
->addComponent(new GridFieldToolbarHeader())
->addComponent(new GridFieldAddNewButton('toolbar-header-right'))
->addComponent(new GridFieldSortableHeader())
->addComponent(new GridFieldDataColumns())
->addComponent(new GridFieldPaginator(50))
->addComponent(new GridFieldEditButton())
->addComponent(new GridFieldDeleteAction())
->addComponent(new GridFieldDetailForm())
->addComponent(new GridFieldSortableRows('SortOrder'))
);
$fields->addFieldToTab("Root.Videos", $VideosGridField);
$FirmwareDownloadsGridField = new GridField(
'FirmwareDownloads',
'Firmware Downloads',
$this->FirmwareDownloads(),
GridFieldConfig::create()
->addComponent(new GridFieldToolbarHeader())
->addComponent(new GridFieldAddNewButton('toolbar-header-right'))
->addComponent(new GridFieldSortableHeader())
->addComponent(new GridFieldDataColumns())
->addComponent(new GridFieldPaginator(50))
->addComponent(new GridFieldEditButton())
->addComponent(new GridFieldDeleteAction())
->addComponent(new GridFieldDetailForm())
->addComponent(new GridFieldSortableRows('SortOrder'))
);
$fields->addFieldToTab("Root.FirmwareDownload", $FirmwareDownloadsGridField);
return $fields;
}
public function getParentTitle() {
$parent = $this->Parent();
if ($parent->Exists()) {
return $parent->Title;
}
return '';
}
public function getGrandParentTitle() {
$parent = $this->Parent();
if ($parent->Exists()) {
$grandParent = $parent->Parent();
if ($grandParent->Exists()) {
return $grandParent->Title;
}
}
return '';
}
public function getGrandGrandParentPayPal() {
$parent = $this->Parent();
if ($parent->Exists()) {
$grandParent = $parent->Parent();
if ($grandParent->Exists()) {
$grandgrandParent = $grandParent->Parent();
if ($grandgrandParent->Exists()) {
return $grandgrandParent->PayPalEmail;
}
}
}
return '';
}
}
class ProductPage_Controller extends Page_Controller {
public function init() {
parent::init();
Requirements::css('shop/css/shop.css');
// Requirements::javascript('gallery/javascript/jquery-1.7.1.min.js');
// Magnific Popup core JS file -->
Requirements::javascript('gallery/javascript/jquery.magnific-popup.js');
Requirements::javascript('shop/javascript/organictabs.jquery.js');
// Magnific Popup core CSS file -->
Requirements::css('gallery/css/magnific-popup.css');
Requirements::css('gallery/css/magnific-popup-main.css');
Requirements::css('shop/css/shop.css');
}
}
class ProductPage_Images extends DataObject {
static $db = array (
'PageID' => 'Int',
'ImageID' => 'Int',
'Caption' => 'Text',
'SortOrder' => 'Int'
);
}
FirmwareDownload.php
class FirmwareDownload extends DataObject {
// Contact object's fields
public static $db = array(
'FirmwDownloadTitle' => 'Varchar(255)',
'SortOrder' => 'Int'
);
public static $default_sort = array('SortOrder');
// One-to-one relationship with Video picture and contact list page
public static $has_one = array(
'FirmwDownloadFile' => 'File',
'ProductPage' => 'ProductPage'
);
// Create Summary fields
public static $summary_fields = array(
'FirmwDownloadTitle' =>'Firmware Download Title',
'FirmwDownloadFile' =>'Firmware Download File'
);
// renames the summary columns
static $field_labels = array(
'FirmwDownloadTitle' =>'Firmware Download Title',
'FirmwDownloadFile' =>'Firmware Download File'
);
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('FirmwDownloadTitle','Title'),
$uploader = UploadField::create('FirmwDownloadFile','EEPROM & Firmware File')
);
$uploader->setFolderName('firmware');
return $fields;
}
}
The problem is this line:
public static $summary_fields = array(
// ...
'FirmwDownloadFile' =>'Firmware Download File'
);
This will try to display the FirmwDownloadFile in the GridField, but FirmwDownloadFile is a File. GridField does not know how to display a File.
What we can do is display a variable of File such as the File.Name in our summary_fields:
private static $summary_fields = array(
// ...
'FirmwDownloadFile.Name'
);
private static $field_labels = array(
// ...
'FirmwDownloadFile.Name' =>'Firmware Download File'
);
This has to do with an update form SilverStripe.
Earlier versions:
private static $summary_fields = array(
'Field Name' => 'Model.Field'
);
Current version:
private static $summary_fields = array(
'Model.Field' => 'Field Name'
);
you need to switch the key/value pairs.

SilverStripe gridField Entries not visible for normal backend user

I have two user groups Administrator and Inhaltsautoren
My LandingPage has a Tab Teaser with a gridField. The normal user can not see the entries and i dont know why?
I cant find something for setting the permissions for Inhaltsautoren. Has someone an idea why there are no entries in the gridField?
Teaser.php
<?php
class Teaser extends DataObject {
private static $db = array (
'Title' => 'Varchar',
'Description' => 'HTMLText'
);
private static $has_one = array (
'Photo' => 'Image',
'Link' => 'Link'
);
private static $many_many = array(
'Tags' => 'Tag'
);
private static $summary_fields = array (
'GridThumbnail' => '',
'Title' => 'Titel',
'Description' => 'Beschreibung'
);
public function getGridThumbnail() {
if($this->Photo()->exists()) {
return $this->Photo()->SetWidth(100);
}
return "(no image)";
}
public function getCMSFields() {
$fields = FieldList::create(
TextField::create('Title'),
$tags = TagField::create('Tags','Tags',Tag::get(),$this->Tags()),
HTMLEditorField::create('Description', 'Beschreibung'),
LinkField::create('LinkID', 'Weiterleitung'),
$uploader = UploadField::create('Photo')
);
$tags->setShouldLazyLoad(true); // tags should be lazy loaded
$tags->setCanCreate(true); // new tag DataObjects can be created
$uploader->setFolderName('teaser');
$uploader->getValidator()->setAllowedExtensions(array('png','jpeg','jpg'));
return $fields;
}
}
and my LadingPage.php
$fields->addFieldToTab('Root.Teaser', $gridField = GridField::create(
'Teasers',
'Landing Page Teaser',
$this->Teasers(),
GridFieldConfig_RecordEditor::create()
));
$gridField->getConfig()->getComponentByType("GridFieldDataColumns")->setFieldCasting(array("Description"=>"HTMLText->BigSummary"));
Use canView() on your dataobject and check inside this function if your user is allowed to see this object or not.
public function canView($member = null) {
return Permission::check('ADMIN', 'any');
}

How to use GridList of one page into home page in shortcode in SilverStripe

I am using SilverStripe and I am stuck on one problem.
I have created a custom NewsPage page type and a News DataObject:
NewsPage
class NewsPage extends Page
{
private static $has_many = array(
'News' => 'News'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$gridFieldConfig = GridFieldConfig::create()->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
new GridFieldSortableHeader(),
new GridFieldDataColumns(),
new GridFieldPaginator(10),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm(),
new GridFieldBulkManager()
);
$gridField = new GridField(
'News',
'All News Entries',
$this->News(),
$gridFieldConfig
);
$fields->addFieldToTab('Root.News', $gridField);
return $fields;
}
}
class NewsPage_Controller extends Page_Controller
{
}
News
class News extends DataObject
{
private static $db = array(
'Title' => 'Text',
'Description' => 'HTMLText'
);
private static $has_one = array(
'NewsPage' => 'NewsPage',
'NewsImage' => 'Image'
);
private static $summary_fields = array(
'Title' => 'Title',
'NewsImage'=> 'NewsImage',
'Description'=> 'Description'
);
public function getCMSFields()
{
$fields = new FieldList(
new TextField('Title', 'Title'),
new UploadField('NewsImage', 'NewsImage'),
new HTMLEditorField('Description', 'Description')
);
return $fields;
}
}
Now I want to display latest news on my home page but I don't know how to display it using shortcodes.
Here is a screenshot of the CMS. Here you can see the news section I want to display on the home page with a shortcode.

Categories