Hi.
this goes for Mautic 5, in Mautic 4 we did not have that problem.
I am building a plugin and I noticed when page content is loaded via ajax CoreBundle/CommonController->ajaxAction is not dispatching
CoreEvents::VIEW_INJECT_CUSTOM_CONTENT
CoreEvents::VIEW_INJECT_CUSTOM_TEMPLATE
and as a result if you override core templates and then go and load page by going directly to url of the page modifications are visible, but if you visit page via menu on the left, modifications disappear, because default template is loading.
I can fix that, but before I do, I would just like to know if there is a specific reason why ajaxAction is not dispatching events.
Thank you.
Best, M.
Thank you for your reply.
Original version was 5.1.0. I did look at the source of 5.2,0, but I dd not actually test it if it would trigger the events for ajax based request (will do that).
Just from looking into the code it did not seem like this was fixed on 5.2.0. But let me actually test the thing and get back, to be sure.
Best, M.
I have tested this, happens on 5.2.1 as well. I think we need to dispatch events somwhere in ajax version as well.
I think i fixed the issue with this PR:
mautic:5.2
← mzagmajster:mcp738_mautic_tpl_ajax_rendering_fix
opened 06:09PM - 06 Jan 25 UTC
eventAwareRenderView: We added this method, because ajax call needs to dispatch … event to give plugins chance to replace templates.
| Q | A
| -------------------------------------- | ---
| Bug fix? (use the a.b branch) | Y
| New feature/enhancement? (use the a.x branch) | N
| Deprecations? | N
| BC breaks? (use the c.x branch) | N
| Automated tests included? | N
| Related user documentation PR URL | mautic/user-documentation#...
| Related developer documentation PR URL | mautic/developer-documentation-new#...
| Issue(s) addressed | Fixes #...
## Description
In Mautic 5, when you override core template, changes only take an effect if page is not loaded via ajax, that is because when loading page via ajax does not dispatch CoreEvents::VIEW_INJECT_CUSTOM_TEMPLATE. This PR, fixes that
---
### 📋 Steps to test this PR:
[most likely you will have to test this locally]
1. Open this PR on Gitpod or pull down for testing locally (see docs on testing PRs [here](https://contribute.mautic.org/contributing-to-mautic/tester))
2. Load a plugin that overrides some core template
3. Visit a dashboard page.
4. Then click on a menu/link that uses custom template
5. Mautic should load the custom template.
I am not sure, this line also seems suspicious, I might uncover a bug down the road and end up writing a wrapper for it too, for now it fixed the issue at hand:
}
}
// Ajax call so respond with json
$newContent = '';
if ($contentTemplate) {
if ($forward) {
// the content is from another controller action so we must retrieve the response from it instead of
// directly parsing the template
$query = ['ignoreAjax' => true, 'request' => $request, 'subrequest' => true];
$newContentResponse = $this->forward($contentTemplate, $parameters, $query);
if ($newContentResponse instanceof RedirectResponse) {
$passthrough['redirect'] = $newContentResponse->getTargetUrl();
$passthrough['route'] = false;
} else {
$newContent = $newContentResponse->getContent();
}
} else {
$GLOBALS['MAUTIC_AJAX_DIRECT_RENDER'] = 1; // for error handling
$newContent = $this->renderView($contentTemplate, $parameters);