ClientConfig by modmore multi context issue

Got an issue:

If you use client config in a multi context senario e.g.

In Client conf I have this setup with multi contexts
DEFAULT
SomeValueInConfig = Value X
CONTEXT1
SomeValueInConfig = Value X
CONTEXT1
SomeValueInConfig = Value Y

CONTEXT1
[[++SomeValueInConfig]]
Shows correct Value X

CONTEXT2
[[++SomeValueInConfig]]
Shows iincorrect Value X

This does not always happen sometimes if you clear the cache it shows value “Value Y” in CONTEXT2, however most oof the time I get the wrong value.

Could it be that it pulls client config before it knows what context it is in?

What could be the issue How can I fix it?

What extra do you use to switch the context? On what event does the plugin run that switches the context?

Maybe you have to change the Priority of the event in the plugin, so that the context is switched, before the ClientConfig-plugin runs.

I will have a look. for contexts I am using contextRouter 1.2.0-pl

how do I make sure it context loads before? I set onMODXinit in the ClientConfig plugin to a lower priority than the contextRouter. does not seem to make a difference though.

<?php
/**
 * ClientConfig
 *
 * Copyright 2011-2014 by Mark Hamstra <hello@markhamstra.com>
 *
 * ClientConfig is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * ClientConfig is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * ClientConfig; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * @package clientconfig
 *
 * @var modX $modx
 * @var int $id
 * @var string $mode
 * @var modResource $resource
 * @var modTemplate $template
 * @var modTemplateVar $tv
 * @var modChunk $chunk
 * @var modSnippet $snippet
 * @var modPlugin $plugin
*/

$eventName = $modx->event->name;

switch($eventName) {
    case 'OnMODXInit':
    case 'OnHandleRequest':
    case 'pdoToolsOnFenomInit':
        /* Grab the class */
        $path = $modx->getOption('clientconfig.core_path', null, $modx->getOption('core_path') . 'components/clientconfig/');
        $path .= 'model/clientconfig/';
        $clientConfig = $modx->getService('clientconfig','ClientConfig', $path);

        /* If we got the class (gotta be careful of failed migrations), grab settings and go! */
        if ($clientConfig instanceof ClientConfig) {
            $contextKey = $modx->context instanceof modContext ? $modx->context->get('key') : 'web';
            $settings = $clientConfig->getSettings($contextKey);

            /* Make settings available as [[++tags]] */
            $modx->setPlaceholders($settings, '+');

            /* Make settings available for $modx->getOption() */
            foreach ($settings as $key => $value) {
                $modx->setOption($key, $value);
            }
        }
        break;
}

return;

Plugin code for CLient Config all seems fine. Maybe it does not know what context it is in when it calls

$contextKey = $modx->context instanceof modContext ? $modx->context->get('key')

note sure what to do how to I get it to make sure it is in right context? so it does not call default web context?

Plugins with a lower priority-number run first. Try using a higher number for the ClientConfig plugin.

yes thats what I did ClientConfig is runnning at 2 and Contextrouter at 0. however still same issue

Maybe try logging some information in the plugins to debug the issue.
Add a line like this to the ClientConfig plugin and something similar to ContextRouter plugin.

$modx->log(modX::LOG_LEVEL_ERROR, 'Plugin: ClientConfig | Event: ' . $eventName . ' | Context: ' . $contextKey);

Then check the MODX error log for the order of the log message and their content.

keeps loading web just like I thought

[2022-06-22 15:58:20] (ERROR @ 15.include.cache.php : 48) Plugin: ClientConfig | Event: OnMODXInit | Context: web
[2022-06-22 15:58:30] (ERROR @ 15.include.cache.php : 48) Plugin: ClientConfig | Event: OnMODXInit | Context: web
[2022-06-22 15:58:30] (ERROR @ /15.include.cache.php : 48) Plugin: ClientConfig | Event: OnHandleRequest | Context: web

eventhought I am refresh a page under another context…

and
[2022-06-22 16:07:43] (ERROR @ .include.cache.php : 52) Plugin: ContextRouter | Routes: correctcontext

is returning correct routes ie context…

could it be a cache thing?

Please, don’t show the log messages separately. With your posts above, there is no way to see in which order the two plugins are executed.

Then in the ContextRouter-plugin also log the event ($event) and the context it switches to ($routes[$host]).

$modx->switchContext($routes[$host]);

sorry only managed to look at it again now… I do still have the issue… does no one else have it?

so basically
Plugin: ContextRouter | Routes (using $routes[$host]): context2| Event (using $even) : OnMODXInit | Context (using $contextKey): web
Plugin: ClientConfig | Event (using $eventName): OnMODXInit | Context (using $contextKey): web

it says it routing to context2 but it is still in web context which means clientConfig it will read the web contect instead of context 2 and display the wrong variable.

is there another way to get the key? instead of
$contextKey = $modx->context instanceof modContext ? $modx->context->get(‘key’) : ‘web’;
which does not seem to get the right context? Or am I completly off?

I’m pretty sure this used to work, but maybe something changed. I will try to test it.
What version of MODX are you using?

3.01 many thanks, I will also try investigate some more and try find the issues bit later today when I get a gap,

I tested it, but I can’t reproduce the issue. The correct value for the clientconfig-setting is output.

MODX: 3.0.1-pl
ClientConfig: 2.3.1-pl
ContextRouter: 1.2.0-pl

In the ClientConfig-Plugin I have set the Priority for the event “OnHandleRequest” to a value > 0, so that it runs after the ContextRouter-Plugin.


Edit: I spoke too soon. It only seems to work for the first page I load after the cache is cleared.


Edit: The same setup in MODX 2.x works correctly. It has to be an issue with MODX 3.

Ok, I think I have found the problem: The code hasn’t been changed to take the MODX 3 namespaces into account.

In the ClientConfig plugin change this line

to this:

$contextKey = ($modx->context instanceof modContext || $modx->context instanceof \MODX\Revolution\modContext) ? $modx->context->get('key') : 'web';