Search code examples
phpoctobercmsoctobercms-backendoctobercms-plugins

Backend\Controllers\Users must define property $relationConfig


I like to build a Plugin where Frontend User belongsTo BackendUser ( One to Many Relation ). For the backend User i want to display an partial with Relation Manager to add many Frontend Users to BackendUser. If i try dynamically define a property on Plugin.php like:

use Backend\Models\User as BackendUser;
use Backend\Controllers\Users as BackendUsersController;


public function boot(){    
    BackendUsersController::extend(function($controller) {  
        $controller->implement[] = 'Backend.Behaviors.RelationController';  
        $controller->relationConfig = '$/plg-user/plg/controllers/plg-ctr/config_relation.yaml'  
    });
});

i get an Error : Class Backend\Controllers\Users must define property $relationConfig used by Backend\Behaviors\RelationController behavior

If i try to put manually :

public $relationConfig = '$/plg-user/plg/controllers/plg-ctr/config_relation.yaml'

to the Backend\Controllers\Users Controller everything woks.

any Idea ?


Solution

  • The problem arises because any object that implements the October\Rain\Extension\ExtendableTrait trait (like the Users controller does, which is how you can call ::extend() on it) prevents undeclared properties from being auto-declared on first assignment.

    Instead, you must use the addDynamicProperty($property, $value) method to add undeclared properties to the object. This was previously undocumented and was documented in octobercms/docs@9d454c.

    An example of working code for your case would now be

    /**
     * Extend the BackendUsers controller to include the RelationController behavior
     */
    BackendUsersController::extend(function($controller) {
    
        // Implement the list controller behavior dynamically
        $controller->implement[] = 'Backend.Behaviors.RelationController';
    
        // Declare the relationConfig property dynamically for the RelationController behavior to use
        $controller->addDynamicProperty('relationConfig', '$/plg-user/plg/controllers/plg-ctr/config_relation.yaml');
    });