This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

planetadeleste
planetadeleste

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

slave2anubis
slave2anubis

Hi, nice post! Do you have any idea how to add a class to a relation manager list?

WaskiWabit
WaskiWabit

Hi @bdacian18487, did you ever figure out how to add a css class to a relation list?

roulendz
roulendz

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

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.