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

  1. Enable the Mida module and visit /admin/config/services/mida.
  2. Under Visibility > Roles, check "Anonymous" and save.
  3. Inspect the saved configuration (drush config:get mida.settings visibility): the user_role condition has no context_mapping key.
  4. 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

Command icon 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

ltrain created an issue. See original summary.

ltrain’s picture

Version: 1.0.1 » 1.0.x-dev
Status: Active » Needs review

  • j0sbeir committed 4a29410a on 1.0.x authored by ltrain
    Resolve #3608809 "Visibility conditions with required context (e.g....
j0sbeir’s picture

Status: Needs review » Fixed

Thanks! Looks good.

And sorry for the delay :-)

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.