Problem/Motivation
\Drupal\Core\Condition\ConditionManager::evaluate()
negates the outcome of condition evaluation results. While ConditionInterface
has an ::isNegated()
method, negation is in fact part of the conditions' implementations and one would expect the negation to performed as such. However, if that is done, ConditionManager
will negate the outcome yet again.
This is bad design at least, and maybe a bug, because plugins should be usable without the default condition manager implementation.
Proposed resolution
- Refactor Condition and Condition Plugins - correcting the evaluate
implementations and isNegated
usage (after "condition" results)
Consider whether to fix or document this inconsistency/bug.
Remaining tasks
Write test
Code review
User interface changes
None.
API changes
T.b.d.
Data model changes
None.
Comment | File | Size | Author |
---|---|---|---|
#76 | 2535896-nr-bot.txt | 90 bytes | needs-review-queue-bot |
Issue fork drupal-2535896
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 #1
andypostand the behaviour should b covered with tests
Comment #2
XanoThe responsibility for executing conditions was moved from the conditions themselves to
ConditionManager
in #1743686: Condition Plugin System. I asked the authors why on Twitter.Comment #3
XanoIt seems
\Drupal\Core\Condition\ConditionInterface::isNegated()
should not have been a public method, as it should be irrelevant to calling code whether a plugin is negated or not (in fact it makes sense for simple boolean conditions not to be negatable at all). The internal configuration concerns only the plugin conditions themselves, and calling code should use the evaluation result directly.Five calls show up in core, of which only one outside condition plugins, and that's in
ConditionManager
in order to negate the results. Three of the remaining calls are in conditions themselves, used to negate the result inConditionInterface::evaluate()
.Comment #4
XanoLet's see what fixing this directly actually breaks, so we can decide on the best course of action.
Comment #6
XanoThis fixes all test failures and adds a test for
ConditionManager
.Comment #8
XanoComment #10
andypostI'd better change the docs for negated... but +1 to approach
implementation details of
isNegated()
should be able to access config and do some custom logic butreturn !empty($this->configuration['negate'])
by defaultneeds a point about removal
scope-creep?
Why not configuration accessed directly?
return $this->isNegated() ? ..
will make more sense
Comment #11
XanoIt's not directly related to the issue, but it makes sure IDEs know about the
::evaluate()
method that is called there, and adds some error handling in case someone passes on an invalid value.Comment #13
dawehnerIsn't that a usecase of an assertioN?
Comment #14
XanoPHP 5.5 goodies!
What do you mean with an assertion in this context?
Comment #15
dawehnerSee #2536560: Runtime Assertion unit and functional testing
Comment #17
dawehnerShouldn't we add some form of test coverage for all those fixes?
Mh, so this method doesn't have any assertions ... isn't there something we could check for?
Comment #18
XanoBesides the obvious feedback: why are tests without assertions that bad? I figured that just running code and verifies it does not causes errors is some sort of a test.
Comment #25
Pavel Ruban CreditAttribution: Pavel Ruban as a volunteer and at FFW commenteddo we have the patch for core 8.7.x? As current one doesn't apply & 8.7 still has this negate issue
Comment #26
tim.plunkettRerolled for 8.9 (which is incidentally the same as 8.7 in this case)
I'm not sure what to do with isNegated(). It's not that it needs to go away, as it is important to the implementations. But it shouldn't be on the interface. For now I just turned the @deprecated into a doc comment.
Comment #28
andypostshould be removed
Comment #29
andypostClean-up and fix test but not sure the test is right
Comment #36
vasikeIt seems this doesn't cover the EntityBundle conditions
@see https://www.drupal.org/project/drupal/issues/3255496 - closed as it seems part of this.
Comment #37
ravi.shankar CreditAttribution: ravi.shankar at OpenSense Labs commentedAddressed Drupal CS issue of patch #29, keeping the status needs work for comment #36.
Comment #38
Anchal_gupta CreditAttribution: Anchal_gupta at Srijan | A Material+ Company for Drupal India Association commentedI have fixed CS error. please review it
Comment #39
vasike2 updates for the latest patch
1. use
$this->isNegated()
, instead of$this->configuration['negate']
2. Updated also for Entity Bundle condition, as specified on #36
Comment #40
vasike2 more updates:
1. We don't need the "initial"
$this->isNegated()
condition, besides the ones about condition configuration check.2. UserRole condition updated.
Comment #42
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
As a bug this will need a test case
Comment #44
vasikeMR created from the latest patch ... for anyone to contrib.
+ Added missing test file - it got lost in re-rolling/updating patches
+ Add negated condition for
core/tests/Drupal/KernelTests/Core/Entity/EntityBundleConditionTest.php
But dunno what tests could be created that would fail for current core ... before those updates.
Comment #45
vasikeComment #46
smustgrave CreditAttribution: smustgrave at Mobomo commentedTests appear to have been added so removing that tag.
Can the issue summary be updated, specially the proposed solution. Seems to be open ended.
Comment #48
vasikeissue summary updated
Comment #49
smustgrave CreditAttribution: smustgrave at Mobomo commented11.x is the latest development branch
Comment #50
smustgrave CreditAttribution: smustgrave at Mobomo commentedIssue summary had been updated so removing that tag.
Test coverage seems good so think this is good for committer review too.
Comment #51
longwaveThere are surely backward compatibility issues here. Contrib and custom plugins will need updating to handle isNegated internally, now the ConditionManager doesn't do it for them?
Comment #52
smustgrave CreditAttribution: smustgrave at Mobomo commentedSo you think the safer bet would be to deprecate isNegated from ConditionManager?
Comment #53
vasikei think we're still Need Review, as there is no "clear" ToDo.
imho, i can't see why we need to "deprecate"
isNegated
.i don't think
$this->configuration['negate']
is an "universal" config/option for all condition plugin.As a reminder: the issue is to (re)move
isNegated
usage from ConditionManager to Condition plugin level + make sure is properly used.Maybe we could have this
return $condition->isNegated() ? !$result : $result;
in the ConditionPluginBase .. similar withbuildConfigurationForm
implementations and usage for condition plugins.Comment #54
smustgrave CreditAttribution: smustgrave at Mobomo commented@longwave thoughts on
Comment #55
smustgrave CreditAttribution: smustgrave at Mobomo commentedHiding patches as work appears to be in the MR.
@vasike can the MR be updated to 11.x please maybe with return $condition->isNegated() ? !$result : $result;
Comment #57
longwaveWondering if a possible way forward here is to add NegatableConditionInterface and/or NegatableConditionBase which signifies that a condition allows negation and will handle it internally, and then we can deprecate it from ConditionManager? But if most conditions allow negation this is still quite a change for them all to make.
Comment #58
vasikenew MR for 11.x available.
This moves
isNegated
checking toConditionPluginBase
new "helper"evaluateIsNegated
But still not sure.
Maybe @longwave has a point with #57 suggestions ... but still i can't see "the ideal" solution.
However, for now ... Needs Review
Comment #59
vasikesorry, But
Reminder: This is a Bug, so i think this should be fixed (already).
And if the fix is not ideal. then create follow-up ticket to chase the perfect solution of Negated Conditions.
Comment #60
borisson_http://codcontrib.hank.vps-private.net/search?text=configuration%5B%27ne...
There are multiple pages of plugins out there that are using the same negated, so we have to keep BC in mind here, I think I agree with having an interface to mark them as being able to do it themselves + a deprecation is needed here.
Comment #62
larowlanYes, I think #57 is the best way forward here
I have client projects with lots of condition plugins that would need to be communicated this change.
With #57 we can trigger a deprecation error if the condition plugin doesn't implement the new interface, which gives us a way to eventually consolidate them.
Comment #63
vasikeUpdated the MR - NegatableConditionInterface and NegatableConditionBase added and related updates.
a review it would be nice
Comment #64
smustgrave CreditAttribution: smustgrave at Mobomo commentedI don't see the deprecation
Comment #65
vasike@smustgrave i didn't say i added deprecation.
just the interface and the "base class"
and i asked for a review ... for those things ...
maybe there are other things ... to make them "nice" ... before the final touch
Comment #66
smustgrave CreditAttribution: smustgrave at Mobomo commentedleft some comments
Comment #67
vasike@smustgrave Type hints added. thanks
But unfortunately, it started failing for spellcheck ... i thought it was the new class name(s) - but not sure.
also not sure what are the "related" updates on CI ... "any help will be helpful"
Comment #68
smustgrave CreditAttribution: smustgrave at Mobomo commentedSeems you got bit by #3401988: Spell-checking job fails with "Argument list too long" when too many files are changed there's a workaround post on there.
Comment #69
andypostrebased again and 11.x branch as well
Comment #70
andypostand cleaned-up rebase
Comment #71
vasike@smustgrave / @andypost you're my brain saviours
thanks ... cleaned up my last changes. it looks "green" ... even it tried to "do it red"
then ... let's review
Comment #72
smustgrave CreditAttribution: smustgrave at Mobomo commentedBelieve all feedback has been addressed so +1 from me but leave in review for some additional eyes to avoid ping ponging around.
Comment #73
smustgrave CreditAttribution: smustgrave at Mobomo commentedBeen a few days and don't want the issue to stale so going to mark.
Comment #74
quietone CreditAttribution: quietone at PreviousNext commentedI'm triaging RTBC issues.
Ah, a 9 year old issue. I like seeing the older issues getting near completion.
I read the IS and the comments. I didn't find any unanswered questions. However the proposed resolution does not match the direction taken in #57. This is more that a refactor, it should include that an interface is added. And also a CR.
I made comments in the MR that need to be addressed.
Changing to NW
Comment #75
vasike@quietone thanks a lot for the reviews (issue and MR/code) and their feedback
Update the MR trying to cover all of threads ... it looks nicer ... at least
However, i expected a feedback about #57 changes i added ... like a confirmation ... so we could update the issue summary ... add change record ...
But, i saw no one.
Maybe @longwave, as "initiator" could check it.
Comment #76
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #77
vasikesolved MR conflicts.
Comment #78
longwaveAfter looking at the new approach I'm still not entirely convinced this is worthwhile, and this only feels like a stopgap; somehow we have to later remove non-NegatableConditionInterfaces, but also we probably want to allow conditions to not actually support negation (like in the boolean case where it makes no sense), but not sure how to achieve that.
Comment #79
longwaveThis is ultimately a framework manager decision so let's tag them for input.
Comment #80
catchLike #78 I'm not sure whether this gets us to the eventual end point and it doesn't look like much of an improvement on its own, if that's known, can follow-up issues be opened with an explanation?
I would be useful if the issue summary and a change record were fleshed out too, so marking needs work for that.