Problem/Motivation
The Mida A/B testing scripts silently fail to appear on any page when a visibility condition is configured under Configuration > Mida A/B Testing Settings (/admin/config/services/mida) that requires context — for example restricting visibility to the "Anonymous" role.
MidaSettingsForm::buildVisibilityInterface() builds each visibility condition's config form, but never sets the gathered_contexts temporary value on the form state before doing so. ConditionPluginBase::buildConfigurationForm() relies on that value to render a context_mapping element (the piece that tells a context-aware condition, like user_role, which runtime context to bind to). Without it, no context_mapping is ever built or saved.
At runtime, MidaScriptAttachment::evaluateVisibility() then calls ContextRepository::getRuntimeContexts() with an empty context ID list (since $condition->getContextMapping() returns nothing), so the condition's required user context is never resolved. ContextHandler::applyContextMapping() throws a MissingValueContextException, which is caught silently and treated as "condition failed" — so attach() returns early and the scripts never get added to the page, with no error surfaced anywhere.
Steps to reproduce
- Enable the Mida module and visit
/admin/config/services/mida. - Under Visibility > Roles, check "Anonymous" and save.
- Inspect the saved configuration (
drush config:get mida.settings visibility): theuser_rolecondition has nocontext_mappingkey. - View source of any page as an anonymous visitor: the anti-flicker (
abhide) and optimize scripts are absent, with no visible error or log entry.
Proposed resolution
In MidaSettingsForm::buildVisibilityInterface(), gather the available contexts once and store them via $form_state->setTemporaryValue('gathered_contexts', $contexts) before building each condition's configuration form — mirroring the pattern core's own block visibility form uses in BlockForm::form(). This lets ConditionPluginBase::buildConfigurationForm() build and save a working context_mapping for any context-aware condition (user_role, entity_bundle:*, etc.), so they resolve correctly at runtime instead of throwing MissingValueContextException.
Patch attached, including a Kernel test (tests/src/Kernel/MidaSettingsFormTest.php) that reproduces the bug against unpatched code and passes once the fix is applied.
Remaining tasks
- Review and merge the attached patch/MR.
- Confirm no other context-aware conditions (e.g.
entity_bundle:*) need additional handling beyond this fix.
User interface changes
API changes
Data model changes
Issue fork mida-3608809
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
ltrainComment #5
j0sbeir commentedThanks! Looks good.
And sorry for the delay :-)