magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Extend Observer declaration in events.xml by new attribute ifconfig

Open bartekAtconvert opened this issue 1 year ago • 18 comments

Description (*)

Extension of Observer declaration in events.xml by new optional attribute ifconfig with appropriate handling of it. Similar to usage of ifconfig in layout's xml files - it executes (or not) Observer depending on boolean value of given config path. The purpose is to condition Observer's execution on config paths representing module's state - usually <module/general/active> or <module/general/enabled> etc.

Manual testing scenarios (*)

  1. Add ifconfig attribute to Observer A declaration in events.xml file where its value will be config path and dispatch event to trigger Observer. a) When config path value equals to true - Observer should execute. b) When config path value equals to false - Observer should not execute c) When config does not exist (path does not represent any actual value) - Observer should not execute
  2. Add ifconfig attribute to Observer A declaration in events.xml file where its value is empty string "" and dispatch event to trigger Observer. Observer should execute.

Contribution checklist (*)

  • [X] Pull request has a meaningful description of its purpose
  • [X] All commits are accompanied by meaningful commit messages
  • [X] All new or changed code is covered with unit/integration tests (if applicable)
  • [X] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [X] All automated tests passed successfully (all builds are green)

Resolved issues:

  1. [x] resolves magento/magento2#38514: Extend Observer declaration in events.xml by new attribute ifconfig

bartekAtconvert avatar Mar 06 '24 16:03 bartekAtconvert

Hi @bartekAtconvert. Thank you for your contribution! Here are some useful tips on how you can test your changes using Magento test environment.

Add the comment under your pull request to deploy test or vanilla Magento instance:
  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

:exclamation: 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 :information_source: 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.

m2-assistant[bot] avatar Mar 06 '24 16:03 m2-assistant[bot]

Failed to run the builds. Please try to re-run them later.

@magento run all tests

bartekAtconvert avatar Mar 06 '24 16:03 bartekAtconvert

@magento create issue

engcom-Hotel avatar Mar 12 '24 11:03 engcom-Hotel

Failed to run the builds. Please try to re-run them later.

@magento run WebAPI Tests

bartekAtconvert avatar Mar 12 '24 13:03 bartekAtconvert

Failed to run the builds. Please try to re-run them later.

@magento run Unit Tests

bartekAtconvert avatar Mar 14 '24 14:03 bartekAtconvert

@magento run Static Tests

bartekAtconvert avatar Mar 18 '24 10:03 bartekAtconvert

@magento run Unit Tests

bartekAtconvert avatar Mar 18 '24 11:03 bartekAtconvert

@magento run Static Tests

bartekAtconvert avatar Mar 20 '24 08:03 bartekAtconvert

@magento run WebAPI Tests

bartekAtconvert avatar Mar 20 '24 08:03 bartekAtconvert

@magento run Database Compare

bartekAtconvert avatar Mar 20 '24 12:03 bartekAtconvert

@magento run Static Tests

bartekAtconvert avatar Mar 21 '24 11:03 bartekAtconvert

@magento run WebAPI Tests

bartekAtconvert avatar Mar 22 '24 10:03 bartekAtconvert

Ha I was just running into this this week! I was wondering whether it existed.

My usecase; I wanted to add an ifconfig here in events.xml that checks for this config value from system.xml so I didn't have to do this in the observer.

peterjaap avatar Apr 11 '24 07:04 peterjaap

@magento run all tests

bartekAtconvert avatar Apr 19 '24 12:04 bartekAtconvert

@magento run all tests

bartekAtconvert avatar Apr 22 '24 10:04 bartekAtconvert

@magento run all tests

engcom-Hotel avatar Jun 05 '24 05:06 engcom-Hotel

@magento run all tests

engcom-Hotel avatar Jun 07 '24 05:06 engcom-Hotel

@magento run Functional Tests B2B, Functional Tests EE, Functional Tests CE, Integration Tests, WebAPI Tests

engcom-Hotel avatar Jun 07 '24 07:06 engcom-Hotel

@bartekAtconvert,

Thank you for the contribution and collaboration!

✔️ QA Passed

Steps to reproduce:

  1. Add ifconfig attribute to Observer A declaration in events.xml file where its value will be config path and dispatch event to trigger Observer. a) When config path value equals to true - Observer should execute. b) When config path value equals to false - Observer should not execute c) When config does not exist (path does not represent any actual value) - Observer should not execute
  2. Add ifconfig attribute to Observer A declaration in events.xml file where its value is empty string "" and dispatch event to trigger Observer. Observer should execute.

Before: ✖️ No ability to condtion the execution of observer.

After: ✔️ Able to conditionalize observer execution.

Created a module with observer, and added ifconfig in events.xml, observer is getting trigerred when config path in ifconfig is true.

Please refer the screenshot:

  • Created a system configuration for ifconfig attribute, when this is enabled or the path for ifconfig is empty the observer is trigerred and it will be logged(for testing purpose). image log output:
[2024-06-11T10:06:03.927013+00:00] main.DEBUG: custom event Triggered [] []
  • If the config is disabled or if the config path passed to ifconfig is incorrect, the observer will not be trigerred and the value will not be logged. image

Please find the custom module used to test this PR. P38493V.zip

Thank you.

engcom-November avatar Jun 11 '24 10:06 engcom-November

As the linked issue is tagged as feature request, moving this to on hold for PO confirmation.

engcom-November avatar Jun 11 '24 10:06 engcom-November

As the linked issue is tagged as feature request, moving this to on hold for PO confirmation.

We have a confirmation from Product Owner on this feature request issue hence moving it back to Extended Testing.

engcom-Charlie avatar Dec 18 '24 06:12 engcom-Charlie

@magento run all tests

engcom-Dash avatar Dec 20 '24 13:12 engcom-Dash

@magento run all tests

engcom-Dash avatar Dec 23 '24 07:12 engcom-Dash

Raised internal approval Jira for the SVC failure. Moving this PR to Pending Approval now. We will proceed ahead on this once will get all the required approvals. image

engcom-Dash avatar Dec 23 '24 10:12 engcom-Dash