Currently, we are adding an anchor before comments for the fragment identifier anchor point (e.g., "node/41#comment-42").

<a id="comment-42"></a>
<div class="comment">
  <div>comment contents</div>
</div>

I would like to see the id placed on the actual comment container for a couple of reasons.

<div id="comment-42" class="comment">
  <div>comment contents</div>
</div>

1. It is more symantically accurate. Having the id on an empty anchor doesn't "identify" anything.
2. It allows increased styling options. For example, there is a proposed CSS3 pseudo class ":target" which selects the fragment identified by the fragment identifier in the document URI.

http://www.w3.org/TR/css3-selectors/#target-pseudo

Comments

Jaza’s picture

Version: x.y.z » 6.x-dev
Status: Needs review » Needs work

This looks good. +1 as long as anchors pointing to DIVs (rather than other A's) works in all major browsers - and I believe it does. Moving to 6.x-dev queue.

catch’s picture

Status: Needs work » Active

setting back to active now everything is .tpl.php-ified, not sure if this is still valid at all.

dpearcefl’s picture

Is this issue still needed in current D6?

anybody’s picture

Version: 6.x-dev » 7.x-dev
Issue summary: View changes

+1 here, the same problem is existing for Drupal 7, which should be more interesting. I hope the problem will NOT exist in Drupal 8 (or I will cry ;))

The problem is that there is absolutely NO CHANCE to alter the output because all the HTML gets bashed into the #prefix value together with the intendation, 'new' info, etc.!

This is really important and the current solution is a bit crappy :)

You can find the code in comment.module, line 955:

    // Add anchor for each comment.
    $prefix .= "<a id=\"comment-$comment->cid\"></a>\n";
    $build['#prefix'] = $prefix;

I'd suggest to add this kind of prefixes to a tpl file to make it better editable.

Thanks a lot.

dcam’s picture

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

This is still the case in D8.

gargsuchi’s picture

StatusFileSize
new1.88 KB

Here is a patch which fixes it for D8. Against 8.0.1

jonathanshaw’s picture

StatusFileSize
new1.88 KB

Changed extension to .patch so that testbot will pick it up.

jonathanshaw’s picture

Status: Active » Needs review

The last submitted patch, comment_fragment1.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 7: comment-fragment-patch.patch, failed testing.

gargsuchi’s picture

StatusFileSize
new1.87 KB

Reuploading after applying a fix.

gargsuchi’s picture

Status: Needs work » Needs review

Removed

Status: Needs review » Needs work

The last submitted patch, 11: comment-fragment-patch.patch, failed testing.

gargsuchi’s picture

StatusFileSize
new2.53 KB

Trying a git patch upload.

jonathanshaw’s picture

Status: Needs work » Needs review

Status must be set to Needs review to trigger testbot

Status: Needs review » Needs work

The last submitted patch, 14: comment-fragment.patch, failed testing.

jonathanshaw’s picture

Multiple test fails. May not be a problem with the patch, maybe more that these tests use a method for detecting a comment exists which is disrupted by this patch, hence tests need adjusting.

gargsuchi’s picture

Bump! Can someone RTBC this?

jonathanshaw’s picture

The test fails need to be addressed before the patch can be committed.

vprocessor’s picture

Assigned: Unassigned » vprocessor

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

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now 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.

vprocessor’s picture

StatusFileSize
new5.5 KB

Please, don't check it, because only diff there

vprocessor’s picture

Assigned: vprocessor » Unassigned
Status: Needs work » Needs review
vprocessor’s picture

Assigned: Unassigned » vprocessor
Status: Needs review » Needs work
vprocessor’s picture

vprocessor’s picture

Hi all, there is interdiff & wip patch: todo some core templates and fixed tests by this log https://www.drupal.org/pift-ci-job/132672

vprocessor’s picture

Assigned: vprocessor » Unassigned
Status: Needs work » Needs review

The last submitted patch, 22: comment-fragment-49707-22.patch, failed testing.

andypost’s picture

+++ b/core/themes/classy/templates/content/comment.html.twig
@@ -76,7 +76,7 @@
-<article{{ attributes.addClass(classes) }}>
+<article{{ attributes.addClass(classes) }} id="comment-{{ comment_id }}">

