This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Adding row class in backend lists controller
In the OctoberCMS doc we can find some css classes for highlight rows. And we can see an example in the Rainlab.User plugin.
From the controller, only need to add the method listInjectRowClass
.
// This code is part of the \RainLab\User\Controllers\Users
public function listInjectRowClass($record, $definition = null)
{
if ($record->trashed()) {
return 'strike';
}
if (!$record->is_activated) {
return 'disabled';
}
}
But, if we need to extend the controller \RainLab\User\Controllers\Users
and add our row class, we can use our own behavior and extend the controller.
For more information see Extending list behavior
So, first we need to add our behavior in $implement
var
// Our Plugin.php
class Plugin extends PluginBase
{
public function boot()
{
UserController::extend(
function ($controller) {
$controller->implement[] = '@Author.Plugin.Behaviors.UserController';
}
);
}
}
// Our behaviors/UserController.php
class UserController extends ExtensionBase
{
public function __construct($controller)
{
Event::listen('backend.list.injectRowClass', function ($lists, $record) {
return $this->listInjectRowClass($record);
});
}
public function listInjectRowClass($record)
{
// return the css class
// see https://octobercms.com/docs/ui/list at Row classes
}
}
Important note
The $record
variable in listInjectRowClass($record)
method varies as the method is executed.
When listInjectRowClass($record)
is called directly from the controller, $record
is a model reference and is the first parameter, but when is called from Event::listen()
it is a \Backend\Widgets\Lists
reference, and is needed to call it as second parameter, as in the above example.
Last updated
I also cannt figure out how to inject class in relation list. Has anybody had any luck, or can give some guidence?
1-4 of 4