I followed the docs to create a custom report but keep failing to generate the report in the CMS. Has anyone else had this problem? I noticed that with older versions, the config had to include the report, but I see no sign of this in 3.1.
Here is the contents of CustomSideReport.php
class CustomSideReport_Day extends SideReport {
public function title() {
return "Event Calendar";
}
public function records() {
return Page::get()->sort("Title");
}
public function fieldsToShow() {
return array(
"Title" => array("NestedTitle", array("2"))
);
}
}
I have the done the usual dev/build and flush, but still nothing appears.
The documentation has now been updated to correctly show how to make custom site reports.
In SilverStripe 3.1 the class should extend SS_Report instead of SideReport.
Try this:
class CustomSideReport_Day extends SS_Report {
public function title() {
return 'Event Calendar';
}
public function sourceRecords($params = null) {
return Page::get()->sort('Title');
}
public function columns() {
$fields = array(
'Title' => 'Title'
);
return $fields;
}
}
Also note that records() has changed to sourceRecords($params = null) and fieldsToShow() has changed to columns().
Related
I have two models Post and Comment
Post modal :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'user_id','community_id','topic_id','title','description','slug','url', 'image_url', 'timezone', 'image', 'news_organization_id'];
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable')->whereNull('parent_id');
}
public function allComments()
{
return $this->hasMany('App\Models\Comment', 'commentable_id');
}
}
Comment modal :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['user_id', 'parent_id', 'text', 'commentable_id', 'commentable_type'];
public function user()
{
return $this->belongsTo(User::class);
}
public function commentable()
{
return $this->morphTo();
}
public function replies()
{
return $this->hasMany(Comment::class, 'parent_id');
}
}
I want to fetch all comments of post with nested comments and their count in graphql query.
I have tried with the below query, it gives me a count of comments and nested comments but the count is null for nested comments. Also, there is pagination in my project.
$comments = Comment::withCount('replies')->where('commentable_id', $post->id)->where('parent_id', null)->orderBy('created_at', 'desc')->paginate($limit, ['*'], 'page', $current_page);
$last_page = $comments->lastPage();
You can see the result in the below image
Like for Parent comment 1 => replies_count is 2, but for its Child comment 3 => replies_count is null, it has to be 1.
I've searched through the documentation and the API for SilverStripe 4 but I'm having trouble using the appropriate class to call the default exception handler.
How it worked before SilverStripe 4 within my Page.php controller's init()
parent::init();
set_exception_handler(function($exception) {
if( get_class($exception) == 'CustomException' ) {
Controller::curr()->handleCustomException($exception);
}
return exceptionHandler($exception);
});
How I expect it to work with SilverStripe 4
parent::init();
set_exception_handler(function($exception) {
if( get_class($exception) == 'CustomException' ) {
Controller::curr()->handleCustomException($exception);
}
return <SomeMonologClass>::handleException($exception);
});
I'm aware of SilverStripe 4 now using Monolog and I've come across the handleException function which I believe is what I need to call instead of exceptionHandler($exception).
Any advice would be much appreciated.
use SilverStripe\Control\Controller;
class MyController extends Controller
{
private static $dependencies = [
'logger' => '%$Psr\Log\LoggerInterface',
];
// This will be set automatically, as long as MyController is instantiated via Injector
public $logger;
protected function init()
{
$this->logger->debug("MyController::init() called");
parent::init();
}
}
As described here:
https://docs.silverstripe.org/en/4/developer_guides/debugging/error_handling/#accessing-the-logger-via-dependency-injection
And even more info here: https://www.silverstripe.org/learn/lessons/v4/advanced-environment-configuration-1
My project has a crossword. The client would like to upload JavaScript files and change the pattern.
How do I make the upload file and include the JavaScript in the front end of the website? The file is getting uploaded but it is not showing up in source code.
The following is for SilverStripe 3.1.
This example adds a JavaScript file upload to each page. In the Controller's init() function if a file has been uploaded we load it into the page:
Page.php
class Page extends SiteTree {
private static $has_one = array(
'CustomJavascriptFile' => 'File'
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$customJavascriptFile = UploadField::create('CustomJavascriptFile', 'Custom Javascript File');
$customJavascriptFile->setFolderName('javascript');
$customJavascriptFile->setAllowedExtensions(array('js'));
$fields->addFieldToTab('Root.Javascript', $customJavascriptFile);
return $fields;
}
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
if ($this->CustomJavascriptFileID) {
Requirements::javascript($this->CustomJavascriptFile()->RelativeLink());
}
}
}
This method gives us a custom JavaScript file per page. We could alternatively have the file uploaded to the Config Settings instead so we can upload one file that is then loaded for the whole site.
First we need to extend SiteConfig. We need to declare the extension in our config.yml:
mysite/_config/config.yml
---
Name: site
After: 'framework/*','cms/*'
---
SiteConfig:
extensions:
- CustomSiteConfig
CustomSiteConfig.php
class CustomSiteConfig extends DataExtension {
private static $has_one = array(
'CustomJavascriptFile' => 'File'
);
public function updateCMSFields(FieldList $fields) {
$customJavascriptFile = UploadField::create('CustomJavascriptFile', 'Custom Javascript File');
$customJavascriptFile->setFolderName('javascript');
$customJavascriptFile->setAllowedExtensions(array('js'));
$fields->addFieldToTab('Root.Javascript', $customJavascriptFile);
}
}
Page.php
class Page extends SiteTree {
}
class Page_Controller extends ContentController {
public function init() {
parent::init();
$siteConfig = SiteConfig::current_site_config();
if ($siteConfig->CustomJavascriptFileID) {
Requirements::javascript($siteConfig->CustomJavascriptFile()->RelativeLink());
}
}
}
Wondering if it was possible to add additional actions when editing a page to the More options menu in Silverstripe 3.1
Using the below sample, I have added an additional FormAction which I can see when editing an page. But where do I actually add the "custom_action" method?
class CustomPage extends SiteTree {
function getCMSActions(){
$actions = parent::getCMSActions();
$actions->addFieldToTab(
'ActionMenus.MoreOptions',
new FormAction('custom_action', 'Custom Action')
);
return $actions;
}
}
class CustomPage_Controller extends ContentController {
public function init() {
parent::init();
if(!Member::currentUserID()) {
die();
}
}
}
I get this error message when i create since i have created a simple twig filter. The message is not clear at all.
An exception has been thrown during the compilation of a template
("Warning: Illegal offset type in app/cache/dev/classes.php line
3342")
My filter:
class simpleFilter extends Twig_Extension
{
public function getFilters()
{
return array('simpleFilter' => new Twig_SimpleFilter($this, 'simpleFilter'));
}
public function simpleFilter($value)
{
return 'test'.$value;
}
public function getName()
{
return 'some_extension';
}
}
My config
my.twig.extension.simpleFilter:
class: Bundle\Twig\Filter\SimpleFilter
tags:
- { name: twig.extension }
Am i missing something?
Try to change your getFilters to look like this:
public function getFilters()
{
return array(
new Twig_SimpleFilter('simpleFilter', array($this, 'simpleFilter'))
);
}
I had to use Twig_Filter_Method
public function getFilters()
{
return array(
new Twig_Filter_Method('simpleFilter', array($this, 'simpleFilter'))
);
}
even if i do not know the difference beetween them.