Gateway plugin – Undefined array key ”cultureKey”

In a multilanguage site I’m using a gateway plugin to switch context depending on URL. The language switching works, but in the error log I get an error message each time I load a page in the default (web) context:

Undefined array key "cultureKey"

All contexts have a cultureKey set.

The URL for the default language is just domain.se/ and the domains for the other languages are domain.se/en and domain.se/de.

The gateway plugin looks like this:

if($modx->context->get('key') != "mgr"){
                /* grab the current langauge from the cultureKey request var */
                switch ($_REQUEST['cultureKey']) {
                    case 'en':
                        /* switch the context */
                        $modx->switchContext('English');
                        break;
                    case 'de':
                        /* switch the context */
                        $modx->switchContext('Deutsch');
                        break;
                    default:
                        /* Set the default context here */
                        $modx->switchContext('web');
                        break;
                }
                /* unset GET var to avoid
                 * appending cultureKey=xy to URLs by other components */
                unset($_GET['cultureKey']);
            }

PHP 8.2.10
MODX 3.0.4-dev

In the plugin code, make sure the variable $_REQUEST has a key cultureKey before you use it in the switch statement here:

For example with if (isset($_REQUEST['cultureKey'])) {...} or with array_key_exists.

2 Likes

@halftrainedharry solved it! Thank you so much!

It seems the same principle also solves an error (Undefined array key “galAlbum”) thrown by the snippet getURLparameter. If anyone’s looking:

Before:

    $output = $_GET[$par];
    if ($output == "") { $output=$defaultalbum; }
    return $output;

After:

if (isset($_REQUEST['galAlbum'])) {
    $output = $_GET[$par];
    if ($output == "") { $output=$defaultalbum; }
    return $output;
}

It’s better to check the same array you’re trying to access:

$output = isset($_GET[$par]) ? $_GET[$par] : $defaultalbum;
return $output;

You could also use the Null Coalescing Operator (??) instead:

$output = $_GET[$par] ?? $defaultalbum;
return $output;

Thank you @halftrainedharry! Appreciate it!

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”.