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');
}
Related
I am trying to use a Has_many relation as the summary fields for a DataObject and can't seem to get it working.
Basically:
I have a Form
each form has many submissions/entries
each form has many fields
Each field has many answers.
I'm trying to create a gridfield in the back end admin area of each form which displays the entries for each form.
In the summary fields for the entry, i'd like to display the Date created, and the first 3 fields for that form.
So, for example if we had a form with a name, email, and phone field the summary fields would be as follows:
Date Created
Name
Email
Phone
with the relevent entry data/responses as summary information for the entry.
Here is what I have so far. This is the form:
<?php
class ContactBlock extends Block {
private static $db = array(
// Fields for the form/block go here
);
private static $has_many = array(
'ContactBlockFields' => 'ContactBlockField',
'ContactBlockEntries' => 'ContactBlockEntry'
);
public function getCMSFields() {
// Irrelevant code goes here!
// CONTACT BLOCK ENTRIES
$entriesInfo = new GridFieldDataColumns();
$entriesInfo->setDisplayFields(singleton('ContactBlockEntry')->summaryFields());
$entriesConfig = GridFieldConfig::create();
$entriesConfig->addComponents(
new GridFieldToolbarHeader(),
new GridFieldAddNewButton('toolbar-header-right'),
$entriesInfo,
new GridFieldSortableHeader(),
new GridFieldPaginator(50),
new GridFieldEditButton(),
new GridFieldDeleteAction(),
new GridFieldDetailForm()
);
$entriesGrid = GridField::create('ContactBlockEntries', 'Form Entries', $this->ContactBlockEntries(), $entriesConfig);
$fields->addFieldToTab('Root.FormEntries', $entriesGrid);
return $fields;
}
}
This is the Entry DataObject:
<?php
class ContactBlockEntry extends DataObject {
private static $has_one = array(
'ContactBlock' => 'ContactBlock'
);
private static $has_many = array(
'ContactBlockFieldAnswers' => 'ContactBlockFieldAnswer',
);
private static $many_many = array(
'FormFields' => 'ContactBlockField'
);
static $summary_fields = array(
'Date'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
//=== REMOVE FIELDS ====
$fields->removeFieldFromTab('Root','FormFields');
$fields->removeFieldFromTab('Root','ContactBlockFieldAnswers');
$fields->removeFieldFromTab('Root.Main','ContactBlockID');
//=== REMOVE FIELDS ====
return $fields;
}
public function onBeforeDelete() {
parent::onBeforeDelete();
// Delete answers that are associated with this block.
$data = ContactBlockFieldAnswer::get()
->filter('ContactBlockEntry', $this->ID);
foreach( $data as $d) {
$d->delete();
}
}
public function getDate() {
$date = date('d/m/Y',strtotime($this->Created));
return $date;
}
}
This is the field code:
<?php
class ContactBlockField extends DataObject {
private static $db = array(
'SortOrder' => 'Int',
'FieldName' => 'Varchar',
'FieldType' => 'Varchar',
'DropdownValues' => 'Varchar(255)',
'Label' => 'Varchar',
'Placeholder' => 'Varchar',
'Required' => 'Boolean'
);
private static $has_one = array(
'ContactBlock' => 'ContactBlock',
);
private static $has_many = array(
'ContactBlockFieldAnswer' => 'ContactBlockFieldAnswer',
);
private static $belongs_many_many = array(
'Entries' => 'ContactBlockEntry'
);
static $searchable_fields = array(
'FieldType',
'FieldLabel',
'Required'
);
static $summary_fields = array(
'FieldType' => 'Field Type',
'Label' => 'Field Label',
'Required' => 'Required Field?'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
// Unrelated stuff here
return $fields;
}
}
I can't seem to figure out how to get the column labels, and their relevant data showing on the gridfield. Any help or advice would be much appreciated.
UPDATE 24/3/17:
OK I've got a little further with this. On the ContactBlockEntry DataObject, after implementing the changes suggested by UncleCheese, I have discovered the following:
public function getFirstField() {
return $this->FormFields()->first();
}
public function getSecondField() {
return $this->FormFields()->offsetGet(1);
}
public function getThirdField() {
return $this->FormFields()->offsetGet(2);
}
public function summaryFields() {
return [
'Date' => 'Submitted',
'Answers.First.Value' => $this->getFirstField()->Label,
'Answers.Second.Value' => $this->getSecondField()->Label,
'Answers.Third.Value' => $this->getThirdField()->Label,
];
}
$this->getFirstField()->Label is returning [Notice] Trying to get property of non-object however, when I echo/print this function in getCMSFields() I can see the label value.
Answers.First.Value works. It returns the value/answer submitted in the first field. The problem is, I can't seem to get the second and third values, as I can't figure out the method to retrieve them. I tried offsetGet() and it said the method isn't available.
In this case, you can define a summaryFields() method in lieu of the static array. This will allow you to return a computed value for the headings.
What complicates things is getting the values. The first three fields are not properties of the Entry object, so you'll need to provide getters for the Entry dataobject to compute them on the fly as "virtual" properties. It's a bit clunky, but something like this should work.
public function getFirstField()
{
return $this->FormFields()->first();
}
public function getSecondField()
{
return $this->FormFields()->offsetGet(1);
}
public function getThirdField()
{
return $this->FormFields()->offsetGet(2);
}
public function summaryFields()
{
return [
'Created' => 'Created',
'FirstField.Answer' => $this->getFirstField()->FieldName,
'SecondField.Answer' => $this->getSecondField()->FieldName,
'ThirdField.Answer' => $this->getThirdField()->FieldName,
];
}
I'm not too sure about your data model, though. You have the answers has a has_many on your field object. You would in that case have to create another getter on your Field object for AnswerLabel that somehow concatenated all the answers into a string, or maybe just got the first one. Whatever business logic you choose. Just use FirstField.AnswerLabel etc. in your array, or whatever you choose to call that method. The point being, you need to resolve a plural relationship into a single readable value. How you do that is up to you.
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',
);
}
I got a many to many relationship between Products and Categories using Silverstripes' ORM. All is working except when I try to add a Category for a product the choices for Category is shown by user id instead of Category name. I tried to map ID to name but did not work. Below is what I tried, what am I missing?
class Product extends DataObject {
private static $db = array(
'ProductName' => 'Varchar(32)',
);
private static $many_many = array (
'Category' => 'Category'
);
}
class Category extends DataObject {
private static $db = array(
'Category' => 'Varchar(32)',
);
public function searchableFields() {
return array (
'Category' => array (
'filter' => 'ExactMatchFilter',
'title' => 'Category',
'field' => 'TextField'->setSource(
$this::get()->map('ID','Category')
)
)
);
}
private static $belongs_many_many = array (
'Product' => 'Product'
);
}
When Silverstripe needs to display a shorthand for a DataObject, it will call the getTitle() method on that DataObject.
getTitle() will first check if the DataObject has a Title field and return that value if it does.
If your DataObject doesn't have Title field, it will try searching for a Name field.
If it can't find a Name field either, it will default to returning the ID of your DataObject, which is probably what is happening for you.
How to fix your specific example
There's 2 ways you can fix your specific example:
Rename your DataObjects DB fields to Name or Title
Override the getTitle() method on your 2 DataObjects
Solution #1
class Product extends DataObject {
private static $db = array(
'Name' => 'Varchar(32)',
);
private static $many_many = array (
'Category' => 'Category'
);
}
class Category extends DataObject {
private static $db = array(
'Name' => 'Varchar(32)',
);
public function searchableFields() {
return array (
'Category' => array (
'filter' => 'ExactMatchFilter',
'title' => 'Category',
'field' => 'TextField'->setSource(
$this::get()->map('ID','Category')
)
)
);
}
private static $belongs_many_many = array (
'Product' => 'Product'
);
}
Solution #2
class Product extends DataObject {
private static $db = array(
'ProductName' => 'Varchar(32)',
);
private static $many_many = array (
'Category' => 'Category' );
public function getTitle() {
return $this->ProductName;
}
}
class Category extends DataObject {
private static $db = array(
'Category' => 'Varchar(32)',
);
public function searchableFields() {
return array (
'Category' => array (
'filter' => 'ExactMatchFilter',
'title' => 'Category',
'field' => 'TextField'->setSource(
$this::get()->map('ID','Category')
)
)
); }
private static $belongs_many_many = array (
'Product' => 'Product' );
public function getTitle() {
return $this->Category;
}
}
Which solution is best?
In your specific case, I would go with solution #1, because your DB fields are functionally name/title fields.
I would use solution #2 if my DataObject's title needs to use many fields. Let's say you a have Person DataObject with a first name and a last name:
public function getTitle() {
return $this->FirstName . ' ' . $this->LastName;
}
I've created a DataObject called Service and I've got a many_many relationship to RelatedServices as follows:
class Service extends DataObject {
private static $db = array (
'Name' => 'Varchar',
'Description' => 'Varchar',
);
private static $many_many = array (
'RelatedServices' => 'RelatedService'
);
public function getCMSFields() {
$fields = FieldList::create(TabSet::create('Root'));
$services = $this->get()->where("\"Service\".\"Name\" != '$this->Name'")->map('ID', 'Name')->toArray();
$fields->addFieldsToTab('Root.Main', array(
TextField::create('Name'),
TextField::create('Description'),
ListBoxField::create('RelatedServices', 'Related services')->setMultiple(true)->setSource($services)
));
return $fields;
}
}
and:
class RelatedService extends DataObject {
private static $db = array (
'Name' => 'Varchar',
);
private static $belongs_many_many = array (
'RelatedServices' => 'RelatedService'
);
}
This is being used in a ModelAdmin and the service works right including the related services text area, however it doesn't save. I did it previously that it was in a seperate tab in the CMS and had RelatedService have and admin section which looking through the DB looked like it worked however I thought it was an unnecessary section so tried to make it all in one and now no longer saves to the DB.
You don't need the 'RelatedService' class, as you are referencing to the class you are working in. So relating to the class 'Service' itself would make more sense.
The reason why your code won't work is because you have got your relations mixed up.
class Service extends DataObject{
private static $db = array (
'Name' => 'Varchar',
'Description' => 'Varchar',
);
private static $many_many = array (
'RelatedServices' => 'Service'
);
private static $belongs_many_many = array (
'ParentServices' => 'Service'
);
public function getCMSFields() {
$fields = FieldList::create(TabSet::create('Root'));
$services = $this->get()->where("\"Service\".\"Name\" != '$this->Name'")->map('ID', 'Name')->toArray();
$fields->addFieldsToTab('Root.Main', array(
TextField::create('Name'),
TextField::create('Description'),
ListBoxField::create('RelatedServices', 'Related services')->setMultiple(true)->setSource($services)
));
return $fields;
}
}
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.