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.
Issue fork commerce_promo_bar-3631082
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
valicUpdate task
Comment #4
valic