Problem/Motivation

If user pictures are enabled but no user picture is set an empty <article> wrapper prints in node template. Additionally even when there is a user picture the article is untitled.

Proposed resolution

  1. Prevent empty markup printing.
  2. Remove the article wrapper entirely (however we do that, this should happen).
  3. Consider a method for getting the user/author picture directly without any wrapper markup, this should be a renderable array.

Remaining tasks

Discuss and patch.

User interface changes

None.

API changes

None.

Comments

Jeff Burnz’s picture

Jeff Burnz’s picture

Issue summary: View changes
joelpittet’s picture

I saw something that would be awesome everywhere. But how about this?

{{ node.author|view_mode('compact') }}

As long as we had view_mode's for all entities like entity_view(), we can take author and a new filter called view_mode(), with the name of the view mode you'd like to render that entity in.

Thoughts?

mortendk’s picture

may i suggest a couple of things:

theme suggestion for user.html.twig
that we put in a theme suggestion as well so we can use user--compact.html.twig
that would make i it logicl

2 i would love if we could grap the image directly without the chance off all kinds of other things coming in (like an editor playing with compact)
something like {{ author.image }} that only prints out the image would make a ton of more sense than the user.html.twig with article tags n everything that we get out now.

Jeff Burnz’s picture

#3 I like that idea a lot, nice to put that at the template level and not buried in preprocess, so I really like it.

Kind of agree with the view mode suggestions, I would probably go that route if we did {{ node.author|view_mode('compact') }}, to strip the article wrapper rather than doing that in preprocess as per the OP.

I like the idea of getting just the image out also, I haven't dug very deep but is there a method for doing that sort of thing, cause all that stuff in #items is protected afaict and seriously #items is so dam complicated, theming has become a real chore in D8, ya know?

andypost’s picture

At the same time user picture is a image field now.
So maybe better use a field template override?

Jeff Burnz’s picture

Yes, I am currently using field template in my theme to strip away field markup, so good call, but can we or should we do that for core?

I thought about this yesterday and can see that user_view compact is an interesting idea, as in how this is meant to work pulling the user and a specific view mode - however this actually feels more like you want to add a bio to the node, that you want more than just the author picture - such as Bio field, link to website etc - in other words this is good for adding a complete author bio/byline but for simple user pictures to comment or node is this overkill?

Should we be requiring overriding of two templates to get a clean img element (potential user--view-mode--compact.html.twig) and the field template?

Can we write a method/function that special cases getting the author picture, so we can use that to get it any time we want? Is that uncool in D8 or acceptable?

Just thinking out loud here.

mortendk’s picture

my 2 cents
i think its epic overkill to include the whole view with whatever fields etc - what i think we should do is grapping the image and sticking that into the {{ author_picture }} that would make sense for the node & thats whaat we need

.. then it would be awesome with a {{ author_picture|imagesize('small') }} or something.

andypost’s picture

agree about overkill especially for comments that could be hundreds on page

mortendk’s picture

@jeffbunz yeah i dont think it was ever ment to be pulling a bio etc - thats what we have our beloved blocks for right ?
so in my head im thinking something like this:
the author_image is well basically just the file path to that image ?

some pseudocode like this:

node.module

preprocess ...
$author_image = the_image_from_the_user

node.html.twig

{{ autor_image}}

then it still keeps using the picture magic etc right ?

tstoeckler’s picture

So the reason for why this view mode was introduced in #1292470: Convert user pictures to Image Field was that the user picture is just a regular configurable field that is created by the Standard install profile. And we want to avoid having core themes depend on configuration from installation profiles. The current mechanism allows for other profiles to install a different image field (i.e. with a different name, or a gravatar field, or whatever...) but still use e.g. Bartik and not have to override the node template. That's why pulling the field value directly from the user is not going to fly, I think.

Of course we should definitely avoid printing empty markup, but, yeah...

Jeff Burnz’s picture

Issue summary: View changes

Remove some example code and clarify intentions/discussion points.

Jeff Burnz’s picture

OK, so re #11 that might leave us with quite limited options here - the view mode suggestion for one is viable and gives us ability to strip markup (article wrapper), and if you need to go further (strip field markup) then themes can do this with a field template? Better ideas more than welcome.

lewisnyman’s picture

Issue tags: +frontend

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

megan_m’s picture

I have made the mistake (more than once) of thinking it would be clever to use the "Compact" view mode as an author bio or teaser type view mode, only to realize that it would cause problems elsewhere. It would be nice if that view mode could be renamed "Picture", so it's more clear that this should just be used for the picture field.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dqd’s picture

Version: 8.4.x-dev » 8.5.x-dev
Issue tags: +DrupalWTF

Taken from node.html.twig comment block line 23.

author_picture: The node author user entity, rendered using the "compact" view mode.

This can be missed quickly (and is confusing if not), so we maybe should rename the view_mode or the author_picture (and user_picture) variable? Actually all 3 namings are not really fitting for their purpose here. Plus: I don't see the reasons for hard coded <article> HTML element wrapper here eather. Even DS full reset and Fences is not able to remove it. If we need to keep this view mode and the twig variables in here (even if for legacy reasons), we should make it possible to rewrite the markup. Therefore at least a field or view mode theme suggestion would be helpful at minimum. Just first thoughts at a glimpse. If this is too much (like stated already in case of 1000 threaded comments), we possibly need a better way for viewing/referencing the author picture on a node and a user picture on comments then.

Beware that we have the default theme setting "show user picture in posts" and "show user picture in comments" under admin appearance (see screenshot uploaded), which can be turned on/off. Both settings activate/fill two different theme variables: {{ user_picture }} and {{ author_picture}}. And both are using the user compact view mode. The user does not await a complete compact user view mode if turned on one or the other, but the user image field only here. Especailly since modules like Flag automatically add their fields to this view mode (tested). 2 possible solutions:

  1. Change the setting description to: "show compact user view mode in posts and comments" (and maybe change {{ author_picture }} to {{ author_compact }} and {{ user_picture }} to {{ user_compact }} which will become difficult because of all the respective objects and functions [look at Gist occurence below])
  2. Change the value of {{ author_picture }} and {{ user_picture }} to print picture field only, not a complete view mode.

In all cases the <article> HTML element with quick-edit attributes make no sense here at all, since this will most commonly appear in content lists or node full view mode, where the main article HTML element comes in the mix additionally. So point 2 on Proposed solution in the issue summary should be agreed on.

NOTE: I change the focused Drupal core dev version for this issue, since we are on 8.4,2 already ...

dqd’s picture

Issue tags: +d8ux
dqd’s picture

Issue summary: View changes

Let's catch some spots to know of (Gist link since the code is unreadable here): Occurence of author_picture, user_picture and respective settings in Drupal 8.5.x dev

As we can see (this can be also seen as a reply to #8/#10), there are A LOT spots (especially for user_picture), and renaming user_picture (also in functions etc) can become a real mess to keep naming convention in sync. And since the naming convention of user and author sticks together here it is a decision for both. So maybe changing the rendered content in user_picture and author_picture away from a view mode back to a simple field value injection is maybe still the easiest approach for this issue. Even if view modes is a the new way to go. And since this can break themes we should consider an additional twig variable {{ user_compact }} or something similar to reflect the existing view mode.

BTW: there is a way in contrib to revert to the behaviour we have (at that moment here) in core (before we maybe change it next time). It can be used to rebuild the user compact view mode for certain purposes: https://drupal.stackexchange.com/a/207719

dqd’s picture

StatusFileSize
new65.5 KB
berdir’s picture

dqd’s picture

@Berdir: Thanks for the useful reference. Yeah this one seems to be a good spot and definitely related. But this link seems to be restricted (I get a 403). But the title looks promising :) (Any chance to add me to the group to access it for getting things done here?)

berdir’s picture

I don't know why that issue is unpublished, I guess I can only see it because I'm the owner. There is no reason for it mentioned. Try asking someone from the security team.

dqd’s picture

Issue tags: -dreammarkup, -DrupalWTF +D8Media

Another even "harder" suggestion to solve this whole issue (to raise the MVC and modularity flag again) is:

Finally remove the "show user/author picture" kind of settings thing from appearance and the default user picture field completely. TBH, I do not really know what the use of it is no more when we are able to add fields everywhere nowadays and can render them like we want on module or theme level anytime. Simply turning on this appearance setting mostly looks akward from scretch in any theme anyway and needs tweaking anyway. So why not doing it the Drupal fields way here too? Most users do it already this way and leave the settings off. The whole "show user picture" concept feels quite dated and forces to use this default field, IMHO. This would also affect the issue mentioned by @Berdir and would possibly fix this one too by one strike.

Removing the default user_picture field simply greys-out the appearance settings (screenshot follows) without any issues. I think the suggestion would not break any other contrib or core theme/module.

dqd’s picture

StatusFileSize
new63.59 KB
pranali.addweb’s picture

I can suggest a few things, they might help to improve the solution itself.

Issue: User picture improvements in node template

Solution: I am mentioning few steps from start:

-->We should start from Global theme setting and pretend unwanted checks. Like if we dont want the user picture on user commnets we can prevent it from there by theme global setting.
-->There are two ways to get profile picture:
.One by the suggestions which are mentioned on the top of the twig file. We can directly access from there. i.e: {{ content.user_picture }}
-->The another way to access the profile picture by fetching field in node. If the user picture is a specific field then we should use : i.e: {{ node.uid.entity.field_name.value }}
-->For styling {{ author_picture|imagesize('small') }} this is a sample example which is working fine for me.
-->For styling by taking variable I am mentioning below sample code which by which we can apply css on user picture or auther picture div:

 {% set imagestyle = {
      '#theme':      'image_style',
      '#style_name': 'user_avatar_80x80',
      '#uri':        node.uid.entity.user_picture.entity.uri.value,
      '#attributes': { class: 'authorImg' },
    } %}
    <div class="authorCol1">{{ imagestyle }}</div>
ivan berezhnov’s picture

Issue tags: +CSKyiv18

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

quietone’s picture

Status: Active » Closed (outdated)
Issue tags: +Bug Smash Initiative

This doesn't happen on 11.x with Olivero.