When outputting results as rendered entity in a specific view mode, the excerpt field for that entity is not available in the manage display screen.

We can provide this with an extra field, and fill the data dynamicly for that field.

CommentFileSizeAuthor
#33 2983606-33--entity_excerpt_field.patch22.01 KBdrunken monkey
#33 2983606-33--entity_excerpt_field--interdiff.txt2.25 KBdrunken monkey
#32 2983606-32--entity_excerpt_field.patch21.03 KBdrunken monkey
#32 2983606-32--entity_excerpt_field--interdiff.txt18.37 KBdrunken monkey
#30 2983606-30--entity_excerpt_field.patch4.16 KBdrunken monkey
#30 2983606-30--entity_excerpt_field--interdiff.txt666 bytesdrunken monkey
#28 2983606-28.patch3.57 KBborisson_
#28 interdiff.txt701 bytesborisson_
#26 2983606-26.patch3.55 KBborisson_
#26 interdiff.txt1011 bytesborisson_
#24 2983606-24.patch3.22 KBborisson_
#24 interdiff.txt405 bytesborisson_
#22 2983606-22.patch2.95 KBborisson_
#16 2983606-16--entity_excerpt_field.patch3.29 KBsgurlt
#14 2983606-11--entity_excerpt_field.patch2.58 KBdrunken monkey
#14 2983606-11--entity_excerpt_field--interdiff.txt2.5 KBdrunken monkey
#12 Manage_display_excerpt.jpg20.38 KBlolandese
#6 2983606-5--excerpt-field.patch2.55 KBStryKaizer
#2 2983606-2--excerpt-field.patch2.55 KBStryKaizer
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

StryKaizer created an issue. See original summary.

StryKaizer’s picture

Status: Active » Needs review
FileSize
2.55 KB
StryKaizer’s picture

Issue summary: View changes
StryKaizer’s picture

Still todo: we need to disable the entity caching mechanism when rendering entities who have the excerpt field enabled and are being rendered in a search, to prevent showing the wrong excerpt.

Status: Needs review » Needs work

The last submitted patch, 2: 2983606-2--excerpt-field.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

StryKaizer’s picture

Status: Needs work » Needs review
FileSize
2.55 KB

Status: Needs review » Needs work

The last submitted patch, 6: 2983606-5--excerpt-field.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

StryKaizer’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 6: 2983606-5--excerpt-field.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

borisson_’s picture

Issue tags: +Needs tests

The testfail is not related, but we should create a new integration test to fix this.

borisson_’s picture

in #4 you mentioned that we still needed to disable the caching for that field, is that still the case?

lolandese’s picture

FileSize
20.38 KB

Works as expected with the built-in Database backend (thus not using SOLR). Missing a field label though. See screenshot.

Screenshot Manage Display settings

StryKaizer’s picture

Yes, we still need to address that.
Entities are being cached, and as this excerpt is a dynamic result, we need to flag the entity non-cachable when rendering it in a search result with this field enabled.

drunken monkey’s picture

Status: Needs work » Needs review
FileSize
2.5 KB
2.58 KB

Awesome idea, thanks!
Attached are a few small code style corrections for now. Otherwise, yes, we still need a test for this, plus apparently the caching fix (which should also be covered by the test, of course).
But great work so far!

drunken monkey’s picture

Status: Needs review » Needs work
sgurlt’s picture

Status: Needs work » Needs review
FileSize
3.29 KB
drunken monkey’s picture

Status: Needs review » Needs work

Thanks!
In the future, though, please provide an interdiff when posting a new revision of an existing patch!

Regarding the changes: the url.query_args cache context is a good attempt to keep cacheability, but I don’t think this would be enough. Search keywords aren’t guaranteed to come via query parameters – e.g., in the Search API Pages module, they are passed as part of the path.
So, until we have something like a search_api.keys context, I fear we have to stick with a max-age of 0.

The test change seems not really related? That code tests the normal “Search: Excerpt” field, unless I’m mistaken. We still need code that tests this current patch.

judapriest’s picture

Hi,

I tested #14, with search API and Search API page.
On my "search api page", I switched to display result through view mode.

I do have the extrafield in the view mode, but the value is NULL: nothing is rendered for the extrafield. When debugging, it doesn't seem to go through the method render() in SearchApiRow class.

Is it right ?

iyyappan.govind’s picture

Hi

I just applied this patch to search_api module (version 8.x-1.12). But dynamic field is not created because of the below condition. I did following change in the patch then it shows the dynamic field.

from:

