nikkio
November 21, 2023, 4:20pm
1
Hi,
I am having an issue with pdopage ajax pagination on a multilanguage website.
The main language is ok, it loads the other pages without problem, but with the second language it stop on the first page.
You can find an example here:
the italian version is OK, but when I switch to English, is not working anymore:
I have no error in the js console, but looking in the network panel, there is this request
Can be related to the problem? In the Italian side, there is only connector.php followed by the images.
This is the code I use for the pdopage call:
<div class="col" id="pdopage">
[[!+page.nav]]
<div class="rows row row-cols-lg-3 row-cols-md-2 row-cols-1 gy-4 item-list">
[[!pdopage?
&ajaxMode=`scroll`
&tpl=`itemBoxRender`
&sortby=`menuindex`
&limit=`12`
&depth=`0`
&parents=`[[*id]]`
&includeTVs=`ImgRender,CatElemento`
&processTVs=`ImgRender`
&sortdir=`DESC`
]]
</div>
</div>
Thank you for any help.
To load the data for the second page, the connector assets/components/pdotools/connector.php
gets executed.
The connector should return JSON, but for your “it” context, it returns HTML (probably the content of the “error” page).
Theoretically, the connector checks if the requested page (request parameter “pageId”) is in a different context and switches to that context here in the code:
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->services->add('error', new MODX\Revolution\Error\modError($modx));
$modx->error = $modx->services->get('error');
// Switch context if needed
if (!empty($_REQUEST['pageId'])) {
if ($resource = $modx->getObject(MODX\Revolution\modResource::class, ['id' => (int)$_REQUEST['pageId']])) {
if ($resource->get('context_key') !== 'web') {
$modx->switchContext($resource->get('context_key'));
}
$modx->resource = $resource;
}
}
// Run snippet
if (!empty($_REQUEST['hash']) && !empty($_SESSION['pdoPage'][$_REQUEST['hash']])) {
$scriptProperties = $_SESSION['pdoPage'][$_REQUEST['hash']];
$_GET = $_POST;
Maybe when the code invokes the event “OnHandleRequest” on this line
$modx->resource = $resource;
}
}
// Run snippet
if (!empty($_REQUEST['hash']) && !empty($_SESSION['pdoPage'][$_REQUEST['hash']])) {
$scriptProperties = $_SESSION['pdoPage'][$_REQUEST['hash']];
$_GET = $_POST;
// For ClientConfig and other similar plugins
$modx->invokeEvent('OnHandleRequest');
$modx->runSnippet('pdoPage', $scriptProperties);
}
your router plugin switches the context again.
To debug the problem, can you test if it works correctly when you (temporarilly) comment out line 26 that invokes the “OnHandleRequest” event?
What extra do you use to switch the contexts? Or did you write a custom plugin?
nikkio
November 21, 2023, 5:52pm
3
thanks @halftrainedharry ,
well, if I comment the line 26, it works!
I use languagerouter to switch contexts with these context settings:
for the Italian context
for the english context
any suggestion?
Do you mean the extra LangRouter ?
There already exist similar issues in the Github repos of both pdoTools and LangRouter :
opened 04:28PM - 26 Apr 23 UTC
Hello!
I have two: contexts: **web**(english) and **de**(german).
I have an… issue with langRouter + pdoPage. It works correctly with **web** context until I haven't german language in the settings of my browser, but when I add it, the langRouter returns an empty result.
Could you help me resolve this issue?
MODx: 3.0.1-pl
langRouter: 1.4.2
**English version located on url:** http://localhost:8081/en/
**German version located on url:** http://localhost:8081/de/
**my context settings:**
base_url: /
cultureKey: **en** and **de** in the **de** context
url_scheme: https://
site_url: {url_scheme}{http_host}{base_url}{cultureKey}/
**here is the langRouter log:**
[2023-04-26 18:09:41] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:41] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /en/profile/
REDIRECT_URI:
QUERY_STRING: q=en/profile/
q: en/profile/
Context: web
Site start: 1
[2023-04-26 18:09:41] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:41] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:41] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key found in URI:
REQUEST_URI: /profile/
REDIRECT_URI:
QUERY_STRING: q=en/profile/
q: profile/
Context: web
Site start: 1
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 166) PHP warning: Undefined array key "q"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /assets/components/pdotools/connector.php
REDIRECT_URI:
QUERY_STRING:
q:
Context: web
Site start: 1
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) Detected culture key: string(2) "de"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) Detected context key: string(2) "de"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 166) PHP warning: Undefined array key "q"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key not found in URI:
REQUEST_URI: /assets/components/pdotools/connector.php
REDIRECT_URI:
QUERY_STRING:
q:
Context: de
Site start: 114
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 198) Redirect to http://localhost:8081/de/?pdotreat3=1&pageId=6&hash=b5edf689092779b36e96e2bdc4b3cbc95f1be8e0
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 166) PHP warning: Undefined array key "q"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /assets/components/pdotools/connector.php
REDIRECT_URI:
QUERY_STRING:
q:
Context: web
Site start: 1
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) Detected culture key: string(2) "de"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) Detected context key: string(2) "de"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 166) PHP warning: Undefined array key "q"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /assets/components/pdotools/connector.php
REDIRECT_URI:
QUERY_STRING:
q:
Context: web
Site start: 1
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) Detected culture key: string(2) "de"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) Detected context key: string(2) "de"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 166) PHP warning: Undefined array key "q"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key not found in URI:
REQUEST_URI: /assets/components/pdotools/connector.php
REDIRECT_URI:
QUERY_STRING:
q:
Context: de
Site start: 114
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 198) Redirect to http://localhost:8081/de/?pdouser=1&pageId=6&hash=0f7a4ea631e722615e70cfd727eef12ae0193c54
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:43] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 166) PHP warning: Undefined array key "q"
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key not found in URI:
REQUEST_URI: /assets/components/pdotools/connector.php
REDIRECT_URI:
QUERY_STRING:
q:
Context: de
Site start: 114
[2023-04-26 18:09:43] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 198) Redirect to http://localhost:8081/de/?pdotreat1=1&pageId=6&hash=6b61654128b5084c489de49dcf1c40c4881ba6af
[2023-04-26 18:09:44] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /de/?pdouser=1&pageId=6&hash=0f7a4ea631e722615e70cfd727eef12ae0193c54
REDIRECT_URI:
QUERY_STRING: q=de/&amp;pdouser=1&amp;pageId=6&amp;hash=0f7a4ea631e722615e70cfd727eef12ae0193c54
q: de/
Context: web
Site start: 1
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:44] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /de/?pdotreat3=1&pageId=6&hash=b5edf689092779b36e96e2bdc4b3cbc95f1be8e0
REDIRECT_URI:
QUERY_STRING: q=de/&amp;pdotreat3=1&amp;pageId=6&amp;hash=b5edf689092779b36e96e2bdc4b3cbc95f1be8e0
q: de/
Context: web
Site start: 1
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:44] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key found in URI:
REQUEST_URI: /?pdotreat3=1&pageId=6&hash=b5edf689092779b36e96e2bdc4b3cbc95f1be8e0
REDIRECT_URI:
QUERY_STRING: q=de/&amp;pdotreat3=1&amp;pageId=6&amp;hash=b5edf689092779b36e96e2bdc4b3cbc95f1be8e0
q:
Context: de
Site start: 114
[2023-04-26 18:09:44] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key found in URI:
REQUEST_URI: /?pdouser=1&pageId=6&hash=0f7a4ea631e722615e70cfd727eef12ae0193c54
REDIRECT_URI:
QUERY_STRING: q=de/&amp;pdouser=1&amp;pageId=6&amp;hash=0f7a4ea631e722615e70cfd727eef12ae0193c54
q:
Context: de
Site start: 114
[2023-04-26 18:09:44] (ERROR @ /www/core/src/Revolution/modParser.php : 509) Could not find snippet with name !++site_start.
[2023-04-26 18:09:44] (ERROR @ /www/core/src/Revolution/modParser.php : 509) Could not find snippet with name !++site_start.
[2023-04-26 18:09:44] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Unhandled request:
REQUEST_URI: /de/?pdotreat1=1&pageId=6&hash=6b61654128b5084c489de49dcf1c40c4881ba6af
REDIRECT_URI:
QUERY_STRING: q=de/&amp;pdotreat1=1&amp;pageId=6&amp;hash=6b61654128b5084c489de49dcf1c40c4881ba6af
q: de/
Context: web
Site start: 1
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 186) contextmap: array(2) {
["en"]=>
string(3) "web"
["de"]=>
string(2) "de"
}
[2023-04-26 18:09:44] (ERROR @ /www/core/components/langrouter/src/LangRouter.php : 164) PHP warning: Undefined array key "REDIRECT_URI"
[2023-04-26 18:09:44] (ERROR in LangRouter @ /www/core/components/langrouter/src/LangRouter.php : 169) Culture key found in URI:
REQUEST_URI: /?pdotreat1=1&pageId=6&hash=6b61654128b5084c489de49dcf1c40c4881ba6af
REDIRECT_URI:
QUERY_STRING: q=de/&amp;pdotreat1=1&amp;pageId=6&amp;hash=6b61654128b5084c489de49dcf1c40c4881ba6af
q:
Context: de
Site start: 114
`
opened 08:34AM - 15 May 23 UTC
bug
I have two: contexts: **web**(english) and **de**(german).
I have an issue w… ith langRouter + pdoPage. It works correctly with **web** context until I haven't german language in the settings of my browser, but when I add it, the ** /assets/components/pdotools/connector.php** returns an empty result.
Could you help me resolve this issue?
MODx: 3.0.3-pl
langRouter: 1.4.2
pdoTools: 3.0.2-pl
**English version located on url:** http://localhost:8081/en/
**German version located on url:** http://localhost:8081/de/
**my context settings:**
base_url: /
cultureKey: **en** and **de** in the **de** context
url_scheme: https://
site_url: {url_scheme}{http_host}{base_url}{cultureKey}/
Here is the video link of reproducing the issue: https://www.awesomescreenshot.com/video/17277172?key=254c18657247e2046b314cb9861d9f28
I contacted to the langRouter developer but he told that the issue with pdoTools
the related langRouter thread is here: https://github.com/Jako/LangRouter/issues/33
I believe the issue has to be fixed in the code.
Maybe LangRouter could use the event OnMODXInit
instead, so that invoking the event OnHandleRequest
doesn’t switch the context.
Or pdoTools could introduce an option to disable the invoking of OnHandleRequest
.
jako
November 21, 2023, 11:26pm
6
LangRouter does not just switch the context, it initializes the context and the culture. Maybe that’s missing in the pdoPage connector.
Ok, so I tested it.
To load the items for the second page via AJAX, pdoTools calls the file assets/components/pdotools/connector.php
.
In this connector $modx->invokeEvent('OnHandleRequest');
is executed.
This runs the LangRouter plugin.
There is no request parameter “q” (because it’s a request to a connector), so the code switches the context here (depending on the request header “HTTP_ACCEPT_LANGUAGE”)
// Remove cultureKey from request
$_REQUEST[$queryKey] = preg_replace('~^' . preg_quote($cultureKey, '~') . '/(.*)~', '$1', $_REQUEST[$queryKey]);
$_SERVER['REQUEST_URI'] = preg_replace('~^/' . preg_quote($cultureKey, '~') . '/(.*)~', '/$1', $_SERVER['REQUEST_URI']);
$this->langrouter->logRequest('Culture key found in URI');
$this->modx->cultureKey = $cultureKey;
} else {
$contextKey = $this->langrouter->contextKeyDetect($contextmap);
if ($this->modx->getContext($contextKey)) {
$switched = $this->modx->switchContext($contextKey);
} else {
if ($contextKey) {
$this->modx->log(xPDO::LOG_LEVEL_ERROR, 'The system setting langrouter.contextDefault/babel.contextDefault is invalid!', '', 'LangRouter');
} else {
$this->modx->log(xPDO::LOG_LEVEL_ERROR, 'The system setting langrouter.contextDefault/babel.contextDefault is empty!', '', 'LangRouter');
}
$switched = false;
}
$this->langrouter->logRequest('Culture key not found in URI');
and then makes a redirect here
$get = $_GET;
unset($get[$queryKey]);
$query = (empty($get)) ? $query : $query . '?' . http_build_query($get);
$siteUrl = $this->modx->context->getOption('site_url') . $query;
$currentUrl = ($this->modx->getOption('HTTPS', $_SERVER, false) === true ? 'https' : 'http') . '://' . $this->modx->getOption('HTTP_HOST', $_SERVER, '') . $this->modx->getOption('REQUEST_URI', $_SERVER, '');
if ($siteUrl != $currentUrl) {
// Redirect to valid context
$this->langrouter->logMessage('Redirect to ' . $siteUrl);
$this->modx->sendRedirect($siteUrl, ['responseCode' => $this->langrouter->getOption('response_code')]);
}
} else {
$this->langrouter->logMessage('The switched MODX context was not valid');
}
} else {
$this->langrouter->logMessage('Context switch to "' . $contextKey . '" was not valid.');
}
}
// Set locale since $this->modx->_initCulture is called before OnHandleRequest
jako
November 22, 2023, 10:48am
8
Then the q parameter has to be set in the pdoPage connector.
This is one possible solution for this specific problem.
But it could theoretically generate different issues with other router plugins.
Alternative you could check $_SERVER['REQUEST_URI']
in the LangRouter plugin, and stop processing it, if for example the request is for a “connector.php” file.
jako
November 22, 2023, 12:32pm
10
Sure, but for this request (to the pdoTools -connector), the routing-plugins shouldn’t do anything at all. MODX was already switched to the correct context.
$modx->invokeEvent('OnHandleRequest');
is only called as a workaround to make ClientConfig placeholders available .
If a routing-plugin has no “q” request parameter and just keeps the current context, then everything works ok.
The “problem” with LangRouter is, that if there is no “q” request parameter, the code guesses the most likely context (depending on the request header “HTTP_ACCEPT_LANGUAGE”) and therefore “wrongly” switches the context (in some cases).
jako
November 22, 2023, 2:41pm
12
The q request parameter is essential for displaying the MODX frontend. The pdoPage snippet wants to render a part of the frontend and the pdoPage connector has to set that essential parameter or it has to tell MODX (and other parts), that it runs in connector mode.
nikkio
November 22, 2023, 5:35pm
13
Aassuming that pdoPage developer should handle this p parameter, it is safe to leave the OnHandleRequest commented?
I have already checked the website and seems working fine, as usual.
The problem with changing the file assets/components/pdotools/connector.php
directly is, that on the next update (of the extra), all your changes will get overwritten.
So if the developer of pdoTools doesn’t fix the issue, your site will break again.
So maybe instead, you could try something like this:
Copy the connector assets/components/pdotools/connector.php
to a custom folder. (e.g. assets/components/mypackage/
)
In this copied connector, delete the line that invokes the event OnHandleRequest
.
Copy the file assets/components/pdotools/js/pdopage.js
to a custom folder. (e.g. assets/components/mypackage/js/
)
In the copied JS file, change this line
var params = pdoPage.Hash.get();
for (var i in params) {
if (params.hasOwnProperty(i) && pdoPage.keys[i] && i != key) {
delete(params[i]);
}
}
params[key] = pdoPage.keys[key] = page;
params['pageId'] = config['pageId'];
params['hash'] = config['hash'];
$.post(config['connectorUrl'], params, function (response) {
if (response && response['total']) {
wrapper.find(pagination).html(response['pagination']);
if (mode == 'append') {
wrapper.find(rows).append(response['output']);
if (config['mode'] == 'button') {
if (response['pages'] == response['page']) {
$(config['more']).hide();
}
else {
$(config['more']).show();
to $.post('/assets/components/mypackage/connector.php', params, function (response) {
so that your custom connector gets called.
Change the pdoPage tag in the resource to use the custom JS file.
[[!pdoPage?
...
&frontend_js=`[[++assets_url]]components/mypackage/js/pdopage.js`
]]
nikkio
November 23, 2023, 5:51pm
15
Thank you @halftrainedharry it works!
If the developer of pdoTools fix the issue, i will change back to the original connector.
Regards,
.nicola
system
Closed
November 25, 2023, 5:51pm
16
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”.