I need to add a block to a page. In a block adding dialog I have a select box which I need to fill with data received from ajax prior to saving the block (it cannot be saved unless I select the option).
The problem is the ajax call is usually to a controller action which doesn't exist until I save the block which I can't save before the option is selected. How can I make an ajax call to any other non-action function prior to the block saving? Can it be any other controller function or it must only be an action_function? Is this possible?
[UPDATE]
I'm trying with routes. Declared a route in the package controller:
$this->app->make(Router::class)->register('/api/get_forum_posts', '\Concrete\Package\AbForum\Src\Forum\MyFunctions::get_forum_posts', null, [], [], '', [], ['GET']);
but it says:
Exception Occurred: /srv/www/htdocs/c584/concrete/src/Controller/ApplicationAwareControllerResolver.php:89 Class "\Concrete\Package\AbForum\Src\Forum\MyFunctions" does not exist.
but the class MyFunctions IS in that folder packages/ab_forum/src/Forum/MyFunctions.php
I got it. The following works.
In the package controller:
protected $pkgAutoloaderRegistries = [
'src/Forum' => 'Forum'
];
public function on_start()
{
$this->app->make(Router::class)->register('/api/get_forum_posts', 'Forum\MyFunctions::getForumPostsJson', null, [], [], '', [], ['GET']);
...
}
The MyFunctions controller class in packages/ab_forum/src/Forum/MyFunctions.php:
namespace Forum;
use Concrete\Core\Controller\AbstractController;
class MyFunctions extends AbstractController
{
public function getForumPostsJson()
{
$data = $request->request->all();
...
echo json_encode($json);
exit;
}
}
The block form with ajax:
$.ajax({
url: '<?php echo Url::to('api/get_forum_posts'); ?>',
type: 'GET',
data: {
topic: topic.val(),
date: date.val(),
},
})
.done(function(data) {
...
});