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 ?
Answer
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/[email protected].
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');
});
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?