backdrop-contrib / references

References module for Backdrop CMS
GNU General Public License v2.0
2 stars 6 forks source link

Views caching causes validation error #12

Open kswan opened 2 years ago

kswan commented 2 years ago

Steps to recreate:

In a site with a few users create a user reference view that outputs some users. Set Caching to Time-based, 1 hour, 1 hour. Create a content type with a user reference field. On the user reference field set "View used to select the users" to the view created above. Add a node of the new content type and set the user reference field to "user a". Save the new node. Promptly edit the new node and change the user reference to "user b". You will see a validation error "User: this user can't be referenced."

Workaround

If we change the Caching to None, the validation error is gone and the field works as expected.

Factors

The function references_potential_references_view() sets $view->is_cacheable = FALSE, but this apparently isn't effective.

jromine commented 2 years ago

So if I'm understanding this right, you:

  1. create a view that returns a list of some users
  2. set the view to cache its results, time-based, 1hour
  3. create a content type with a user reference field
  4. configure the user reference field to use the user view as its allowed value list
  5. create a node of the new content type with the user reference field value "user a"
  6. edit the node, and attempt to change the user reference field value to "user b".
  7. validation fails

Is "user b" in the result set from the view?

kswan commented 2 years ago

Yes. Both user a and user b are in the view. It also happens if you create a second node that references user b.

If you check the $results variable in references_potential_references_view() in references.module, you'll see that when validating step 6, the only result is the uid for user a.

By the way, I assume the caching issue would be the same for node references since the function building the view is the same, but I didn't test it.

jromine commented 2 years ago

The user reference field lists the users correctly, but the validation fails on node save.

Verified: image

jromine commented 2 years ago

In the call to references_potential_references_view($entity_type, $view_name, $display_name, $args, $options), to render the select list, the $options is: image and when validating the selection after clicking Save $options is: image $options['ids'] = array('7') // 7 is the uid of the user I selected in the node edit form

In the first views query, the result set is an array of user uid's from the view, as expected. In the second views query, the result set is an array with only one element, the uid of the existing value of the references field (not 7, another uid). So that explains why the validation is failing, but not why the views query result is wrong.

kswan commented 2 years ago

If you flush the cache between the first (step 5) and second (step 6) validation, the result with one element will be the correct uid.

The validation also fails with different nodes that go through the validation within the cache window.

Also if you set caching in the view to none, the single element will be correct.

jromine commented 2 years ago

I am pretty confident this is a bug in views. The Drupal bug report is https://www.drupal.org/project/drupal/issues/1055616

Looking at the patch, it is updating get_results_key() and get_output_key(), and adding a new function get_cache_key(). I substituted the D7 versions of these functions in "views_plugin_cache.inc" and added get_cache_key() and the bug in this issue was fixed.

jromine commented 2 years ago

The Drupal patch is https://www.drupal.org/files/issues/views-1055616-152.patch (but note I only edited views_plugin_cache.inc as noted above).

jromine commented 2 years ago

Here is the Drupal commit: https://git.drupalcode.org/project/views/-/commit/0cb96e5269a5479498b6ca9f51762c68974923c8

jromine commented 2 years ago

@kswan you can file a bug report against Backdrop core views module at https://github.com/backdrop/backdrop-issues/issues/new/choose or if you prefer, I can do so for you. I don't think I am able to transfer this issue to the Backdrop core issue queue.

jromine commented 2 years ago

is_cacheable was removed from views. More info: https://www.drupal.org/project/views/issues/635336