How to use lexicon entries in a plugin

Hi,
i have two contexts (de|en) and want to fill in a TV by passing a context-related lexicon-entry into it OnDocFormSave.

I tried a few things, but only get the proper output for the standard context (de), not my second context (en). For the second context the output is the unprocessed lexicon placeholder itself.

I tried:
$modx->lexicon('my.placeholder'); outputs the de-value in both contexts
$modx->lexicon('my.placeholder', array(), 'de'); standard context, works fine
$modx->lexicon('my.placeholder', array(), 'en'); second context, outputs my.placeholder

I also tried $modx->lexicon->load('en:namespace:default'); without luck

Is the setting cultureKey correctly set for both contexts?

I think $modx->lexicon->load('en:namespace:topic'); should work.
Did you use the correct values for “namespace” and “topic”?

Yes, the cultureKey is set correctly for both contexts. [[++cultureKey]] works as expected in the frontend.
No typos, namespace and topic values are correct.

Strangely, this here outputs “de” in each context:

<?php
$culturekey = $modx->getOption('cultureKey');
$resource->setTVValue('seo-title', $culturekey);

$modx->lexicon->load('de:namespace:topic'); outputs the value for “de” in both contexts
$modx->lexicon->load('en:namespace:topic'); outputs “my.placeholder”, which is the placeholder, not the value

Coming closer… this here works:

$modx->lexicon->load('en:namespace:topic');
$lex = $modx->lexicon('my.placeholder', array(), 'en');

$resource->setTVValue('myTV', $lex);

If I delete array(), things stop working.

So in order to pass in the cultureKey dynamically i need to fetch that.
Why does $modx->getOption('cultureKey'); output “de” in each context?

In fact: it doesn’t matter which SystemSetting i am trying to get. I always get the values from my main context using this: $modx->getOption('SettingName');

edit:
This is my modx_context_setting:

I think the problem is, that when the plugin is executed you are always in the “mgr” context.

So you probably have to get the correct context from the resource $resource->get("context_key") and then use this information for the language.

1 Like

That works. Thanks a lot, @halftrainedharry !

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