Views will not display the value of a newly added field to user accounts even when the field has value.

Created on 14 May 2024, 7 months ago

When adding new fields to user accounts via /admin/config/people/accounts/fields and then editing the user account and setting a value for the new fields so they can be pulled into a view block does not display in the view unless the user account has at least 1 published or unpublished node content. Very...strange!

For about 3 days I went round and round thinking I had some kind of permission problem, removing contrib modules that handle extra permissions or events like ECA, Content Access, ACL and eliminating any other possibilities I could think of that could be causing this issue. After reading through probably 50 different posts on similar events occuring.

I couldn't understand why some accounts would display the new view blocks (being displayed on user/* account pages with the new fields data) showing the complete view block with the correct data and other accounts would only show partial data and not the new fields data (even when those fields have value) especially since I have the fields in the view set to display even when empty including display rewrite if empty. I used the rewrite output to display the field but with some added html, ID's and classes - so even if the field had no data it should still display but that didn't even happen. Thus why I thought I had a permission problem somewhere!

By the time I was done with all that, basically stripped all permissions between fields and the view and view blocks themselves "and the same thing was/is happening" (getting overly frustrated at this point I started digging further into it.) The entire time no errors, warning or issues reported anywhere, not in dblog or onscreen or browser console, nothing...

Roles, permissions, etc., all set the same between all accounts (this should not be happening). The only difference I could find between any accounts was the fact that some accounts have published and unpublished node content while others did not. The ones that in the past had/have at least one published or unpublished node (did not matter what the content type was/is) their account view blocks were working as expected displaying the new field data from the user account (which to note, those fields are in NO WAY attached to any content type). Now on the other side any account that has never published a node and does not have any unpublished nodes as well are the accounts not displaying the view blocks with the new fields data.

OK, found a difference I can test...

Using the masquerade module I switched to a user account without any published node type content and add a new node of any content type the user account can access. Once completed I go back to the users account page and "waahhhh'laaa" the view block now showed properly. I switched accounts back to administrator account and then to make sure it still works, I check the users account page and all is good and then I unpublish that users node content to make sure it still shows and it does for both admin and user accounts.

Now before just posting this here, the field I initially added to the user accounts was actually a paragraph with 2 number fields so to rule out the paragraph module as causing this issue I created a new "test number field" again at /admin/config/people/accounts/fields then duplicated my view block removed the original field and added the new one and placed the block on the same user account pages and got the same exact issue in my results.

I'll include some sample images showing the issue before and after the user has a published node. I also tested to see if a user without a published or unpublished node could trigger the view to work properly by another published entity like comments which did not work!

That said I'm not entirely sure why not having a published or unpublished node content type that isn't related to the user account fields display through a view. It just doesn't make sense.

You'll see on the example images the before image looking at /user/4 (I have the field themselves displayed just as the fields as per user display of fields and then the view blocks in the right sidebar which should also have the data and does not. When all is said and done the fields themselves won't be displayed there just the view blocks with the fields data will because of what and how they are being used all around.

It's also worth mentioning using "preview" in views using user id's to preview with contextual filters also produces the same results. If user has a published or unpublished node everything displays properly. If the user does not have a published or unpublished node the view does not display properly.

Regarding the view there is no default filter criteria or sort criteria for the view and it uses relationships "User" and Contextual filter "(User) User: User ID" -> set to Provide default value: Type "User ID from route context".

For good measure:
Drupal: 10.2.6
PHP: 8.1.13
Apache: 2.4.38 (Debian)
Database: MySQL 5.7.40

πŸ› Bug report
Status

Active

Version

10.2 ✨

