I am trying to translate a DataObject using the _t() method.
I have been using it on Pages without a problem, but it doesn't seem to work on data objects.
class SliderItem extends DataObject {
private static $default_sort = 'Sort';
private static $db = array(
'Sort' => 'Int',
'Title' => 'Varchar(255)',
'Summary' => 'Text'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', array(
TextField::create(_t('SliderItem.Title', 'Title')),
TextareaField::create(_t('SliderItem.Summary', 'Summary'))
));
$fields->removeByName(array('HomePageID', 'Sort'));
return $fields;
}
}
mysite/lang/de.yml
de:
SliderItem:
Title: 'I want to change this'
Summary: 'And this..'
Why does this example not work?
Make sure to define the variable name in the FormField constructor as the first parameter and the translatable title as the second parameter.
This:
TextField::create(_t('SliderItem.Title', 'Title'))
Should be this:
TextField::create('Title', _t('SliderItem.Title', 'Title'))
Your getCMSFields function should look something like this:
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Main', array(
TextField::create('Title', _t('SliderItem.Title', 'Title')),
TextareaField::create('Summary', _t('SliderItem.Summary', 'Summary'))
));
$fields->removeByName(array('HomePageID', 'Sort'));
return $fields;
}
Related
I have relation between BlogArticle and BlogCategory many_many & belongs_many_many. I would like to add CheckboxSetField or ListBoxField to cmsFields on BlogArticle, which contains BlogCategories.
Following code shows correct checkboxes in cms, but from some reason it doesn't store the values:
class BlogCategory extends DataObject
{
private static $db = [
'Title' => 'Varchar(255)'
];
private static $belongs_many_many = [
'BlogArticles' => BlogArticle::class
];
}
class BlogArticle extends Page
{
private static $many_many = [
"BlogCategories" => BlogCategory::class,
];
public function getCMSFields()
{
$fields = parent::getCMSFields();
$field = CheckboxSetField::create(
'BlogCategories',
'Categories',
BlogCategory::get()
);
$fields->add($field);
return $fields;
}
}
Any ideas what's wrong? Thanks a lot!
On your BlogArticle.php you aren’t referencing the relationship so it can’t save.
So BlogCategory::get() should be $this->BlogCategories() - you will probably have to map() the values aswell.
There’s an example of using the checkbox field with a $many_many here: https://www.silverstripe.org/learn/lessons/v4/working-with-data-relationships-many-many-1
I am looking to make a DataObject class which injects fields into a Page object but I'm having a bit of trouble doing so. The only solutions I've found so far require me to do something within the Page (other than adding in a relationship between the two classes), I'm not all that certain as to what I'm doing wrong...
What I need would be for the DataObject to add a tab and some fields within Page and have the data for that Page to save to the DataObjects table.
In a sense I would like them to be somewhat independent of each other, so that I can link it to Article now and then some other page types at some later date.
This is what I have so far :
Article.php
class Article extends Page {
private static $description = 'An article page for writing and posting content';
private static $has_many = array(
'MyExtraFields' => 'MyExtraFields'
);
}
The DataObject
class MyExtraFields extends DataObject {
private static $db = array(
'ExtraText' => 'Varchar(255)',
'ExtraWYSIWYG' => 'HTMLText'
);
private static $has_many = array(
'Article' => 'Article'
);
private static $summary_fields = array(
'ExtraText' => 'ExtraText',
'ExtraWYSIWYG' => 'ExtraWYSIWYG'
);
public function updateCMSFields(){
$fields = parent::getCMSFields();
$fields->addFieldsToTab('Root.Content.Translation', array(
TextField::create('ExtraText'),
HTMLEditorField::create('ExtraWYSIWYG')
)
);
return $fields;
}
}
If I understand it correctly, you want to be able to add the DataObject MyExtraField to the Pagetype Article. If the MyExtraField is dependent on the Article, then you need to change the Relation of the ExtraField to has_one like jberculo says:
class MyExtraField extends DataObject {
...
private static $has_one = array(
'Article' => 'Article'
);
}
Then you have to add a GridField to the Article's CMS view manage the DataObject:
class Article extends Page {
...
private static $has_many = array(
'MyExtraFields' => 'MyExtraField'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridfield = new GridField( "MyExtraFields", "MyExtraField", $this->MyExtraField(), $gridFieldConfig );
$fields->addFieldToTab( 'Root.ExtraFields', $gridfield );
return $fields;
}
If you want to reuse the DataObject in different Articles, you need to use a many to many relation and a different GridField-Configuration:
$gridFieldConfig = GridFieldConfig_RelationEditor::create();
You will find the documentation of GridField here:
https://docs.silverstripe.org/en/3.4/developer_guides/forms/field_types/gridfield
If you want to manage has_one relation to be able to add just one optional instance of MyExtraField, you could use:
https://github.com/burnbright/silverstripe-hasonefield
I want to be able to associate team members to a project using the ListBoxField . I have a ProjectHolder which has Project pages as it's children. I also have a TeamHolder which has TeamPage's as its children.
I want to be able to save multiple team members in the ListBoxField and then loop through them on a Project page. I also want to be able to link to the team members pages. e.g
<% loop $TeamMemberNames %>
$Name
<% end_loop %>
My Current code:
TeamPage.php
class TeamPage extends Page
{
private static $db = array(
'Name' => 'Varchar(255)',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
return $fields;
}
}
Project.php
class Project extends Page
{
private static $db = array(
'Name' => 'Varchar(255)',
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'TeamPage',
'Select Team Members for project',
TeamPage::get()->map("ID", "Name")->toArray()
)->setMultiple(true));
return $fields;
}
}
Screenshot:
I can pull through the names from the TeamPage Object into the ListBoxField however after selecting the names I need a way to save the multiple values and also get the Link so I can link to the appropriate team members pages that are listed.
To save data to the database we must first define a relationship to the data in our class. In this case we want to create a $many_many relationship between Project and TeamPage.
In the Project class we add a $many_many relationship to TeamPage. Here, the relationship name is TeamPages.
When creating the ListBoxField we pass the TeamPages relationship name so the field knows where to save this data:
Project.php
class Project extends Page
{
private static $db = array(
'Name' => 'Varchar(255)'
);
private static $many_many = array(
'TeamPages' => 'TeamPage'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'TeamPages',
'Select Team Members for project',
TeamPage::get()->map('ID', 'Name')->toArray()
)->setMultiple(true));
return $fields;
}
}
Now in our template we can loop through a Project's $TeamPages by calling the following:
Template
<% loop $TeamPages %>
$Name
<% end_loop %>
If we would like a TeamPage to be able to access it's related Projects we can add a $belongs_many_many to the TeamPage class to point back to the Project class. You can also add a ListBoxField to control Projects from the TeamPage.
TeamPage.php
class TeamPage extends Page
{
private static $db = array(
'Name' => 'Varchar(255)'
);
private static $belongs_many_many = array(
'Projects' => 'Project'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', TextField::create('Name'));
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'Projects',
'Select project for this team page',
Project::get()->map('ID', 'Name')->toArray()
)->setMultiple(true));
return $fields;
}
}
Something to note is TeamPage and Project both extend Page. This means both classes inherit a Title field. I would suggest using Title instead of Name, unless you have a specific reason to do so.
This would make the code:
Project.php
class Project extends Page
{
private static $many_many = array(
'TeamPages' => 'TeamPage'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.TeamMembers', ListBoxField::create(
'TeamPages',
'Select Team Members for project',
TeamPage::get()->map('ID', 'Title')->toArray()
)->setMultiple(true));
return $fields;
}
}
TeamPage.php
class TeamPage extends Page
{
private static $belongs_many_many = array(
'Projects' => 'Project'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Projects', ListBoxField::create(
'Projects',
'Select project for this team page',
Project::get()->map('ID', 'Title')->toArray()
)->setMultiple(true));
return $fields;
}
}
Template
<% loop $TeamPages %>
$Title
<% end_loop %>
I have 'Resouce' dataobject as below with attachment as has_one relation. I would like to delete the attachment when 'resource' object is deleted.
But what I get is Fatal error: Call to a member function delete() on a non-object in
<?php
class Resource extends DataObject
{
private static $db = array (
'Name' => 'Varchar(200)',
'Description' => 'Text',
'Category' => "Enum('Data, Drafts, Drawings, Reports, Images, Other')",
'SortOrder' => 'Int'
);
private static $has_one = array (
'Attachment' => 'File',
'ResourcePage' => 'ResourcePage'
);
public function onBeforeDelete()
{
$myAttachment = $this->Attachment();
$file = DataObject::get_by_id('File', $myAttachment->ID); //we have to make sure it is a Dataobject object
$file->delete();
$file->destroy();
return parent::onBeforeDelete();
}
}
The problem here is your assumption that DataObject::get_by_id always returns an object is incorrect. You could check that $file is a non-false value first, or just do everything through the has_one getter, using:
public function onBeforeDelete() {
if ($this->Attachment()->exists()) {
$this->Attachment()->delete();
}
}
I'm using silverstripe version 3.1
I tried to add to each DataObject a translation. For that I've used the following module:
https://github.com/bummzack/translatable-dataobject
I would like to have a modeladmin which uses a dataobject in 3 languages. (German, French and English - 3 Tabs)
private static $db = array(
'Title' => 'Varchar(200)'
);
public function getCMSFields() {
$fields = new FieldList();
$fields->add($this->getTranslatableTabSet());
$fields->removeByName("URLSegment");
return $fields;
}
How can I add
new TextField('Title', 'Titel *', '', 20)
in order to be able to define for example the limit of characters of 20?
This example didn't work for me. Only a blank site is shown in the CMS:
public function getCMSFields() {
$fields = new FieldList(
new TextField('Title', 'Titel *', '', 20)
);
$fields->add($this->getTranslatableTabSet());
$fields->removeByName("URLSegment");
return $fields;
}
Any ideas what I did wrong?
Thank you