All views have the same content when you place viewfield twice with different args.
Steps to reproduce:
1. Create Content Type with a reference to taxonomy.
2. Create some nodes with a term ID 1 and several nodes with a term ID 2.
3. Create view “Node lists” with the list of nodes and contextual filter by taxonomy (Content: Has taxonomy term ID).
4. Create Custom block type with a viewfield and view “Node list”.
5. Create two blocks - one with a viewfield’s argument 1 and another one with an argument 2.
6. Place those blocks on the page.
Expected result: Views in blocks show different lists of nodes.
Current result: Views show the same nodes.
Some debug showed me that I have the same cache keys for both views after $view->buildRenderable($display_id, $arguments) here in viewfield/src/Plugin/Field/FieldFormatter/ViewfieldFormatterDefault.php
if (!empty($view->result) || $always_build_output) {
$elements[$delta] = [
'#theme' => 'viewfield_item',
'#content' => $view->buildRenderable($display_id, $arguments),
'#title' => $view->getTitle(),
'#label_display' => empty($view->result) ? $empty_view_title : $view_title,
'#delta' => $delta,
'#field_name' => $this->fieldDefinition->getName(),
'#view_id' => $target_id,
'#display_id' => $display_id,
];
}
I can turn off cache keys by $view->buildRenderable($display_id, $arguments, FALSE) and all works well but it's not the solution.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | viewfield-same_view_arguments-2892073-11.patch | 1.73 KB | pookmish |
| #11 | interdiff-4-11.txt | 1.75 KB | pookmish |
| #9 | 2018-12-07_08h39_50.png | 14.94 KB | maxilein |
| #9 | 2018-12-07_08h38_26.png | 4.76 KB | maxilein |
| #4 | same-view-different-arguments-2892073-4.patch | 793 bytes | pminf |
Comments
Comment #2
pminfI stumbled upon this issue while using multiple paragraphs with the same viewfield in a single node. Each paragraph embeds the same view but passes different argument values. Unfortunately all paragraphs embedded views list the rows of the first paragraph if cache is enabled.
I think there needs to be a cache context according to the contextual filter of the view or the value of the viewfield. But I don't know how.
And there might be some more work to do ;)
Any cache experts out there?!
Comment #3
pminfComment #4
pminfI get different lists of nodes by adding the arguments as cache keys.
Comment #5
codesquatch commentedThanks for the patch, it worked for me.
Comment #6
codesquatch commented@pminf Thanks for the patch, it worked for me.
Comment #7
codesquatch commentedComment #8
andypostLooks the view should not use cache to leverage entity render cache
Comment #9
maxilein commentedwhat if content is empty?
The default display setting (on content type) for a viewsfield is to not render output: "Always build output:no"
If a view does not have any content there is an error in the line of your patch:
$cache_keys = array_merge($elements[$delta]['#content']['#cache']['keys'], $arguments);
If you set the the output to always output, then the warnings and notice go away.
Maybe use a different field for your keys?
Any suggestions are welcome!
Comment #10
maxilein commentedSolution to your code:
if the delta is null, we don't have anything to cache. Add this line:
I have added a patch to my proposed new dev release here: https://www.drupal.org/project/viewfield/issues/3014568#comment-12885275
Comment #11
pookmish commentedAttached an adjusted patch to remove the line that disables field caches.
Comment #12
jerdavisTested and verified patch from #11
Comment #14
jerdavisComment #15
maxilein commentedjerdavis: thank you for taking over!