In SilverStripe I have a event dataobject
class Event extends DataObject {
private static $db = array(
'Name' => 'Text'
);
private static $has_many = array(
'EventDates' => 'EventDate'
that has many event dates:
class EventDate extends DataObject {
private static $db = array(
'Day' => 'Date',
'StartTime' => 'Varchar',
'EndTime' => 'Varchar'
);
Now if I query all events on a page like
Event::get()
I want them to be sorted by the earliest date.
Which way is the best to achieve this?
You can define a static property on the DataObject class with a default sort rule:
<?php
class EventDate extends DataObject {
…
// This also effects the default sorting of `has_many` related Events.
private static $default_sort = "\"Day\" ASC";
}
?>
Now
<?php
// first we pick a Event object, for instance by a (given) $id
$event = Event::get()->byId($id);
// because the default sort ist Day the attached `EventDate` will now be sorted by `Day` if you don't specify otherwise
$events = $event->EventDates();
// will return the earliest Event (of course you don't filter events here which are in the past).
$firstEvent = $events->First();
// if you then want to sort differently at some place, you can call ->sort() on the list:
$event = Event::get()->byId($id);
$events = $event->EventDates()->sort('LastEdited', 'DESC');
$lastEditedEvent = $events->First();
?>
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 have tried:
removeFieldFromTab
removeByName
replaceField
But the field persists.
use SilverStripe\ORM\DataObject;
use //.....
class Product extends DataObject {
private static $db = [
'ProductName'=>'Varchar',
'TagLine'=>'Text',
'GeneralDescription'=>'HTMLText'
];
private static $has_one = [
'SplashImage'=>Image::Class,
'ProductCategory'=>ProductCategory::Class
];
private static $has_many = [
'ProductImage'=>Image::Class,
'Features'=>'Feature'
];
private static $owns = [
'SplashImage',
'ProductImage'
];
private static $summary_fields = array(
'ProductName'=>'Product Name'
);
private static $searchable_fields = [
];
public function getCMSFields(){
$fields = parent::getCMSFields();
$categoryField = DropdownField::create('ProductCategory', 'Choose Product Category', ProductCategory::get()->map('ID', 'ProductCategoryTitle'));
$fields->replaceField('ProductCategory', $categoryField);
return $fields;
}
}
I am not getting any errors but the default drop down field that has the id #'s is at the top.
With has_one relations the field name should be <RelationName>ID, so in your case ProductCategoryID.
You have to reference the append ID to the relation name in order for SilverStripe to remove the field from the CMS tab by name.
$fields->removeByName('ProductCategoryID');
Also, if you create a custom field for a has_one relation, make sure you use <RelationName>ID as the name for the field. Eg. to create a Dropdown, use:
DropdownField::create(
'ProductCategoryID', // Use RelationName + ID
'Choose Product Category',
ProductCategory::get()->map('ID', 'ProductCategoryTitle')
);
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 a DataObject called ContentSection that has 2 has_one relationships: to a page type LandingPage and another DataObject Person.
class ContentSection extends DataObject {
protected static $has_one = array(
'Person' => 'Person',
'LandingPage' => 'LandingPage'
);
}
Both LandingPage and Person define a has_many relationship to ContentSection.
class LandingPage extends Page {
protected static $has_many = array(
'ContentSections' => 'ContentSection'
);
}
class Person extends DataObject {
protected static $has_many = array(
'ContentSections' => 'ContentSection'
);
}
ContentSections are editable through the LandingPage and Person with the GridFieldConfig_RelationEditor e.g.:
function getCMSFields() {
$fields = parent::getCMSFields();
$config = GridFieldConfig_RelationEditor::create(10);
$fields->addFieldToTab('Root.Content', new GridField('ContentSections', 'Content Sections', $this->ContentSections(), $config));
return $fields;
}
My question is how can you hide/remove irrelevant has_one fields in the CMS editor tab? The Person and LandingPage relationship dropdown fields both display when you are editing a ContentSection, whether it is for a Person or LandingPage. I only want to show the relevant has_one relationship field. I've tried using dot notation on the has_many relationships:
class Person extends DataObject {
protected static $has_many = array(
'ContentSections' => 'ContentSection.Person'
);
}
I've also tried using the removeFieldFromTab method in the getCMSFields method of the ContentSection class, where I define the other CMS fields for the ContentSection:
$fields->removeFieldFromTab('Root.Main', 'Person');
Instead of removeFieldFromTab use the removeByName function. removeFieldFromTab will not work if there is no 'Root.Main' tab.
Also we remove PersonID, not Person. has_one variables have ID appended to the end of their variable name.
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('PersonID');
$fields->removeByName('LandingPageID');
return $fields;
}
If you would like to selectively hide or display these fields you can put in some if statements in your getCMSFields function.
function getCMSFields() {
$fields = parent::getCMSFields();
if (!$this->PersonID) {
$fields->removeByName('PersonID');
}
if (!$this->LandingPageID) {
$fields->removeByName('LandingPageID');
}
return $fields;
}