OctoberCMS: How to extend a controller by adding or editing RelationController?

- 1 answer

Ad

How can we extend a plugin by adding Backend.Behaviors.RelationController to one of its controllers? I need to extend the User Plugin and add a belongsToMany relationship to its User model. I also want to extend its Users controller to implement the Backend.Behaviors.RelationController behavior. I know that form fields can be extended using extendFormFields and list columns can be extended using extendListColumns. But what about extending a plugin by adding or editing the RelationController behavior?

Ad

Answer

Ad

Read up on the Behavior documentation, specifically as it relates to extending classes to implement new behaviors.

Essentially, you will need to extend the Users controller to both implement the RelationController behavior and configure that behavior. A code example accomplishing that is below:

UsersController::extend(function($controller) {
    $controller->implement[] = ['Backend.Behaviors.RelationController'];
    $controller->relationConfig = '$/myvendor/myplugin/controllers/users/config_relation.yaml'
});
Ad
source: stackoverflow.com
Ad