Use ajax method in a Setting model

Hello,

I work on a plugin that have setting model. Have some basic fields in it, everything works fine.

Now I need to do an advanced field, a partial that will trigger an ajax method.

So I create my partial :

<button class="btn btn-primary" data-request="onCopyWebsiteContent">
    My button
</button>

And I put my method on my model :

public function onCopyWebsiteContent() {
    trace_log('ok');
}

My problem is, that when I click the button, I get the following error :

Ajax handler “onCopyWebsiteContent” cannot be found.

I understand why, my method should be in the controller, not the model, but in the case of a setting model, there is no controller.

How can I solve that problem ?

Thank you

Hi @Zmove ,

https://octobercms.com/docs/api/system/controllers/settings

Is this the controller ? If so, you could extend it.

You need in your plugin.php extend settings controller like apinard mentoined…
here some blueprint for that

in Plugin.php - boot() method

\System\Controllers\Settings::extend(function($controller) {
      $controller->addDynamicMethod('onCopyWebsiteContent', function() use ($controller) {
           ...  your code ...
      });
               
});
1 Like

@apinard @snipi

Thank you, yout solution works perfectly (except the $cspSettings variable not found, but just removing it and it works).

1 Like

yup, sorry for that. remove whole use ($cspSettings) part