Winter CMS resources and help articles

Simple and to the point. Optimized by the community.

Add a relation to an existing plugin's controller

0
by mjauvin, last modified on May 3rd, 2022 - Previously published on OctoberTricks

Let's assume the following:

  • There is a plugin named "pluginAuthor.pluginName" you want to extend;
  • This plugin has a controller named "pluginController";
  • You want to add a new relation named "myRelation" to this controller;
  • Your plugin is named "myAuthor.myPlugin";

Add the following code to your plugin's boot() method:

pluginController::extend( function ($controller) {
    $new_relation = $controller->makeConfigFromArray([
        'myrelation' => [
            'label' => 'My Relation',
            'view' => [
                'list' => '$/myauthor/myplugin/models/myrelation/columns.yaml',
                'toolbarButtons' => 'add|create|remove',
                'showSearch' => true,
             ],
             'manage' => [
                 'list' => '$/myauthor/myplugin/models/myrelation/columns.yaml',
                 'form' => '$/myauthor/myplugin/models/myrelation/fields.yaml',
                 'showSearch' => true,
             ],
         ]
    ]);

    $controller->relationConfig = $controller->mergeConfig(
        $controller->relationConfig, 
        $new_relation
    );

Alternatively, you can create a config_relation.yaml file with the new relation(s) and use this code:

pluginController::extend( function ($controller) {
    $controller->relationConfig = $controller->mergeConfig(
        $controller->relationConfig,
        '$/myAuthor/myPlugin/config/config_relation.yaml'
    );
});

Discussion

0 comments

We use cookies to measure the performance of this website. Do you want to accept these cookies?