Problem/Motivation

The rules that decide which promo bar shows on a page only run inside PromoBarBlock, so headless front ends, REST/JSON:API resources and mobile endpoints can't reuse them:

- PromoBar::evaluateVisibility() is static and always reads the current request path. An API request is asking about a front-end page, not its own URL.
- Body tokens are only replaced in commerce_promo_bar_preprocess_field(), so any output that doesn't render the entity contains raw tokens. The preprocess also drops the tokens' cacheability, so for example a promo bar using [site:name] isn't invalidated when the site name changes.
- FilterPromoBarsEvent carries only the bars and the store. Subscribers can't see the page or the customer, and they can't add cache contexts for what they filter on.
- The block computes its max-age itself, so other callers get no expiry.

Small issues found along the way:

- The Dismissible description says "per session", but dismissals are stored in localStorage and persist.
- PromoBar::getDescription() throws a TypeError when the body is empty.
- The promotion field uses the inline_entity_form_complex widget, which is the module's only use of Inline Entity Form.

Steps to reproduce

Proposed resolution

The rules that decide which promo bar shows on a page only run inside PromoBarBlock, so headless front ends, REST/JSON:API resources and mobile endpoints can't reuse them:

- PromoBar::evaluateVisibility() is static and always reads the current request path. An API request is asking about a front-end page, not its own URL.
- Body tokens are only replaced in commerce_promo_bar_preprocess_field(), so any output that doesn't render the entity contains raw tokens. The preprocess also drops the tokens' cacheability, so for example a promo bar using [site:name] isn't invalidated when the site name changes.
- FilterPromoBarsEvent carries only the bars and the store. Subscribers can't see the page or the customer, and they can't add cache contexts for what they filter on.
- The block computes its max-age itself, so other callers get no expiry.

Small issues found along the way:

- The Dismissible description says "per session", but dismissals are stored in localStorage and persist.
- PromoBar::getDescription() throws a TypeError when the body is empty.
- The promotion field uses the inline_entity_form_complex widget, which is the module's only use of Inline Entity Form.

Remaining tasks

- Review the MR.

Tests in the MR:

- Kernel tests:
- visibility: explicit vs current path, show/hide, aliases both ways, the front page, and the deprecated method;
- the body processor, inside and outside rendering;
- loadVisible() per page and customer;
- the path, account and cacheability in event subscribers;
- the max-age;
- the empty body;
- the post-update.
- A functional test of the promotion widget without JavaScript.

User interface changes

- The promotion field becomes a None / Existing promotion / New promotion choice instead of the Inline Entity Form widget. A promotion that's already linked is edited in the promotion's own form.
- The Dismissible field has a corrected description.

API changes

- New services: commerce_promo_bar.visibility (PromoBarVisibilityInterface) and commerce_promo_bar.body_processor (PromoBarBodyProcessorInterface).
- New method PromoBarStorageInterface::loadVisible().
- FilterPromoBarsEvent gains the optional $path, $account and $cacheability constructor arguments, plus getPath(), getAccount() and getCacheability().
- PromoBar::evaluateVisibility() is deprecated in 1.0.7 and will be removed in 2.0.0. Use PromoBarVisibilityInterface::isVisible() instead.
- New field widget commerce_promo_bar_promotion.

Data model changes

- None. commerce_promo_bar_post_update_promotion_widget() updates saved promo bar form displays that use the IEF widget.

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

valic created an issue. See original summary.

valic’s picture

Title: Resolve visible promo bars outside the block (explicit path, token replacement, event context) » Resolve visible promo bars outside the block, and small fixes
Issue summary: View changes

Update task

valic’s picture

Status: Active » Fixed

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.