Closed (fixed)
Project:
Search API
Version:
8.x-1.x-dev
Component:
Views integration
Priority:
Normal
Category:
Feature request
Assigned:
Issue tags:
Reporter:
Created:
24 Aug 2016 at 14:54 UTC
Updated:
1 Mar 2017 at 10:19 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
phenaproximaComment #3
drunken monkeyThe problem is similar to the one in #2782577-4: Fix extraction of configurable properties in processors: currently, extracting processor-added properties at search time isn't completely supported (or, in the case of Views, not at all). You'll have to manually add all fields contained in the aggregated field to the view and then do Views magic to achieve the desired behavior. (Or use Solr, which can be configured to load all indexed fields directly from Solr.)
In the long run, though, we probably want to support this. (Otherwise, we should at least remove them from the options when adding fields.)
Comment #4
phenaproximaI'm unfamiliar with Search API's plumbing, but if you can give me some pointers I'd be happy to take a shot at writing a patch. Adding all the fields to Views is not a feasible solution for my use case (this is something we may want to put into Lightning), so solving the underlying problem is probably the best way forward.
Comment #5
drunken monkeyThanks a lot for the offer, that would be great!
The problem is in
\Drupal\search_api\Plugin\views\field\SearchApiFieldTrait::preRender()– which, unfortunately, might easily be the most complex and ugly method in our whole code base. What it does is extract all necessary property values (necessary for that field handler) from the row's search item into the row object itself, so that they can afterwards be displayed.The first problem is line 316 (current dev – 65743d3): this dismisses properties of datasources different from the current row's datasource, but fails to take datasource-independent properties (
$datasource_id === NULL) into account. It shouldn't dismiss any rows for those properties. ($this->isActiveForRow()in the same line already correctly checks for that (although usingis_null()in there would be better, just in case someone wants to use "0" as a datasource ID).Then, in line 361 we extract the property explicitly by retrieving it from the parent property (or the search item itself, in many cases – including ours). There, you'd instead have to check again whether the property is datasource-independent and the parent is empty and, in that case, instead extract it using the appropriate processor – something like this:
The rest of the code for item value extraction can be taken from
\Drupal\search_api\Processor\ProcessorPluginBase::extractItemValues()– you have to create a dummy item with the field you are interested in and then call$processor->addFieldValues()on it.Also, it seems I didn't think about
NULLvalues used as array keys being automatically cast to empty strings, so we have a bunch of wrong datasource IDs (empty string instead ofNULLflying around the class). We should doif ($datasource_id === '') { $datasource_id = NULL; }between line 299 and 300 and between line 432 and 433.That should do it, basically. Refactoring the code, especially to avoid duplication between this and
\Drupal\search_api\Processor\ProcessorPluginBase::extractItemValues()would also be great, as would be tests.But, as said, I know this is pretty complex code, so I'd also understand if you'll have to give up after all.
Comment #6
phenaproxima@drunken monkey: I asked for pointers, and I got a veritable instruction manual. That is all crystal clear and I think I have a really good shot at fixing this problem now, thanks to you.
Comment #7
phenaproximaHere's a very rough first pass at the patch, based on my decidedly quite limited understanding of Search API's internals. You're right, preRender() is very convoluted...but if you stare at it long enough, it starts to make sense.
Comment #8
drunken monkeyGreat start, thanks a lot for tackling this!
Attached is a revision – the main part is still missing, though. It can just be adapted from the code in
ProcessorPluginBase, though.In any case, I think if we detect a processor-generated property, we need to use completely different code, so that's how I arranged it now.
Feel free to ask back here or ping me on IRC if you don't understand or disagree with some of my changes.
Comment #9
drunken monkeyComment #10
drunken monkeyComment #11
drunken monkeyAre you still (interested in) working on this? Otherwise, please un-assign and I'll try to look at it when I have some time.
Comment #12
phenaproximaI am, because it blocks a feature I'd like to add to Lightning...
Comment #13
drunken monkeyOK, so when do you think you'll be able to work on it?
This is not only blocking a stable release, but also another release blocker (#2650364: Add a "skip access checks" option to the Views relationship plugin), so definitely on the critical path. (But on the other hand, there's still a lot of other release blockers, so it's not totally urgent. But in the next week or two would be good.)
Comment #14
phenaproximaHere's my first shot at filling in the TODO in the #8 patch. It's very hard to grok the API, so I'm sorry if I missed anything. However, under manual testing, this fixes the problem for me.
Comment #15
borisson_I don't really understand everything that's happening here, however as per usual I do have some nitpicks. I feel that this should be reviewed by @drunken monkey to make sure we don't miss anything.
The comment here doesn't apply fully anymore, so let's rewrite that?
We can probably rewrite that if into just one statement to reduce unneeded nesting?
Comment #16
drunken monkeyThanks a lot for your effort here! I know how scary that method is to me, so it's really quite an accomplishment to fix this in a meaningful way!
However, it's still not quite what I had in mind. Please see the attached patch for how I would do this, and please try it out to verify it works correctly!
In any case, thanks again for your work!
Also thanks to Joris for your review!
The problem with that is that it wouldn't give proper type hinting in PhpStorm anymore. Also, the line gets pretty long then. I agree that the additional nesting is undesirable, but I still think it's the better variant.
Comment #17
borisson_LGTM
Comment #19
drunken monkeyLesbians, gays, transgenders and … mutants? Very open-minded of you, admirable!
Committed.
Comment #21
ekes commentedI'm just going to pop this here now. Will open another issue when I've investigated properly. But as far as I can see if you have multiple aggregated_field properties it keeps taking the first one.
Comment #22
ekes commentedFollow-up posted #2856915: Allow the display of more than one processor-generated fields/properties per type in Views