MODX3: How can I use the connector.php file in MODX to connect my custom snippet with the template file (e.g., home.tpl) and process/display form data in real-time?

I want to process the data that I will get from my home.tpl form using my custom snippet then will send the result back to home.tpl,

I created a simple form template for my CMP, and I already have the CMP working only sending the form data to my processor is my problem, I tried connector using the available resources/tutorials but my submit button seems not being read by the connector.php or I’m doing it wrong. You can send also multiple strings in array form.

the process is simple, form(submit data) > PHP code(remove spaces on string) > form(display text output).

please let me know if I’m missing some files needed for this kind of process, Thank you!

It’s hard to say why it doesn’t work or what you are doing wrong, without knowing the code you wrote.
In general, you can do on your CMP whatever you like. There are no strict guidelines you have to follow.


Do you use Ext.js?
Do you use MODX specific Ext.js components, like for example MODx.FormPanel?

If you are using Ext.js, then you should be able to call a custom processor with code like this:

MODx.Ajax.request({
	url: MODx.config.connector_url,
	params: {
		action: 'YourExtra\\Processors\\Sample'
	},
	listeners: {
		success: {
			fn: function(res){
				Ext.Msg.alert('Done', res.message);
			},
			scope: this
		}
	}
});

You can use the default MODX connector. Then define the action request parameter for the routing.

Processor file:

namespace YourExtra\Processors;

class Sample extends MODX\Revolution\Processors\Processor
{
	...
}
1 Like

I didn’t use Ext.js I am not able to replicate this, Developing an Extra, Part II - Developing an Extra | MODX Documentation so I will give it another try. Thank you for your help!

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 (:gear: → 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 (:gear: → 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.

1 Like

I see that’s why. This is helpful, Thank you so much. yeah I am having trouble looking for resources for modx 3.0, I have one more question, on this Ajax, MODx.Ajax.request I will get response/return/echo from my processor right? I believe because of this? listeners: {}. I already did getTemplateFile.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.