
Drupal 7 - When View Rows Don't Display for Anonymous Users
Published: 10/18/2016
I created a view that looked great - from my perspective, as an Administrator with full access to my Drupal site.
However, when I asked an anonymous user to have a look, some of the view results were missing. Furthermore, a sub-admin role (with more restrictive access than I have) also could not see some of the view results.
This immediately "felt like" a permissions issue, but the permissions on the view were set at View Published Content, which the Anonymous role had authority to do. The thing that really bugged me was how/why some results (rows) were visible, while others were missing.
I started looking more closely at the view's relationship. My view used an Entity Reference relationship to pull data from a referenced content type. However, the relationship did not always exist (or was not true). I found that when the relationship existed, the view results displayed for everyone. When the relationship did not exist, the results were hidden from everyone but me(?).
If you're wondering why anyone might create such a view, I'll provide a little more detail.
My view pulled every published instance of a content type called 'Home Page Info Blocks,' which are as their name suggests: blocks of information for the Home Page. Some blocks needed to pull information from a referenced Location content type. In this view, I needed to pull a patient waiting number from our Urgent Care locations. But, this only accounted for some of the Home Page Info Blocks. Other blocks just needed to display static content or a promotional ad or whatever was deemed necessary to be on the Home Page. The portion of Home Page Info Blocks that contained an Entity Reference to a Location were the rows that displayed for everyone. The Home Page Info Blocks without the relationship were the missing rows for my anonymous and other users.
But I could see them! I could see them on the view's page and in the Auto-preview of the View editor. My view was legitimately returning all results, regardless of whether the relationship existed. And so it seemed (again) like a permissions problem.
Googling the description of this issue was challenging, because it was hard to describe what was happening. But somehow I located this gem of an article: Adding a Relationship to Your View? Your Results Might be Different than you Expect (opens in new tab)
I won't recreate the article's content here; the author did a great job of describing the problem and potential solutions. But in summary, this is a permissions "bug", which has been patched in Drupal 8 (opens in new tab). The quickest way to work around this issue in D7 is to disable SQL rewriting, a view setting that prevents node access criteria from being added to the view's query.
I'm sure there are alternatives. I probably could have rewritten the view and/or modified my content types, but disabling SQL rewriting took 2 seconds. And it worked exactly as expected.
