Use Views for search

Created on 20 July 2024, about 2 months ago

Problem/Motivation

Currently, it searches through all entered data, which isn't ideal because I only want to show the user their own data. I created a patch that allows the use of Views for search. Now I can filter, sort, use dynamic arguments, and rewrite the output of fields as I want.

Instructions

Sample of limiting entered values by current user for field A at Article content type:

Prerequisites:

Content type “Article” has text field “Field A”.

Create View:


1. Show set to Fields.
2. Add “Field A”.
3. Add filter for “Field A”, set Operator to “Starts with”, “Contain” and so on.
4. You can sort by “Field A”
5. Add Global:Null — here current entity ID provided, but it's NOT used here.
6. Add “Content: Authored by” for filtering by current user.
7. You can enable aggregation to prevent duplication querying.
8. Enabling Distinct will not help as #7, since it's distinct also NID.
9. Pager will override from “field A” widget settings.

“Field A” widget settings


1. Set “In View”
2. Set “Selected View” above view.
3. This count used for View pager limit.

Article edit time:


Enter some text, and you will see only own entered text suggestions.

Code changes

1. The count argument for the existing_values_autocomplete_widget.autocomplete route was removed, and the bundle of the current editing entity was added.
2. Also, I didn't fix PHP CS errors, since it will be fixed on another issue. So it will raise new PHP CS errors — I will provide new patch after committing this issue .
3. I also fix code with missed count and distinct in DB querying in AutocompleteController.php, row #56-57.

Remaining tasks

1. Possibly, caching is required to prevent calculating $field_column and $field_widget each time.

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇷🇺Russia Nikit

Live updates comments and jobs are added and updated live.
  • views

    Involves, uses, or integrates with views. In Drupal 8 core, use the “VDC” tag instead.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024