Question

I have got a plugin called LangSwither:

if($modx->context->get('key') != "mgr"){
    //grab the current domain from the http_host option
    switch ($modx->getOption('http_host')) {
        case 'www.xxxxxxxxxxx.de':
            //switch the context
            $modx->switchContext('en');
            //set the cultureKey
            $modx->setOption('cultureKey', 'en');
            break;
        default:
            // Set the default language/context here
            $modx->switchContext('web');
            $modx->setOption('cultureKey', 'pl');
            break;
    }
}

From here http://www.butter.com.hk/blog/posts/2010/08/internationalization-in-modx-revolution.html It listesns to the OnHandleRequest event. It throws: Fatal error: Call to a member function get() on a non-object in xxxxxxxx on line 7 (Line 7 is if($modx->context->get('key') != "mgr"){) Thanks.

Was it helpful?

Solution

I would remove the first "if" completely, you don't need to check if you're in manager or not since context doesn't make a difference there. You also don't need a switch to "web" context since that will be the default, you only need to change to "en" or any other language when needed. The culturekey should be set automatically when you switch context. This should be enough:

switch ($_SERVER['HTTP_HOST']) {
    case 'www.xxxxxxxxxxx.de':
        //switch the context
        $modx->switchContext('en');
        break;
}

If you really want the first "if" i would suggest lowering the priority (by increasing the number in the priority field for onHandleRequest to something high, like 999). To make sure it runs after everything else.

Edit: You might also want to try changing $modx->context->get('key') to $resource->get('context_key')

OTHER TIPS

It looks like you don't have a context object at $modx->context. It seems you've removed modx's context initialization method:

$modx->initialize('web'); if (!MODX_API_MODE) { $modx->handleRequest();}

Modx makes context initialization and after that it handles request where onHandleRequest event is published.

P.S. The best way of initialization for multicontext site is index.php modification where you should make context init before request handling. The major reason is that modx (with default index.php content) will authenticate your user at web context instead of proper context. Index.php modification solves this problem and you won't have headache with user's area of authentication.

If we talk about culture_key change — onHandleRequest event is the best place for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top