if ($entity_type instanceof ContentEntityType) {

to

if ($entity_type instanceof \Drupal\Core\Entity\ContentEntityType) {

Am I right here? Thanks

iyyappan.govind’s picture

Sorry I missed to add name space while applying the patch manually. I applied the patch manually because I got error while applying via GIT. Thanks

scott_euser’s picture

I approached this slightly different in my own module to handle this for now in case it is useful for anyone:

<?php

namespace Drupal\my_module\Plugin\views\row;

use Drupal\search_api\Plugin\views\row\SearchApiRow;

/**
 * Provides a row plugin for displaying a result as a rendered item.
 *
 * @ViewsRow(
 *   id = "search_api_with_excerpt",
 *   title = @Translation("Rendered entity with excerpt"),
 *   help = @Translation("Displays entity of the matching search API item
 *   passing an excerpt"),
 * )
 *
 * @see search_api_views_plugins_row_alter()
 */
class SearchApiRowWithExcerpt extends SearchApiRow {

  /**
   * {@inheritdoc}
   */
  public function render($row) {
    $build = parent::render($row);

    // Pass the #search_api_excerpt to the node template. It becomes available
    // in twig in `{{ elements['#search_api_excerpt'] }}`.
    $build['#search_api_excerpt'] = $row->search_api_excerpt;

    // Set cache to match the parameter used for the highlight.
    $build['#cache'] = $build['#cache'] ?? [];
    $build['#cache'] = array_merge_recursive($build['#cache'], [
      'contexts' => [
        'url.query_args:keywords',
      ],
    ]);
    return $build;
  }
}

Noting the following:

  1. Cache context assumes you are using GET for search
  2. Cache context keywords should match the query string arg for your own search
  3. To add, select View > Format > Show > Rendered entity with excerpt
  4. Becomes available in your `node--view-mode-here.html.twig` as `{{ elements['#search_api_excerpt'] }}`
borisson_’s picture

Assigned: Unassigned » borisson_
Status: Needs work » Needs review
FileSize
2.95 KB

I couldn't figure out a reroll, so reapplied all individual hunks. Next up: removing test change and writing a new testcase.

borisson_’s picture

Status: Needs review » Needs work

Back to needs work.

borisson_’s picture

Status: Needs work » Needs review
FileSize
405 bytes
3.22 KB

I made an oopsie.

Status: Needs review » Needs work

The last submitted patch, 24: 2983606-24.patch, failed testing. View results

borisson_’s picture

Status: Needs work » Needs review
FileSize
1011 bytes
3.55 KB

Forgot another use. Also made a change for readability already.

Status: Needs review » Needs work

The last submitted patch, 26: 2983606-26.patch, failed testing. View results

borisson_’s picture

Status: Needs work » Needs review
FileSize
701 bytes
3.57 KB

Got my local test-env working again, thanks to @wannesderoy, should be green again now. Working on a test.

borisson_’s picture

Assigned: borisson_ » Unassigned

I have no clue how to write a test for this. I actually spent most of the last 2 days working on this but I don't know how to do this.

@drunken monkey please halp?

drunken monkey’s picture

Thanks a lot for moving this along, Joris!

I have no clue how to write a test for this. I actually spent most of the last 2 days working on this but I don't know how to do this.

@drunken monkey please halp?

Oh, wow. Thanks again, then, for that!
Unfortunately, I don’t have the time right now to work on this, either (maybe later this week), but I think it would need a Functional test where you include a view mode config that uses the new search_api_excerpt field and some hook, processor or the test backend to add an excerpt to the results for whose presence you can then check on the search results page.
If you tried that already: which part of it caused problems?

Also, something completely different: Can’t we also support this new field in the RenderedItem processor if called during search results rendering? See attached patch revision.

drunken monkey’s picture

Assigned: Unassigned » drunken monkey
drunken monkey’s picture

Component: General code » Framework
Assigned: drunken monkey » Unassigned
Issue tags: -Needs tests
FileSize
18.37 KB
21.03 KB

OK, took a bit but seems to work fine.
The (Views) test is still a bit bare, maybe it should be expanded to also test proper caching? (E.g., visit the page again and verify that the rendered node was retrieved from cache (not sure, how, though?), then use different query args and verify it didn’t touch the cache. (Probably need to adapt search_api_test_excerpt_field_search_api_results_alter() for that.))

drunken monkey’s picture

Implemented my suggestions from #32.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

Ok, I think this looks done now. +1 Thanks for helping out @drunken monkey :)

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Alright, then, thanks for reviewing again!
Committed.
Thanks a lot again, everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.