Component
ViewsΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States sidgrafix

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sidgrafix
  • The best way forward is to add validated steps to reproduce starting with a clean Drupal install.

  • πŸ‡ΊπŸ‡ΈUnited States sidgrafix

    OK "Steps to Reproduce" step by step (just did this with a fresh install of Drupal 10.2.6, PHP 8.1.16, Apache 2.4.38 Debian)

    Step 1:
    Go to /admin/config/people/accounts/fields
    - Now add at least one field (I added 2 Number (integer) fields to be specific) with the "+ Create a new field" button.
    For each field I set configuration as follows:
    - Field Storage: Allowed number of values (Limited : 1)
    - Set the Minimum value that should be allowed to: -1 (negative 1)
    - Check on "Set default value"
    - Set the default value to: 0
    - Save

    Step 2:
    Go to /admin/structure/views
    - Now add a view with the " + Add view" button.
    Create the view with this configuration.
    - Leave "View settings" default (Show: Content of type: All)
    - Under Block settings, Check "Create a Block"
    - Set the "Block display settings" to (Display format: Unformatted list of: fields)
    - Set the "Items per block" to 1
    - Click on Save and edit button

    Step 2B:
    Configure the view block and add the user account fields we previously created:
    - Under the "Advanced" section (right hand view column) expand it open if it is not.
    - Across from Relationships click on the "Add" button to add a relationship to the view.
    - In the Add relationship dialog find under Title "User" where Category is "Content" and the Description reads "The username of the content author"
    - Next click on "Add and configure relationships" button at the bottom of the dialog
    - In the Configure Relationship: User dialog, Don't make any changes and just click the "Apply" button at the bottom of the dialog.
    - Next also under Advanced across from "Contextual filters" click add
    - In the Add contextual filters dialog find under Title "User ID" where Category is "User" and the description reads "The user ID"
    - Next click on "Add and configure contextual filters" button at the bottom of the dialog.
    - In the Configure contextual filter: User: User ID dialog Where it says "When the filter value is NOT available" check the "Provide default value"
    - In the "Type" dropdown that appears select: "User ID from route context"
    - Leave everything else as is and click the "Apply" button at the bottom of the dialog.

    Step 2C:
    - Now on the left column of the view display across from "Fields" click on the Add button.
    - In the "Add fields" dialog find the field or fields you created in step 1.
    - The "Category" should be "User" with a description that reads "Appears in: user." when you have it/them selected click on "Add and configure fields" button at the bottom of the dialog.
    - In the "Configure field: User: Name of the Field" you can just hit the "Apply" button at the bottom of the dialog.
    - When all field configuration is done and the dialog is closed click on the "Title" field listed under the Fields section above the field or fields you just added, when the dialog opens click the "Remove" button at the bottom of the dialog.
    - Below fields (also in the views first column) where it says "Filter criteria" click on "Content: Published (= Yes) and when the dialog opens click on the "Remove" button at the bottom of the dialog to remove it.
    - Next below that where it says "Sort criteria" click on "Content: Authored on (desc)" and when that dialog opens do the same as you did for the filter and click on the "Remove" button at the bottom of the dialog.
    - Next under the views configuration and above the views preview area hit the "Save" button to save the view.

    Step 3:
    Create a new user as an authenticated user, no admin privileges and give value to the new account fields:
    Go to: /admin/people
    - Click the "+ Add user" bottom near the top of the page.
    - Provide the "Username" and add a "Password"
    - Under Roles "Authenticated user" should be checked by default and grayed out.
    - Scroll to the bottom of the add user form and locate your added field or fields and give them values. (I set the first field of my two to "2" and the second number field to "4")
    - Then at the bottom of the form hit the "Save" button.

    Step 4
    Place the view block on user account pages:
    Go to: /admin/structure/block
    - Scroll down to either a "Content" region or a "Sidebar" region (I used the right sidebar)
    - When your at the region you want to use click on the "Place block" button to the right of the region name you are using.
    - In the "Place block" dialog find the view block you created by name (using the search field and typing in part of its name should bring it right to you, its category will read "Lists (Views)" and under "Operations" click on the "Place block" button that is displayed to the right of the view block name.
    - In the Configure block dialog under "Visibility" click on the "Pages" tab.
    - In the Pages input field put /user/* and make sure "Show for the listed pages" is checked
    - Depending on what you named the view and if you added a block name the "Machine-readable name" may be to long and you will have to shorten it to save the block.
    - Finally click on the "Save block" button at the bottom of the dialog.

    Step 5:
    Now visit the users page:
    Go to: /admin/people
    - In the user list under "Username" find the user you just created and click on the name to go to their user account page (as admin doesn't matter if users can see each others account pages)
    - You should be at: /user/2 (if there is only you the admin and 1 user account the path would be /user/2 otherwise it will be /user/[user-id].

    What you will see is unexpected:
    - Under the user account details you will see the two fields with the values you set
    - However...The view block will only have the title of the view block and there will NOT be any field data and if you inspect the html the view content is empty. I would expect to see the same information displayed by the fields and clearly is not.

    I'll include a screen cap of the new user account page at this point (fresh-install-same-issue.jpg).

    Now if you want to give your new test user the ability to add an article or basic page you will also need to grant the "Authenticated User" role to allow it - if you do and you login as the new test user and create an article or basic page and then view their user account page admin or the user you will see the block then has the field information.
    -> If you need instructions on how to do that let me know!

    Couple of things to note (I did straight basic setup for the steps to reproduce) but there are reasons why I did a few things that you wouldn't get from this setup and may explain why I chose the view type I did for our use case.
    1. I used a view to display content not user because for my use case I need content worked into this display seamlessly using attachments that also relate to the user and there content (which they may not have any especially when they are a new account)
    2. The actual fields created on user accounts can only be set or updated by an admin or the corresponding module being developed to work with those fields on behalf of the user. The user has permission to view their field info (thus why it would be able to display in a view block), but they cannot change it. Furthermore the default fields are even hidden for our use case. As we just need it displayed in a block that does a lot more than just display the field info/data.

    Any questions or problems let me know.

  • Thanks for that. That information will be most useful if it is in the issue summary rather than in a comment. This template β†’ is normally used in that way.

  • πŸ‡ΊπŸ‡ΈUnited States sidgrafix
  • πŸ‡ΊπŸ‡ΈUnited States sidgrafix

    Just to add to the issue:

    I just found that deleting a users node if they only had one - also produces the same issue and the account fields are not rendered in the view block. Guess it's time for a workaround...

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Fixes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies. Also, 10.2 is in security mode now.

  • πŸ‡­πŸ‡ΊHungary atomi

    In the Drupal 10 version, it doesn't save the value written in the admin field, I had to try for a long time, then I tried it with a normal user's field and it was already saved there. Isn't that the case with you? So it's not the Views, but the field that's the problem for root admin.
    So if we create a new field in the profile then we have this error. I tried this with ECA first, and it took me a long time before I thought to see if it was even possible to write directly into the field. There are gross errors in version 10 compared to version 7. This is not the only error.

  • πŸ‡ΊπŸ‡ΈUnited States sidgrafix

    @atomi, No this isn't just the case of admin vs user field. Writing to the fields/updating field values. is not the issue. It is the fact that even with value, unless a user either has a published or unpublished node of any type the view blocks will not display the fields values. The only work around I could come up with was to create a dummy content type as the user set to unpublished programmatically on new user registration so the blocks would display with their default value when they or an admin view the user account page. I used event dispatching and event subscribers to handle the heavy lifting.

Production build 0.71.5 2024