Since we can create a custom "Condition" by creating Rules plugins, I created one where I don't need any context, it looks like this:

/**
 * Provides a "Path is admin route" condition.
 *
 * @Condition(
 *   id = "rules_path_is_admin",
 *   label = @Translation("Path is admin route"),
 *   category = @Translation("Path"),
 * )
 */
class PathIsAdmin extends RulesConditionBase implements ContainerFactoryPluginInterface {

I don't need any context, since I am using dependency injection to grab what I need by using the container, however as soon as I hit "save" button, it shows me the following messages:

Warning: Invalid argument supplied for foreach() in Drupal\rules\Form\Expression\ConditionForm->getContextConfigFromFormValues() (line 111 of modules/contrib/rules/src/Form/Expression/ContextFormTrait.php).

Then I debugged and I found this particular "foreach" implementation within "ContextFormTrait" trait:

foreach ($form_state->getValue('context') as $context_name => $value) {

In order to avoid this annoying message, we should provide a default value to "get" method used by $form_state object, like this:

foreach ($form_state->getValue('context', []) as $context_name => $value) {

Reason why I think, we should update that chunk of code.

Comments

keboca created an issue. See original summary.

keboca’s picture

Issue summary: View changes
avpaderno’s picture

Status: Needs work » Active
fago’s picture

Status: Active » Needs review
Issue tags: +Novice, +Contributor

Sounds good - can you provide PR for that?

fago’s picture

Status: Needs review » Needs work
ritzz’s picture

Assigned: Unassigned » ritzz
Status: Needs work » Needs review
StatusFileSize
new738 bytes

Added Default Value for "getValue('context')".

Status: Needs review » Needs work

The last submitted patch, 6: added_default_parameter-2840815-6.patch, failed testing.

ritzz’s picture

Added a condition to check if empty or not.

ritzz’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 8: added_default_parameter-2840815-7.patch, failed testing.

tr’s picture

Status: Needs work » Needs review
StatusFileSize
new2.28 KB

Patch still applies and tests now run green. But the patch introduced a few coding standards errors, so here's a re-roll to fix those:

tr’s picture

@ritzz: You assigned this issue to yourself - are you still planning to work on this?

tr’s picture

Title: Invalid argument supplied for foreach() in Drupal\rules\Form\Expression\ConditionForm->getContextConfigFromFormValues() » Support custom Conditions that don't need context
Assigned: ritzz » Unassigned
Category: Bug report » Feature request
Issue tags: +Needs tests

Still needs some reviews, and wouldn't hurt to add a test case to ensure that Conditions work without a defined context.

tr’s picture

StatusFileSize
new1.45 KB
new3.73 KB

Here's a test-only patch that proves the error. This should FAIL.

Also, here's a patch that includes both the test and the fix from #11. This should PASS, demonstrating that the patch fixes the error.

Status: Needs review » Needs work

The last submitted patch, 14: 2840815-14-fix-plus-test.patch, failed testing. View results

tr’s picture

Status: Needs work » Needs review
StatusFileSize
new5.96 KB
new8.24 KB

Interesting - this why we have testing.

Rules already has some testing-only Conditions which are defined without contexts. But they are only used in Kernel and Unit tests - neither of those uses the Rules UI so any UI-related errors, like this issue, won't be detected by the tests. This Kernel and Unit testing does show that actually USING these Conditions that don't have contexts WORKS in code - it's just a UI issue that we're trying to fix here.

The added test in #13 is a Functional test that enables the 'rules_test' module, which contains these no-context testing Conditions. And when those Conditions become available for the first time in Functional tests we see a new error that hasn't been detected before. Specifically, these test-only conditions have incomplete annotation. Not only are they missing the 'context' annotation but they're also missing the 'category' annotation. While the patch fixes the missing 'context' annotation (we WANT to support conditions without context), the missing 'category' annotation will cause other tests to fail. We DON'T want to support missing category, so the fix here is to add the missing category to the test-only Conditions - and to the test-only Actions while we're at it, since they are also missing the category annotation.

New test-only patch and test-plus-fix patch attached, but the only part that changed is the test part.

The last submitted patch, 16: 2840815-16-test-only.patch, failed testing. View results

tr’s picture

OK, #16 worked exactly as expected - the test-only patch failed, demonstrating the problem, while the test-plus-fix patch PASSED, showing that the fix worked.

  • TR committed 0e1b5e8 on 8.x-3.x
    Issue #2840815 by TR, ritzz: Support custom Conditions that don't need...
tr’s picture

Status: Needs review » Fixed

Committed #16

Status: Fixed » Closed (fixed)

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