Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When using views to submit a search, we need an optional option to choose if we want to search in attached files too or not.
Proposed resolution
Support for views is done in D7.
Remaining tasks
Port this issue to D8
Original report by [dolu]
Hello,
I would like to know if an option can be configured to let the user choose if he wants to include search into files in the results.
Thank you!
Comments
Comment #1
Ludo.RComment #2
izus CreditAttribution: izus commentedi think this may be part of 'tools' that are used to execute the search, like search api pages or views, maybe search_api_attachments can provide a views filter for this...i'm just dropping ideas, if you have some architected solution please share it to know if we can have this in. or even better, if we can see some code :)
Thanks
Comment #3
Ludo.RWell, thank you for your answer.
I guess that a (exposed) Views filter would do the trick, but this is a just a thought.
If I need to investigate further on (and implement) that feature, hopefully I'll provide some code to review.
Thanks!
Comment #4
Ludo.RHi,
I need to implement this feature.
Could you provide me some basic information on how should I proceed?
Building a Views plugin/handler is ok, but how should I talk with SearchAPI to tell him not to return file results, or return file results only?
Thanks!
Comment #5
izus CreditAttribution: izus commentedhi,
i think that what is need here is a views exposed filter.
i'm not sure, but i think it's possible to have this feature without diving in search api. Only diving in views:
actually in the view configuration we can configure which fields we want to search throw.
the idea is to do some 'live' reconfiguration of that before sending the request. some hook_views_query_alter probably
Comment #6
Ludo.ROk, then this solution would be specific to
search_api_views
(which seems to be the default search results display as it is shipped with search_api).But then probably a solution would be needed for
search_api_page
. And in that case one can't usehook_views_query_alter
to filter out results.So I guess that a generic function should be created that would filter out results by hooking into Search API. Then a views filter for search_api_views and a hook_form_alter for search_api_page.
How does it sound to you?
If you can give me some tips on how to implement the Search API filter function, I would be happy to implement it as well as the views plugin.
Otherwise, I'll implement your first thought in my custom site_helper module (I can share code though is someone wants).
Comment #7
izus CreditAttribution: izus commentedi'm ok for both solutions
supporting views and search_pages is ok too (there aren't many tools other that those i think)
to let the user choose the behaviour before submitting the form, the first solution seems natural for me; The tool (views, search_page) should provide the filter and honor it.
the second solution will require the tool to provide the filter and search_api to honor the filter. why not after all if well documented.
for the search_api filter, i'm not very familiar with it, but if i should develop one, i would start from one of those filters implemented by search_api itself. #drupal-search-api chan server can help too i guess.
for both solutions, seeing some code here and adding this feature is much appreciated.
Thanks
Comment #8
Ludo.RMy approach for now, is to override the Search API Views's fulltext search field, which contains configuration to include/exclude fields.
So it would be quite easy to reconfigure those fields at runtime.
However I wonder if this solution is generic enough.
It sounds weird but one could have a search view without the fulltext search field.
So I'm asking myself if there should be a new filter, on its own, but then I need to find a new way to alter the query.
What do you think?
Comment #9
izus CreditAttribution: izus commentedyou're right! only fulltext may be restrictive
i'm really not very familiar with search_api deep integration, try asking the serach_api guys in #drupal-search-api irc chan or in a support request issue.
as said on #7 we will still need a views filter and form_alter filter options if we want the user to be able to switch from searching only in files/ files and content/ only content. i don't know if a search_api filter can expose those.
Comment #10
Ludo.RI'm stuck with the views plugin declaration.
Right now I'm implementing
hook_views_plugin()
:But I this doesn't appear as a views filter, since I don't how to tell this should be a filter.
This is from
search_api_views.views.inc
:This lets me believe that I cant' add a filter, it can only be overriden.
Well, as you can understand, I need some directions here to go further.
Comment #11
izus CreditAttribution: izus commentedhi,
try finding an existing module that adds a views filter and adapt from it. that's what i will do in your place. per example here is an article that seems to have a good example http://precessionmedia.com/blog/how-create-custom-filter-handler-views
Comment #12
Ludo.RHere is a first try.
It works as is, but still needs to be improved at the UI level: finding the right options (include/exclude, both?), labels, exposed options...
Comment #13
Ludo.RSorry, 2 files were missing.
Don't use #12 patch! (it simply won't work).
Comment #14
Ludo.RComment #15
izus CreditAttribution: izus commentedhi,
i can't seem to test the feature here.
i can see the filter in the list of filters in views ui but can't configure it or test it
Comment #16
izus CreditAttribution: izus commentedok
i found out why i couldn't have it working.
it's because of the table name length in hook_views_data_alter
i renamed it saa_exclude_attachments
also moved the filter to views/includes folder and reworked a little some methods in the filter.
please test it and add anything it seems needed for you.
i personally tested, and it seams ok for views. we can focus on this patch and after that add another one for search pages.
Comment #17
GrimreaperHello,
I have tested the patch from comment 16 and it works fine.
Just two remarks:
- Maybe putting the include file in includes/views instead of views/includes. As you want.
- coding standards: Implementation of hook_views_api(). => Implements
Thanks for the patches.
Comment #18
izus CreditAttribution: izus commentedchanged the summary
butting back to needs work (for suggestions in #17)
will merge the views patch when it's ready and wait for a patch for search_api_page :)
Comment #19
GrimreaperOk,
Here is the patch with the modifications and some coding styles fix.
Comment #21
izus CreditAttribution: izus commentedmerged the view support and update issue summary with remaining tasks :
Support for views is merged but needs documentation in README file
Support for search_api_page is to be done.
Thanks all
Comment #24
izus CreditAttribution: izus commentedComment #25
izus CreditAttribution: izus commentedComment #26
izus CreditAttribution: izus commentedjust found a nasty error:
in the views exposed form page.
the value of text form : nothing, just leave it empty
the value of the exclude search in attechments checkbox : checked
when submitting the form, a nasty error is shown.
will upload a patch for this right now
Comment #29
izus CreditAttribution: izus commentedas nobody seems interested in adding support for search_api_page + search_api_page will probabely not be ported to D8 as views is now in core (https://www.youtube.com/watch?feature=player_detailpage&v=aUrdJLPq1FQ#t=...), i will not keep this task for D7.
if someone is interested in it, please create a new task for it.
i'm moving this task to D8 to add views support.
Comment #30
izus CreditAttribution: izus commentedComment #31
izus CreditAttribution: izus commentedComment #32
izus CreditAttribution: izus commentedComment #33
GrimreaperHello,
Here is my first attempt.
I managed to get a filter and to add it to the view.
Before dealing with the exposed filter option. I tried to force the exclusion.
I can't figure out how to alter my other fulltext exposed filter.
Maybe we can add an option to the filter to select specific fulltext fields. That's for later when the basic feature will work.
Thanks for any help.
PS: I had a lot of problems configuring the search api attachments settings form. We can talk about it in another issue or IRL.
Comment #34
izus CreditAttribution: izus commentedOMG, that's a lot of code ! i didn't dug yet in drupal 8 views api, so no clue on this for now.
keep going :)
and yes we can talk about the configuration IRL or via issue queue.
Comment #35
GrimreaperLOL :D
A big part of the code is a copy/paste from:
- search_api/src/Plugin/views/filter/SearchApiFulltext.php
and
- search_api/src/Plugin/views/filter/SearchApiFilterBoolean.php
I hesitated which one to extends, then I chose SearchApiFilterBoolean.
Don't loose time inspecting the commented code. :)
Before I forget:
About the errors on the settings form, I had that:
- "expected parameter to be array, null given"
- some array keys non-existing
Mainly due to when installing, extraction_method = '' (cf. config/install yml files) and the code is not managing that.
Comment #36
izus CreditAttribution: izus commentedHi,
now that views API seems stable, i gave it another try.
following patch adds a boolean select box to exclude/include search in attachments.
(i'd love to make it just a checkbox but for now i'm glad we have sth that seems working)
Comment #38
izus CreditAttribution: izus commentedmerged it
Thanks all