October CMS resources and help articles

Simple and to the point. Optimized by the community.

Add a relation to an existing plugin's controller

2
by mjauvin, last modified on February 29th, 2020

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:

\Event::listen('system.extendConfigFile', function($publicFile, $config) {
   if ($publicFile === '/plugins/pluginauthor/pluginname/controllers/plugincontroller/config_relation.yaml') {
      $config['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,
         ],
      ];
      return $config;
   }
});

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?