Load component on plugin run?


#1

Hii there,
In our project, we have a component that allows us to easily add options to our database form our plugins (a bit like WordPress does it).
For this, we need to run a small piece of code in the plugin to tell the component a new setting is there:

$this->loadComponent('Kikioboeru/Kikioboeru.Settings');

// Create a new settings group
 $this->Settings->registerGroup('contact','Contact Settings');

// Add all settings for this plugin to the settings component
$this->Settings->registerOption('text','smtp[host]','','contact');

I’ve added this to the AppController of the plugin, but this gives a small issue since this code is never ran until we actually do something with the plugin itself.
This is a bit of an issue since it doesn’t use the plugin itself, the plugin basically just tells to the main app: “hey, I’m here, and these are the settings I want you to show the user (so he/she can change it without having to code)”.

Where should I add this code in the plugin (not in the main app) so that it will run when the plugin it loaded?


Trying to run migration results in SQLSTATE[42S02]
#2

Seems like this is maybe the sort of thing that the bootstrap option of the plugin loader is for?


#3

I imagined that too yes, but this only seems to break the app.
I placed the code in plugin’s bootstrap.php like so:

<?php 
use Cake/Core/Plugin;

$this->loadComponent('Kikioboeru/Kikioboeru.Settings');

// Create a new settings group
$this->Settings->registerGroup('contact','Contact Settings');

// Add all settings for this plugin to the settings component
$this->Settings->registerOption('text','smtp[host]','','contact');

but this results in: the error: Call to undefined method Kikioboeru\Contact\Plugin::loadComponent().

EDIT: I’ve hacked around it a little bit and put this code in the bootstrap.php of the plugin:

use Cake\Controller\Controller;

$controller = new Controller();
$controller->loadComponent('Kikioboeru/Kikioboeru.Settings');

// Create a new settings group
$controller->Settings->registerGroup('contact','Contact Settings');

// Add all settings for this plugin to the settings component
$controller->Settings->registerOption('text','smtp[host]','','contact');

While this does sorta work (it adds it to the options property just fine), when I go to the SettingsController and add the following code, the options property is empty again (probably because they are two seperate instances):

pr($this->Settings->options);

#4

I solved this issue by making use of (read: abuse) the Configure class instead of making it a property in the component class.