+++ b/core/themes/stable/templates/content/comment.html.twig
@@ -65,7 +65,7 @@
-<article{{ attributes.addClass('js-comment') }}>
+<article{{ attributes.addClass('js-comment') }} id="comment-{{ comment_id }}">

interesting...
we can't change templates here
but can add "ID" via preprocess

vprocessor’s picture

Assigned: Unassigned » vprocessor
andypost’s picture

StatusFileSize
new3.97 KB
new5.42 KB

suppose that should be like that

vprocessor’s picture

Assigned: vprocessor » Unassigned

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

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now 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.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now 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.

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

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.

duaelfr’s picture

Status: Needs review » Needs work
Issue tags: +Accessibility, +Needs reroll, +Novice

Thanks for working on this issue!

According to WCAG 2.0 rule 2.4.4 Link Purpose (In Context), we should not have links without any text or title. This change seem to me really important to clean markup and improve accessibility.

A lot of tests have moved during the last 2 years so this patch needs to be rerolled. Given the patch is easy, I'm tagging this issue as Novice to help newcomers to find something to start with.

ilya.no’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new6.32 KB
new6.13 KB

Hi! Attaching reroll and interdiff.

Status: Needs review » Needs work

The last submitted patch, 38: 49707-comment-fragment-38.patch, failed testing. View results

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.

duaelfr’s picture

Status: Needs work » Needs review
StatusFileSize
new63.25 KB

Simple reroll after CommentPagerTest has been converted to BrowserTestBase in #2809467: Convert \Drupal\comment\Tests\CommentPagerTest to BrowserTestBase.

Status: Needs review » Needs work

The last submitted patch, 41: 49707-comment-fragment-41.patch, failed testing. View results

andypost’s picture

@DuaelFr your patch somehow affects composer files, please remove this hunks

duaelfr’s picture

Status: Needs work » Needs review
StatusFileSize
new6.43 KB

Sometimes I just want to slap myself ;)
Thank you for your awareness

jonathanshaw’s picture

Status: Needs review » Reviewed & tested by the community

This looks good.

The last submitted patch, 38: 49707-comment-fragment-38.patch, failed testing. View results

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record

This change should have a change record as the change shows tests are often affected and we're changing markup - which is allowed but documenting it is good.

jonathanshaw’s picture

Status: Needs work » Reviewed & tested by the community
lauriii’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/comment/comment.module
@@ -724,6 +724,8 @@ function template_preprocess_comment(&$variables) {
+  $variables['attributes']['id'] = 'comment-' . $comment->id();

We should ensure that the id is unique by running this through Drupal\Component\Utility\Html::getUniqueId.

alexpott’s picture

@lauriii I'm not sure about that - we should these in anchor links elsewhere on the page - I think it is up to the user not to output the comment twice on the same page.

alexpott’s picture

@lauriii Also as that is more of a change - ie. we don't currently run it through that method - how about a follow-up to discuss all the ramifications of that?

lauriii’s picture

Issue tags: +Needs followup

Sure, I'm fine with that 👍

alexpott’s picture

Status: Needs work » Reviewed & tested by the community
alexpott’s picture

idebr’s picture

@lauriii in #49 has a points this might introduce invalid markup. Alternatively, we could use the name attribute instead of the id attribute for identical behavior but without the duplicate id attribute potential.

duaelfr’s picture

The name attribute is only allowed on form elements so that's not suitable for this usage.
We need these ids to be accurate because it's needed to build anchored links to the comments.
As the first call to Drupal\Component\Utility\Html::getUniqueId returns an unmodified id, it'd be fine to me to use it to prevent potential multiple comments outputs in the same page to break the ids uniqueness while preserving linkability.

jonathanshaw’s picture

As I understand it, the discussion in #55 and #56 would be better on #3020394: Investigate using Html::getUniqueId() on comment-ID identifiers, as the present patch does not worsen this problem and therefore will not address it.

  • lauriii committed 69b99ec on 8.7.x
    Issue #49707 by vprocessor, gargsuchi, DuaelFr, andypost, ilya.no,...
lauriii’s picture

Status: Reviewed & tested by the community » Fixed

Looks good! Moving the id to the parent element is a small risk because someone could have used it for targeting CSS or JavaScript but since it isn't generally good practice to use ids for doing that, I'm fine with making this change. Committed 69b99ec and pushed to 8.7.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.