Skip to content

Resolve store information lazily in config directive (#37815) - #41059

Open
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/37815-config-directive-store-information
Open

Resolve store information lazily in config directive (#37815)#41059
lbajsarowicz wants to merge 1 commit into
magento:2.4-developfrom
lbajsarowicz:fix/37815-config-directive-store-information

Conversation

@lbajsarowicz

Copy link
Copy Markdown
Contributor

Description (*)

Magento\Email\Model\Template\Filter::configDirective() resolved the store information object unconditionally, before checking which config path was actually requested:

$store = $this->_storeManager->getStore($storeId);
$storeInformationObj = $this->storeInformation->getStoreInformationObject($store);
if (isset($params['path']) && $this->isAvailableConfigVariable($params['path'])) {
    ...

Magento\Store\Model\Information::getStoreInformationObject() loads two entities from the database:

if ($info->getRegionId()) {
    $info->setRegion($this->regionFactory->create()->load($info->getRegionId())->getName());
}
if ($info->getCountryId()) {
    $info->setCountry($this->countryFactory->create()->loadByCode($info->getCountryId())->getName());
}

Only two config paths need that data — general/store_information/country_id and general/store_information/region_id. Every other {{config path="..."}} directive paid for a region load plus a country load and then discarded the result. Because Magento\Cms\Model\Template\Filter extends this class, a CMS page or block with N {{config}} / {{store}} directives performed 2×N redundant entity loads on every uncached render.

This change:

  1. Resolves the store information object lazily — only inside the two branches that consume it.
  2. Memoizes it per store id, so a template using both the country and the region directive (or repeating either) resolves it once instead of once per directive.

Behaviour of the directive output is unchanged.

Fixed Issues (if relevant)

Fixes #37815

Manual testing scenarios (*)

  1. Disable Block HTML and Full Page cache.
  2. Create a CMS block containing many directives, e.g. 200 repetitions of:
    {{store url=""}}
    {{config path="web/secure/base_url"}}
    
  3. Render the page and profile it (Blackfire / SPX / query log).
  4. Before: Magento\Directory\Model\Region::load and Magento\Directory\Model\Country::loadByCode are called once per directive.
    After: neither is called at all for these paths.
  5. Set Stores > Configuration > General > Store Information country and state/province, then render a CMS block with:
    {{config path="general/store_information/country_id"}}
    {{config path="general/store_information/region_id"}}
    
    Both still output the resolved country and region names, and the store information object is resolved once.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

configDirective() resolved the store information object before checking
which config path was requested. Magento\Store\Model\Information loads a
region and a country entity from the database, so every {{config}} and
{{store}} directive in a CMS block or email template paid for two entity
loads that only the country_id and region_id paths actually use.

Resolve it inside those two branches only, and memoize it per store so a
template using both directives resolves it once.

Fixes magento#37815
@m2-assistant

m2-assistant Bot commented Jul 27, 2026

Copy link
Copy Markdown

Hi @lbajsarowicz. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@lbajsarowicz

Copy link
Copy Markdown
Contributor Author

@magento run all tests

@engcom-Charlie engcom-Charlie added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Jul 28, 2026
@github-project-automation github-project-automation Bot moved this to Pending Review in Pull Requests Dashboard Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: pending review

Projects

Status: Pending Review

Development

Successfully merging this pull request may close these issues.

Using directives in Cms Blocks / Cms Pages is causing same data loaded multiple times from Country and Region classes.

2 participants