Skip to content

Instantly share code, notes, and snippets.

@KevinM2k
Created January 5, 2019 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevinM2k/c2a2e4215ed7c8cbac390601a94960b3 to your computer and use it in GitHub Desktop.
Save KevinM2k/c2a2e4215ed7c8cbac390601a94960b3 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Control\Controller;
class Event extends DataObject
{
private static $summary_fields = [
'Title' => 'Title of Event',
'Description' => 'Event Details'
];
private static $has_one = [
'EventsPage' => EventsPage::class
];
private static $versioned_gridfield_extensions = true;
private static $extensions = [
Versioned::class,
];
private static $db = [
'Title' => 'Varchar',
'Description' => 'Text',
];
private static $searchable_fields = [
'Title'
];
public function getCMSFields()
{
$fields = FieldList::create(
TextField::create('Title'),
TextareaField::create('Description')
);
return $fields;
}
public function Link()
{
return $this->EventsPage()->Link('events/show/'.$this->ID);
}
public function LinkingMode()
{
return Controller::curr()->getRequest()->param('ID') == $this->ID ? 'current' : 'link';
}
}
<?php
namespace App;
use SilverStripe\Admin\ModelAdmin;
class EventAdmin extends ModelAdmin {
private static $menu_title = 'Events';
private static $url_segment = 'events';
private static $managed_models = [
Event::class
];
}
<?php
namespace App;
use SilverStripe\Control\HTTPRequest;
class EventsPage extends \Page {
private static $has_many = [
'Events' => Event::class,
];
private static $owns = [
'Events'
];
}
class EventsPageController extends \PageController {
public function LatestEvents()
{
return Event::get()
->limit(6);
}
private static $allowed_actions = [
'show'
];
public function show(HTTPRequest $request)
{
$event = Event::get()->byID($request->param('ID'));
if(!$event) {
return $this->httpError(404,'That event could not be found');
}
return [
'Event' => $event
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment