Problem/Motivation
While a user's profiles can be added to User entity, there is no way to specify the view mode.
Steps to reproduce:
- Create a view mode for profiles by hand:
/admin/structure/display-modes/view/add/profile - Create a profile entity type on
/admin/people/profiles - Configure profile fields on
/admin/config/people/profiles/manage/[profile_machine_name]/fields. - Configure the view mode display of those fields on
/admin/config/people/profiles/manage/[profile_machine_name]/display. - Add the Profile type's pseudo-reference field to the User entity on
/admin/config/people/accounts/displayand attempt to configure it to render the appropriate profile fields by selecting the view mode to use.
Expected behavior:
User should be able to specify the view mode used to embed the profile on User account.
What actually happened:
There are no options to select the view mode.
Proposed resolution
The profile field on the User entity field management form is missing the settings/widget configurator, because the Profile field is not a real field but a pseudo-field, (see https://www.webomelette.com/creating-pseudo-fields-drupal-8).
These fields do not provide any widget display because Field API cannot figure out which data type the field represents and all the rendering is made programmatically via #2629956: Profile type rendering on user view should be done as extra fields. However, the community reported other limitations:
- Lack of config of field in form display: #2750059: Move the profile "registration" form to a field widget -- which is a sister-ticket for this issue to get profile settings working on the "Manage form display" tab of User entities.
- Lack of config of integration with entity query: #2654980: Storing the active profile(s) in a field.
Matt Glaman implemented one approach using computed fields to solve issue #2654980-39: Storing the active profile(s) in a field regarding the transparent recognition of these fields within entity queries on comment #39 that actually solves this issue.
There were also other relevant approaches on Issue #2654980 which were tested by @citlacom however only Matt Glaman's patch from #39 solves this issue (even though it does not solve the original problem from that issue).
We have confirmed that applying the patch from comment #39 of #2654980-39: Storing the active profile(s) in a field allows us to configure the rendered entity widget with the Profile's Teaser view mode, however it caused a regression in user migrations. So we've adapted the patch from that ticket, and moved it here to add a fix for the migration regression and added testing.
Remaining tasks
Write patch
Validate & test patch
User interface changes
Users will be able to modify the Profile pseudo-field widget settings on the User entity's "Manage Display" tab.
API changes
N/A
Data model changes
N/A,
Original issue description by jwilson3
Thanks for this module Matt!
This is the one entity type in my site that supports Fields API, Field Groups, and the new Drupal 8 field layout, but doesn't support display modes. /admin/structure/display-modes/view lists possible view modes for all my entities except for Profiles, and the /admin/config/people/profiles/manage/{profile_type}/display page is missing the "Custom display settings" collapsed fieldset.
Similarly, if I have a profile reference field configured on a node, the only view mode that comes up in dropdown of the field's display settings for "Rendered entity" is "Default".
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | 2946670-45.patch | 9.63 KB | mglaman |
| #39 | interdiff-2946670-39-32.txt | 2.63 KB | mglaman |
| #39 | 2946670-39.patch | 9.63 KB | mglaman |
| #36 | 2946670.png | 93.58 KB | martijn de wit |
| #32 | interdiff.2946670.12-32.txt | 1.73 KB | super_romeo |
Comments
Comment #2
jwilson3Comment #3
jwilson3Comment #4
jwilson3This was a huge brain fart on my part. I missed the fact that
A) profiles don't have any default view modes by design, and then
B) I didn't even think to check under the "+ Add view mode" button on the page, to see if I could actually create one from scratch.
Sure enough. Profiles can have view modes. Face, meet palm.
Closing this as works as designed with tail between legs.
Comment #5
jwilson3Embedding a profile into any other entity, like a Node, via an entity reference field, works fine, as you're given the standard dropdown to select the display type and then if you choose "rendered entity" to select a view mode for the profile entity in the field display ui.
However, I'm reopening this issue and refining the title to describe the problem more accurately, because it this view mode stuff is in fact still a problem when embedding the Profile onto the User entity/page. You're not given any view modes to select from.
Comment #6
jwilson3@citlacom found the root cause of the issue here configuring the widget and view mode for Profile field on the User entity.
The problem is that the profile field that is missing the settings/widget config was not a real field but a pseudo-field, (see https://www.webomelette.com/creating-pseudo-fields-drupal-8).
These fields do not provide any widget display because Field API cannot figure out which data type the field represents and all the rendering is made programmatically via #2629956: Profile type rendering on user view should be done as extra fields. However, the community reported other limitations:
Matt Glaman implemented an alternative approach using computed fields, (see https://www.drupal.org/docs/8/api/entity-api/dynamicvirtual-field-values...) on comment #39 of #2654980-39: Storing the active profile(s) in a field.
There were also other approaches on Issue #2654980 that were relevant to the problem here. @citlacom tested these and found that Matt Glaman's patch solves this issue even though it does not solve the original problem from that issue, regarding the transparent recognition of this fields within the entity queries.
We have confirmed that applying the patch from comment #39 of #2654980-39: Storing the active profile(s) in a field allows us to configure the rendered entity widget with the Profile's Teaser view mode:
I'm open to suggestions about how to move this issue forward. Obviously, we'd love to see that patch get included, but don't have enough insight onto whether that would cause regressions in other places, or if it would be save to move that patch to this issue so it could get included anyway.
@mglaman, what do you think?
Comment #7
citlacom commentedUsing the original patch from Matt https://www.drupal.org/files/issues/storing_the_active-2654980-39_0.patch we have experimented problems when importing users via migrations, users that are processed for creation via migration trigger the following call at ProfileEntityFieldItemList.php:
$profile_storage->loadMultipleByUser($user, $this->getSetting('profile_type'), TRUE)As the user is not yet saved the $user do not have an assigned uid so when the load query is executed the following condition uid is empty:
This causes the exception: Query condition 'profile.uid IN ()' cannot be empty. (core/lib/Drupal/Core/Database/Query/Condition.php).
In order to solve the problem the loadMultipleByUser should execute only when the current user is not anonymous, the fix is added to the attached patch.
Comment #8
citlacom commentedNeeded to re-roll the patch to apply cleanly on 8.x-1.x and generated the interdiff from 2654980-39 to 2946670-7 that is the fix for the error: Query condition 'profile.uid IN ()' cannot be empty. (core/lib/Drupal/Core/Database/Query/Condition.php).
Comment #9
citlacom commentedChanged approach to check if user profiles should be loaded for an user, needed to use id() instead of isAnonymous() entity method due was still experimenting the query condition error on profile module functional tests. This new patch should resolve those problems.
Comment #10
citlacom commentedRestored the views.view.profiles.yml view config file that was initially removed in Matt's patch but that was required to properly display profiles list when user have more than one profile at userProfileForm:
Additionally I extended some tests to include configuration of the profile field on user entity to show in the default display mode. Attached an interdiff of the changes compared with Matt's initial patch.
Comment #11
citlacom commentedCorrect interdiff from last patch.
Comment #12
citlacom commentedAdded a testProfileFieldOnUserDisplayConfig() to check that profile field configuration at User entity display mode allow to customize the render configuration. Updated the interdiff with the last version.
Comment #13
citlacom commentedAll tests are passed so now need help from other developers to review and confirm that all is working as expected and hopefully @mglaman can review and commit the final patch. :)
Comment #15
zenimagine commentedThe rendered entity of the profile works, but in
/admin/structure/display-modes/view thereare no profile display modesComment #16
jwilson3@zen imagine you have to create the display/view modes for Profile entity types manually on
/admin/structure/display-modes/view/add/profile. I'm not entirely sure if it makes sense for us to create one out-of-the-box or not, but that could be a followup issue to this one.I've updated the issue summary to clarify this.
Comment #17
zenimagine commented@jwilson3
I know how to create a view mode.
The problem is that the profile view modes that it's created, are not listed on
/admin/structure/display-modes/viewI have another problem. If I add the code
{{ content.profile_personnel }}to the user's twig, the profile is not displayed.Comment #18
zenimagine commentedThe profile is displayed if I do not create a twig.
If I create a twig of the user display, the profile does not work.
Can you confirm ?
Comment #19
zenimagine commentedWith this configuration, there is no identifier in the url :
Comment #20
jwilson3@zenimagine: This isn't really the right place for this, but please debug your Twig.
In my case, with field layout module enabled, I get this variable:
{{ content._field_layout.content.staff_profile_profiles }}our profile type is called "staff_profile". Given that your profile appears to be named personnel, I could wager a guess that the profile should be available at either:
{{ content.personnel_profiles }}or
{{ content._field_layout.content.personnel_profiles }}You can use this snippet of code to figure out what sub-components are available for you to use from there:
Good luck.
If you're still having trouble, please open a separate support request on a separate issue and link it here so we can keep this thread limited to the original issue at hand.
Comment #21
jwilson3@zenimagine:
I'm not seeing this issue:
Comment #22
zenimagine commented@jwilson3 I cleaned the cache and I do not see anything :-(
Comment #23
jwilson3Comment #24
jwilson3Comment #25
jwilson3Comment #26
zenimagine commented@jwilson3
#20 It works, but impossible to create a link to the profile (see screenshot).
Before applying the patch, the {{ content.profile_personnel }} code worked
Comment #27
jwilson3Comment #28
jwilson3Creating a link to the profile is not the primary purpose of this issue. This issue is to embed the profile into the User page.
Comment #29
drupgirl commentedThis is working as expected. Thank you all for your work.
Comment #30
yasmeensalah commentedI needed the patch on the rc1 version so i rerolled it to work with it.
Comment #31
geek-merlinI'd say once the broader #2750059: Move the profile "registration" form to a field widget is fixed, anyone can leverage core's form display configuration to move and disable any profile or profile field.
Comment #32
super_romeo commented1. I think it is different issue. #2750059: Profile fields to appear on manage form display for account settings is about manage form (!) display and this issue is about manage display.
2. I've fix the patch.
While creating user with:
I have warning:
Change record: ComputedItemListTrait has been added to standardize the behavior of computed fields
Comment #33
martijn de witUsed patch and field widget display settings appearing as expected.
Tested it with several different view modes as rendered entity.
git: dev commit "716e81dd0328bab469bf1574351c0de373d1ae9a [716e81d]"
+1 RTbC
Comment #34
martijn de witAlso can be applied to "5400cebed4afe4271095f003d754e0361aba5822 [5400ceb]"
Comment #35
mglaman@Martijn de Wit thanks for the screenshots! Helped test this before commit. I will be reviewing.
Comment #36
martijn de witDon't know where my screenshot did go.. uploaded it again...
Comment #37
mglamanFinally sitting down so we can commit this :)
Comment #38
mglamanAnonymous user's do not have a uid of null, it is 0.
Fixing some phpcs. Will post patch with an interdiff.
Comment #39
mglamanHere is an updated patch to address #38. The problem I see is this: what about already configured extra display fields? Do we need some kind of upgrade path or just a change record so users know to reconfigure their profile displays on users?
The extra field used to be keyed
['profile_' . $bundle->id()]and now it is[$profile_type->id() . '_profiles']Comment #40
mglamanClient work and DrupalCon derailed my effort to finish this up. Since no one has replied, I am going to give it a quick test. I will probably post another patch that puts the field keys back to the way they originally were.
Comment #41
mglamanOkay, I experimented with my thoughts in #40. When reusing the same keys, the following error shows up for already configured profile extra fields:
Notice: Undefined index: type in Drupal\field_ui\Form\EntityDisplayFormBase->buildFieldRow() (line 367 of core/modules/field_ui/src/Form/EntityDisplayFormBase.php).I'm going to open a change record that states end users embedding profiles on user pages will need a quick reconfigure.
Here's a new patch which removes an unused function that hacked in the View title, and the view display that used to be used.
Comment #42
mglamanCR: https://www.drupal.org/node/3048287
Comment #44
mglamanBah, this was still needed for ProfileController::userProfileForm
Comment #45
mglamanOkay, #41 just was a bad idea. Here is #39 again. If green, merge ahoy!
Comment #47
mglaman🎉Thanks everyone, committed.
Comment #48
bojanz commentedBetter title :)
Published the change record, but we'll want to flesh it out a bit too.
Comment #49
andyg5000Just had the need for this when building a Search API index. Thanks y'all #partytimeexcellent