Silverstripe unclecheese/dashboard module does not find template - silverstripe

I'm using unclecheese/dashboard module i use it like it is described in the README. I use silverstripe 3.5.3
I get this error message:
[User Warning] None of the following templates could be found (no
theme in use): DashboardMostActiveUsersPanel.ss
this is the panel content:
class DashboardMostActiveUsersPanel extends DashboardPanel{
private static $db = array (
'Count' => 'Int',
);
public function getLabel() {
return 'Most Active Users';
}
public function getDescription() {
return 'Shows the most active Users.';
}
public function getConfiguration() {
$fields = parent::getConfiguration();
$fields->push(TextField::create("Count", "Number of users to show"));
return $fields;
}
public function getMostActiveMembers() {
$members = Member::get()->sort("Activity DESC")->limit($this->Count);
return $members;
}
public function PanelHolder() {
return parent::PanelHolder();
}
}
this is the template:
<div class="dashboard-recent-orders">
<ul>
<% loop $MostActiveMembers %>
<li>$Name, $Activity</li>
<% end_loop %>
</ul>
</div>
This is where the error comes from: theme_enabled is empty
Config::inst()->get('SSViewer', 'theme_enabled‘)
I set the theme in the CMS Backend and i set it in config.yml like
SSViewer:
theme: 'my-theme'
I also tried to put the templates in different folders in /themes directory. But still no luck. What am i missing any help would be highly appreciated.

Themes only affect the frontend. The backend does not use them. You'll need to put the template in your mysite/ directory, or whatever your $project is.

Related

Silverstripe 4 save variable in database

Is posible to save SS template variable in database from CMS and after execute it in template?
Okay lets see example:
In CMS i have settings where i put social media links and contact informatios.
Also in CMS i have module where i create HTML block-s which after that i loop in website.
In that html block i want to put existing $SiteConfig.Email variable.
I Try that but that is rendered in template like $SiteConfig.Email not show real email?
Is this posible to do or i need some extra modification?
Check photo
The question you have written makes no sense to me, but I understand the screenshot.
So, SilverStripe renders .ss files with a class called SSViewer. Basically it reads the file as string and then runs it through SSViewer to generate the HTML output.
But, as you saw, the output of variables is not processed.
I can think of 3 ways to get what you want:
Run the variables through SSViewer aswell (in this example, use $RenderedHTMLContent in the template)
class MyDataObject extends DataObject {
private static array $db = [
'Title' => DBVarchar::class,
'HTMLContent' => DBText::class,
];
public function Foobar() { return "hello from foobar"; }
public function RenderedHTMLContent() {
$template = \SilverStripe\View\SSViewer::fromString($this->HTMLContent);
// using $this->renderWith() will allow you access to all things of $this in the template. so eg $ID, $Title or $Foobar. Probably also $SiteConfig because it's global
return $this->renderWith($template);
// if you want to add extra variables that are not part of $this, you can also do:
return $this->renderWith($template, ["ExtraVariable" => "Hello from extra variable"]);
// if you do not want $this, you can do:
return (new ArrayData(["MyVariable" => "my value"]))->renderWith($template);
}
}
Please be aware of the security implications this thing brings. SilverStripe is purposely built to not allow content authors to write template files. A content author can not only call the currently scoped object but also all global template variables. This includes $SiteConfig, $List, .... Therefore a "bad" content author can write a template like <% loop $List('SilverStripe\Security\Member') %>$ID $FirstName $LastName $Email $Salt $Password<% end_loop %> or perhaps might access methods that have file access. So only do this if you trust your content authors
Use shortcodes instead of variables. But I never liked shortcodes, so I don't remember how they work. You'll have to lookup the docs for that.
Build your own mini template system with str_replace.
class MyDataObject extends DataObject {
private static array $db = [
'Title' => DBVarchar::class,
'HTMLContent' => DBText::class,
];
public function Foobar() { return "hello from foobar"; }
public function RenderedHTMLContent() {
return str_replace(
[
'$SiteConfig.Title',
'$SiteConfig.Tagline',
'$Title',
'$Foobar',
],
[
SiteConfig::current_site_config()->Title,
SiteConfig::current_site_config()->Tagline,
$this->Title,
$this->Foobar(),
],
$this->HTMLContent
);
}
}

How to put an Elemental field under a tab in admin CMS form

Starting out with the Elemental module for Silverstripe 4 and by default it lists the Elemental area(s) under the Main "Content" tab. I'd like to put them under their own tab.
How do I do that in my Page class getCMSField function?
What I have is:
A specific page (ElementPage) for using the module
ElementPage:
extensions:
- DNADesign\Elemental\Extensions\ElementalPageExtension
In ElementPage.php I have two $has_one like this:
private static $has_one = [
'LeftElemental' => ElementalArea::class,
'RightElemental' => ElementalArea::class
];
Those work fine, fields display and can render them in the template.
Trying to put them under their own tab, the getCMSFields:
public function getCMSFields()
{
$fields = parent::getCMSFields();
// To remove the default added one
$fields->removeByName('ElementalArea');
$fields->addFieldToTab('Root.LeftContentBlocks', ElementalArea::create('LeftElementalID'));
return $fields;
}
Resulting error:
[User Warning] DataObject::__construct passed The value
'LeftElementalID'. It's supposed to be passed an array, taken straight
from the database. Perhaps you should use DataList::create()->First();
instead?
I didn't really expect that to work but I can't see the create signature it needs.
EDIT:
This seems to get it done:
public function getCMSFields()
{
$fields = parent::getCMSFields();
// To remove the default added one
$fields->removeByName('ElementalArea');
$fields->addFieldToTab('Root.LeftContentBlocks', ElementalAreaField::create('LeftElemental', $this->LeftElemental(), $this->getElementalTypes()));
$fields->addFieldToTab('Root.RightContentBlocks', ElementalAreaField::create('RightElemental', $this->RightElemental(), $this->getElementalTypes()));
return $fields;
}
I'm not entirely sure $this->getElementalTypes() is what I should be doing. Any improvements/corrections are welcomed.

Silverstripe 4: Include Template from Filepath

I'm working on a Silverstripe 4 project where we need to include a SS template file from a path.
Here's a simple example giving the gist of what I'm trying to achieve.
class ExampleController extends ContentController
{
public function IncludeTemplateFromFilePath() {
var $FilePath = '/path/to/file';
???
return $output
}
}
Template syntax:
<div>$IncludeTemplateFromFilePath</div>
I've had a look through the SSViewer documentation and looked at the Silverstripe source code but can't work out the correct syntax to make this work.
There are many examples of:
return SSViewer::get_templates_by_class(static::class, $suffix, self::class);
But what is the syntax to get a template from it's filepath?
I believe you can do the following:
public function IncludeTemplateFromFilePath()
{
return SSViewer::execute_string(
file_get_contents('/path/to/Template.ss'),
[
'Content' => 'Value that will be in $Content when used in /path/to/Template.ss'
]
);
}
Reference: http://api.silverstripe.org/4/SilverStripe/View/SSViewer.html#method_execute_string

How can I pass variables to login and register view in laravel 5.3

I am learning and developing a project in laravel 5.3. So I stucked at a point that, to every view in this project I am passing variables to views like following.
public function index()
{
$page_title = 'Page Title';
return view('home', ['title' => $page_title]);
}
so in login and register controllers there are no methods to return views. And I want to pass same variables with different string values to login and registration form. so how can i do that. And secong thing I want to ask is, that how can I add a 404 error page in my project for undefined routes. and third question is that can I set 404 page to register route (www.myproject.com/register) after adding some users in my project. looking forword for reply..
The methods to return the views are in traits, if you want to add you own logic for these methods you can simply override them by adding your own methods the class that uses the trait e.g.
RegisterController
public function showRegistrationForm()
{
$title = 'Register';
return view('auth.register', compact('register'));
}
LoginController
public function showLoginForm()
{
$title = 'Login';
return view('auth.login', compact('title'));
}
If you want to add a custom 404 error page then you just need to create that a file at resources/views/errors/404.blade.php as shown in the docs https://laravel.com/docs/5.4/errors#custom-http-error-pages
Laravel comes with a RedirectIfAuthenticated middleware that will (as the name suggests) rediect the user away from a route if they are already logged in. By default, the login and register routes already have this. If you want to change this behaviour just edit your App\Http\Middleware\RedirectIfAuthenticated class.
Hope this helps!
In the login controller > AuthenticatesUsers trait you can type your variables here.
Default path: app/Http/Controllers/Auth/LoginController.php
public function showLoginForm()
{
$test = 'test';
return view('auth.login', compact('test'));
}
Optimized this could save your 'arss'
LoginController.php
public function showLoginForm()
{
$title = 'Login';
$css = array(
asset('sign-up-in/css/index.css'),
);
$js = array(
asset('sign-up-in/js/index.js'),
);
return view('auth.login', compact('title','css','js'));
}
Do the same for RegisterController.php +(bonus) every other controller
Now slay it all with this
app.blade.php
#if(isset($js))
#foreach($js as $key => $value)
<script src="{{$value}}"></script>
#endforeach
#endif
#if(isset($css))
#foreach($css as $key => $value)
<link href="{{$value}}" rel="stylesheet">
#endforeach
#endif

How can I allow the user to set a custom global value in the silverstripe CMS?

I would like to be able to set custom values in the CMS, such as with the site name and tagline. I can't currently find any way of doing this other than on individual pages.
You can do so by extending SiteConfig. Your Extension might look like this:
class CustomSiteConfig extends DataExtension
{
private static $db = array(
'CustomContent' => 'Varchar(255)'
);
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab('Root.Main',
TextField::create('CustomContent', 'Custom content')
);
}
}
Then you need to apply the extension to SiteConfig. Add the following to mysite/_config/config.yml
SiteConfig:
extensions:
- CustomSiteConfig
And that's it. Run dev/build and your new field should be editable in the CMS as well as accessible in the Template using: $SiteConfig.CustomContent

Resources