Problem/Motivation
All field handlers that provide things like links do not want to do anything to affect the views query. So the current 'API' for this is just overriding the query() method and leaving it empty, so the parent does not get called. It would be nicer to make this explicit, like click sort is declared.
Additionally, other Views plugins need to be able to determine whether or not a field handler influences the query, since those plugins might rely on the fact that the field handler adds one or more fields to the query. See #2401953: Database exception for View with Combined field filter with fields with no query alias for an example of this scenario.
Proposed resolution
Add an isComputed() method to FieldPluginBase, this would default to false. Handlers could then either add 'computed' to the views data implementation using the handler or just overriding the method. The base query method can then check this property.
Remaining tasks
User interface changes
API changes
Beta phase evaluation
Issue category | Task because computed fields always existed, at least on a implicit level. |
---|---|
Issue priority | Normal, because its not such a big improvement. |
Disruption | No disruption at all, just some nicety for people writing views handlers, so both advantages for contrib and custom |
Comment | File | Size | Author |
---|---|---|---|
#92 | 2349465-92.patch | 12.12 KB | webdrips |
#82 | interdiff-2349465-78-81.txt | 4.53 KB | mohit_aghera |
#82 | 2349465-81.patch | 12.21 KB | mohit_aghera |
#78 | 2349465_78.patch | 9.54 KB | vsujeetkumar |
#74 | 2349465-74-views-computed-field.patch | 8.84 KB | vacho |
Comments
Comment #1
damiankloip CreditAttribution: damiankloip commentedComment #4
blackra CreditAttribution: blackra commentedI am in the process of re-rolling the patch so that it applies cleanly. At the same time I am checking it for issues and completeness.
Comment #5
blackra CreditAttribution: blackra commentedThere are two features of this patch that I have noted whilst re-rolling it:
The method isComputed() is public. Is this intentional, given that it is on the 8.0.x branch?
Various classes in core override query() without calling query() in the parent class. This removes the check for isComputed() in any of their children. I think this is okay. However, the classes that do this are:
Comment #6
blackra CreditAttribution: blackra commentedRe-roll of patch in comment #1
Comment #7
blackra CreditAttribution: blackra commentedComment #8
dawehnerGiven that this is just an internal change, this should be fine! It makes it easier for contrib and custom modules.
Comment #9
tstoecklerI don't understand why we introduce support for specifying this in the definition (i.e. in the annotation, right?) but then not actually use it but override the method each time. Seems like the base implementation could just return
FALSE
then, right? Can someone explain?Comment #10
dawehnerWe use the definition pattern in case we have entries in hook_views_data() you would otherwise have not to
create a class. This is though not the case here, because you will always override render() for computed fields.
Nice observation tobias!
Comment #11
tstoecklerSo that means this is "needs work", right? Or did I misinterpret your comment?
Comment #12
damiankloip CreditAttribution: damiankloip commentedYep, sounds like a good point. You would always need to override the class I think. We do the same thing with some other methods, so we would not be introducing anything new here.
Comment #13
blackra CreditAttribution: blackra commentedI just noticed a potential problem with a comment.
In the comments for FieldPluginBase::isComputed() it says:
"The default query behavior will be be invoked if this is TRUE."
Surely that should say FALSE. query() does nothing if isComputed() === TRUE
Comment #14
blackra CreditAttribution: blackra commentedUploaded a new patch (and interdiff) to address comments #9 - #13
Comment #15
damiankloip CreditAttribution: damiankloip commentedI think we should stick with saying TRUE, there is a typo earlier in the line:
We just want that to ay "not be".
Comment #16
dawehnerAlright, let's fix that quickly
Comment #17
rpayanmComment #18
dawehnerFeedback got fixed, thanks!
Comment #19
tstoecklerSure.
Edit: Hehe, wanted to RTBC as well
Comment #20
alexpottThis issue is a normal task so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary.
Comment #21
dawehnerAdded
Comment #22
alexpottWhy does this not get an
isComputed
implementation?Comment #23
dawehnerValid response!
Comment #26
tadityar CreditAttribution: tadityar commentedRe-rolled and added isComputed in
/core/modules/contextual/src/Plugin/views/field/ContextualLinks.php
Comment #27
LendudeNitpicking, double newline
Plus, this needs tests. Probably just something along the lines of testClickSortable() in \Drupal\views\Tests\Handler\FieldUnitTest would be a good start.
Comment #28
LendudeComment #31
Hjarnmastara CreditAttribution: Hjarnmastara at Calibrate commentedComment #32
Hjarnmastara CreditAttribution: Hjarnmastara at Calibrate commentedRerolled the patch as it would no longer apply.
Needs review.
Comment #35
mr.baileysTest should probably also assert that query() is not called on field handlers that have isComputed() === TRUE.
I'm also wondering if we should set isComputed = TRUE for the Broken field handler?
Comment #36
mr.baileysTest should probably also assert that query() is not called on field handlers that have isComputed() === TRUE.
Nvm, I talked to @Hjarnmastara, and since it's query()'s responsibility to check isComputed(), this might be hard to test.
Comment #37
LendudeCouple of minor tweaks
unrelated change
extra indent
Can just be {@inheritdoc}
Comment #38
Hjarnmastara CreditAttribution: Hjarnmastara at Calibrate commentedApplied the minor tweaks.
Comment #39
Hjarnmastara CreditAttribution: Hjarnmastara at Calibrate commentedComment #41
LendudeNeeded a reroll.
Comment #42
LendudeAs per https://www.drupal.org/core/d8-allowed-changes : Provides new functionality or a new API: postpone it to 8.1.x.
So done
Comment #43
LendudeLets open this back up for 8.1.x
Comment #44
dawehnerIMHO this would be still really nice to have.
Comment #45
alexpott#2401953: Database exception for View with Combined field filter with fields with no query alias landed so this patch needs to address the code comment that issue added.
Comment #46
LendudeAs per #45 updated the combine filter to use the isComputed method.
Comment #47
dawehnerYou missed to add it to the
FieldHandlerInterface
Comment #48
LendudeAdded the method to FieldHandlerInterface and moved the doc block there too. Makes sense right? Or not?
Comment #51
mohit_aghera CreditAttribution: mohit_aghera as a volunteer and at Axelerant commentedUpdating patch for 8.2.x branch.
Re-rolled fresh patch as #48 wasn't being applied.
Comment #54
mradcliffeAdded reroll tag. The issue summary could use some updates to reflect comments from @alexpott and current patch.
Comment #55
f4o CreditAttribution: f4o commentedI'm working on this on DrupalCon Dublin mentored sprint, re-rolling a patch using this instructions: https://www.drupal.org/contributor-tasks/reroll.
Comment #56
astimac CreditAttribution: astimac commentedI'm working on this on DrupalCon Dublin mentored sprint, re-rolling a patch using this instructions: https://www.drupal.org/contributor-tasks/reroll.
Comment #57
astimac CreditAttribution: astimac commentedPatch is re-rolled , please have a look
Comment #59
mr.baileysComment #60
mr.baileysSeems the patch in #57 accidentally removed the render() function from the Custom field plugin, causing the test failures. Added it back and fixed some whitespace issues that were introduced.
Comment #61
dawehnerShould this be a follow up or still be resolved as part of this patch?
Nitpick: Its
{@inheritdoc}
:(This is really nice, IMHO
Comment #62
dawehnerFeel free to set it back, but this TODO seems to be a bit dangerous.
Comment #63
mr.baileysThanks @dawehner
Agreed that we should have a default implementation for isComputed, returning FALSE. Only concern is that right now, to "implement" a computed field, a plugin just overrides query(). By introducing isComputed() and having this return FALSE, we are essentially making those existing fields not computed. Might not be a big deal since the isComputed() method is only used in the Combine plugin for now?
Also fixed the {@inheritdoc}, and noticed a documentation error.
Comment #64
Lendude@mr.baileys nice work!
Well we go from 'we assume it isn't computed, and you have no way of telling us it is', to 'we assume it isn't computed, but you can tell us it is'. So I don't think we will be introducing any new false assumptions.
See #2695207: Text (plain, long) field is not click sortable and does not appear in combine fields filter as an example of where the current attempt at assumption goes wrong.
Comment #67
tacituseu CreditAttribution: tacituseu commentedRe-rolled for short array syntax and
\Drupal\views\Plugin\views\field\Field
deprecation.Comment #68
LendudeAdding this to the interface is a BC break (I know I did it myself in #48, the things we learn), anything implementing FieldHandlerInterface but not extending FieldPluginBase will break. I doubt this happens in the wild, but technically it's a BC break. Or are interfaces always internal (since any change to them will always lead to a BC break)?
Also we need some additional test coverage for the introduced 'if's
Needs test coverage
Needs test coverage
Needs test coverage.
Comment #69
tacituseu CreditAttribution: tacituseu commented@Lendude: https://www.drupal.org/core/d8-bc-policy
TLDR: It should be safe to add it in a minor release. NW for tests.
Comment #70
tacituseu CreditAttribution: tacituseu commented#2852067: Add support for rendering computed fields to the "field" views field handler went and did "Update the EntityField plugin to support computed fields.", so can't just return FALSE.
Comment #74
vacho CreditAttribution: vacho at Skilld commentedOnly patch reroll to contribute to this cause.
Comment #75
daffie CreditAttribution: daffie commentedNeeds testing for each of the different handler with a computed field.
Comment #78
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedRe-roll patch created and fixed test, Please review.
Comment #81
LendudeAs @daffie pointed out in #75 we need tests for all the changed handlers that isComputed() does indeed return FALSE, and we need test coverage for the change in query().
Comment #82
mohit_aghera CreditAttribution: mohit_aghera as a volunteer and at QED42 commented@Lendude
I've added test cases for few field handlers. Can you please confirm if the approach is correct?
I am not sure how we can test it.
One test case I saw in
FieldKernelTest.php
where we are testing query usingtestQuery()
method.However, in our case I want to test that whenever
$this->isComputed()
is true, no further code gets related to$this->ensureMyTable();
gets executed.Any idea how can test this one?
Comment #85
smustgrave CreditAttribution: smustgrave at Mobomo commented@mohit_aghera one way to check your tests is to upload a tests-only patch with the full one. So the tests only patch will fail and regular ones passes then you know it was a good test.
Moving to needs work based on your other questions though
Comment #88
webdrips CreditAttribution: webdrips commentedRerolling #82 for 10.2
Comment #89
webdrips CreditAttribution: webdrips commentedComment #90
webdrips CreditAttribution: webdrips commentedIgnore last patch; let's try this again
Comment #91
webdrips CreditAttribution: webdrips commentedComment #92
webdrips CreditAttribution: webdrips commentedThird time is a charm: re-roll for 10.2