I don’t think the code in the documentation you linked was updated for MODX 3.
Here is a simple example for a CMP in MODX 3 that calls a Processor.
1. Namespace
Create a namespace in the MODX manager (
→ Namespaces) with the values “Name” = yourextra
, “Core Path” = {core_path}components/yourextra/
, Assets Path = {assets_path}components/yourextra/
2. Processor
Create a file core/components/yourextra/src/Processors/Sample.php
with this content:
<?php
namespace YourExtra\Processors;
class Sample extends \MODX\Revolution\Processors\Processor
{
public function process()
{
$random_value = substr(str_shuffle(MD5(microtime())), 0, 10);
return $this->success($random_value); // return a 'random' value
}
}
3. Bootstrap
Create a bootstrap file core/components/yourextra/bootstrap.php
to autoload your processor class.
<?php
try {
\MODX\Revolution\modX::getLoader()->addPsr4('YourExtra\\', $namespace['path'] . 'src/');
}
catch (\Throwable $t) {
$modx->log(\xPDO\xPDO::LOG_LEVEL_ERROR, $t->getMessage());
}
4. Menu Item
Create a menu item in the MODX manager (
→ Menus) with the values “Action:” = index
and “Namespace:” = yourextra
5. Controller
Create a controller file core/components/yourextra/controllers/index.class.php
with code like this:
<?php
class YourExtraIndexManagerController extends \MODX\Revolution\modManagerController
{
public function checkPermissions()
{
return true;
}
public function process(array $scriptProperties = [])
{
$code = <<<HTML
<input type="text" id="myinput"><br>
<button id="mybutton">Click</button>
<script>
var btn = document.getElementById("mybutton");
btn.addEventListener("click", function() {
MODx.Ajax.request({
url: MODx.config.connector_url,
params: {
action: 'YourExtra\\\\Processors\\\\Sample'
},
listeners: {
success: {
fn: function(res){
document.getElementById("myinput").value = res.message;
},
scope: this
}
}
});
});
</script>
HTML;
return $code;
}
public function getPageTitle()
{
return 'Your Extra';
}
public function loadCustomCssJs()
{
}
public function getTemplateFile()
{
return '';
}
}
The code outputs a text field and a button. A click on the button calls the processor and display the return value in the text field.
To keep the code simple, I put everything into the “process” function. But it’s better practice to use the functions “loadCustomCssJs” and “getTemplateFile” to load JS files and the HTML template.