Use Views for search

Created on 20 July 2024, 5 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 author for field A Article content type:

1. Create a View with "field A" for the Article content type.
2. Add the first argument β€” Global: Null argument. Here, the editable entity ID is provided (if found).
3. Add a second argument for the current author β€” to show only user-added field values.
4. Add an exposed search for field A. You can make the exposed search "Contain", "Start with", "Equal", and so on.
5. Go to "field A" form widget settings (/admin/structure/types/manage/article/form-display).
6. Set "How to search?" to "In View".
7. Set the "Selected View" to the View created in step 1.
8. Go to the article add/edit form and try entering text. It should show values entered only the current user.

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.

Remaining tasks

1. Possibly, caching is required to prevent recalculating $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

  • Issue created by @nikit
  • πŸ‡·πŸ‡ΊRussia nikit
  • πŸ‡·πŸ‡ΊRussia nikit
  • πŸ‡·πŸ‡ΊRussia nikit
  • πŸ‡·πŸ‡ΊRussia nikit
  • πŸ‡ΈπŸ‡°Slovakia tomas.teicher

    For me, the #5 solution works OK, but not for multivalue field. When I start to type a text in the textfield, and this text is found in some particular entity, it returns all field values from an entity as one text, where field values are separated with comma.

    Example:
    Entity A has field values:

    • Paris
    • London
    • New York

    Start typing "Lond" in entity B, and autocomplete will return: "Paris, London, New York". This happens even when "Display all values in the same row" option is unchecked in View field settings.

    The problem is in Views itself, in particular in Drupal\views\Plugin\views\field\FieldPluginBase::getValue method. it ignores "same row" option and always returns all field values (from particular entity) in the same row.

    When using "advancedRender" method, as it is done in this patch, then the mentioned problematic Views method is called, too.
    I have no idea, how to solve it, without changing something in Views, anyway.

Production build 0.71.5 2024