As reported by #402254: How do we do on XHTML validation?, there are some strange duplicate IDs during node preview:

GET http://dev.local/Drupal/Core7/node/add/page returned 200 (7.21 KB).
POST http://dev.local/Drupal/Core7/node/add/page returned 200 (8.2 KB).
DOMDocument::loadHTML(): ID node- already defined in Entity, line: 92
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Issue tags: +XHTML validation

Adding tag. :)

JohnAlbin’s picture

Right now, Drupal 7's node.tpl has:

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes ?> clearfix">

We could add a $id variable and change it to:
<div <?php if ($id) { print 'id="' . $id . '"'; } ?> class="<?php print $classes; ?> clearfix">

And then ensure that $id is unique in the background.

Alternatively, we could move node-NID from an ID to a class, so it would just be added to $classes.

So, which of those 2 solutions do we want to go with?

rcross’s picture

I think there may be times where a node may be displayed more than once on a page. For example, when you're displaying both the teaser and the full node on the preview page. as john cross posted at #405270: Strange duplicate IDs during node preview and when a node is displayed multiple times on a page

So, i think it makes the most since to move NID to a class. I think it may also be good practice to make the change to node.tpl as well.

drupal_was_my_past’s picture

Version: 7.x-dev » 8.x-dev
Issue tags: +Needs backport to D7

I think there may be times where a node may be displayed more than once on a page.

I agree with @rcross in #4 that there are times when a node may be displayed multiple times on a page and therefore having duplicate CSS ids is invalid code. I think the cleanest thing to do would be to remove the CSS id and move the node nid into a CSS class. Though backporting that may be difficult. I can't think of a way to do it that wouldn't break a lot of folks' existing code.

David_Rothstein’s picture

Title: Strange duplicate IDs during node preview » Strange duplicate IDs during node preview and when a node is displayed multiple times on a page
Fabianx’s picture

Important from that duplicate issue is:

jwilson3:

The problem with this is that the ID is the only valid use of URL fragment navigation in HTML5, because anchor tags with the name attribute is now invalid in HTML5. This is a pattern used by Drupal core no less for navigating to comments on a page, so it might be argued that this should not be removed from nodes either.

I think to solve it we could:

* Remove the #id attribute for teaser display
* Add a class attribute with node-[nid] always

jwilson3’s picture

The argument is that potentially the same node would appear on the page more than once, but its just an assumption that they would be different view modes Examples given were the preview (where full node and teaser are show). There are other cases where potentially two view modes might be shown (eg, sideshow and node list).

Instead of code that targets just the teaser display to remove the id, might we instead add the view-mode to the id, for all except the default view mode? This would more widely cover the use case of a node listing appearing more than once on a page, but I still don't think it actually gets at the heart of the issue.

IMO, the only proper solution is to track a registry of nodes on the page and build a method that generates a unique id, similar to how form-api works when you add the search form to the page twice, for example.

Whatever ends up happening, I would also propose moving the logic for the id, to a preprocess function, and to introduce the Attribute object here.

I partially agree with #5, in that anything short of a registry of node ids would have a much higher chance of breaking peoples existing designs for a backport to D7.

David_Rothstein’s picture

Status: Active » Needs review
FileSize
1.65 KB

Presumably just something like this, no?

There's probably no way to backport this in preprocess, though, since for other themes we'd start seeing two "id" attributes on the same node. (I think to backport it at all, the only possibility to consider would be fixing it directly in the template.)

jwilson3’s picture

Nice! This solution is very clean -- putting the id into the Attributes array in preprocess and removing it completely from the template, and letting Drupal do its job -- no themer should be needing to change the id attribute in a template anyway.

Fabianx’s picture

#9: That is a nice approach :)

jwilson3’s picture

Status: Needs review » Reviewed & tested by the community

feel free to overrule me, if someone finds something wrong with #9, but it seems like a clear and easy win to me.

jwilson3’s picture

The only potential thing we are *losing* in this patch is code beautification: the id now comes after a potentially long list of classes, harder to find/read/reference by humans. I don't think this constitutes further work on this solution. If the themer deems this important enough, they can override/implement their own node.tlp.php (or Twig as the case may be) to print out the id before the class, using the syntax provided in the template for class as a guide. Thoughts?

Fabianx’s picture

Easy to fix:

Just do:

<article id="<? print $attributes['id']; ?>" class="<? print $attributes['class']; ?>"<? print $attributes: ?>>

Then its no problem anymore ...

David_Rothstein’s picture

Probably we could rearrange things in the preprocess function so that 'id' is the first thing added to the 'attributes' array?

Then, if the class="..." stuff that's currently hardcoded in the template ever gets moved to preprocess in another issue (which it probably should?), the ID would be first in HTML again.

David_Rothstein’s picture

Oh.. or #14 works too :)

Fabianx’s picture

Status: Reviewed & tested by the community » Needs work

#15: No, themers need to be able to add classes easily, such the class="{{ attributes.class }} clearfix" will definitely need to stay. (writing twig as thats faster to type)

I think making the id explicit is better, because if a themer wants to change this, they can. (not that they should, but its crystal clear what is happening).

Would love a re-roll of #9 with #14 included.

Tentatively setting to NW to get that change in, then can be back to RTBC :-).

jwilson3’s picture

Sorry, beforehand. Yes, this is a total side note, but interestingly enough, I've been theming Drupal for a while now, but I've never needed to add a class to an object where classes were already present, but only to objects where Drupal doesn't provide any class at all.... *ahem* forms. Drupal is really good at providing all the necessary classes out of the box that any web developer / themer worth his grain of salt could figure out a proper css directive chain to target the item needed without additional classes.

I personally side with #15, that the class="{{ attribute.class }}" stuff could go away, and be documented once or twice in various places around the code on how to add classes in template files. It almost seems *cleaner* to show people how they could add a class in a preprocess function instead of overriding a template file just to add a classname.

In summary, I'm not trying to derail here -- so if this input is useful, maybe we should just open up a followup issue about this topic.

UPDATE; The unwritten assumption I'm making is that if you know enough to figure out how to override a template file in a custom theme, you also know enough to read documentation on how to override and extend the attributes classes.

David_Rothstein’s picture

Status: Needs work » Needs review
FileSize
1.84 KB

I can see both sides of that argument myself, but given that "id" does appear in the template files currently, I suppose the simplest change this issue can make is just to leave it there.

So, here's a patch along the lines of @Fabianx's suggestion in #14.

Fabianx’s picture

Status: Needs review » Reviewed & tested by the community

#18: @jwilson3: This is not true for most non programmer themers, but I'll just point you to the sandbox issue, where this has been debated already:

#19: Thanks a lot @David_Rothstein. Back to RTBC now.

podarok’s picture

#19 looks good for me
+1 RTBC

David_Rothstein’s picture

diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php
old mode 100755
new mode 100644

Oops, this wasn't supposed to be here. (I have an installation script that changes file permissions on things in sites/default and it accidentally "fixed" this and slipped into the patch.)

There's actually an issue to fix that at #1830330: Make default.settings.php not executable already, so to avoid confusion I'm uploading a new patch file that removes those changes from this one.

Leaving at RTBC since there are no changes to the rest.

jwilson3’s picture

#18 @Fabianx, you missed the link... It would be much appreciated tho. ;) Also, personal shoutout, thank you, and congratulations on your hard work getting Twig in. I'll be celebrating this evening. nice forward progress.

Fabianx’s picture

#23: Thanks a lot! Here is the link: http://drupal.org/node/1808254

#22: Still RTBC - would love to see this in :-).

catch’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +XHTML validation, +Needs backport to D7

The last submitted patch, node-unique-id-405270-22.patch, failed testing.

Fabianx’s picture

Needs re-roll ... and should also change the twig template ...

Rob230’s picture

Will this be backported to D7?

If not, would it cause any problems if I replace

print $node->nid;

with

print drupal_html_id($node->nid);

in node.tpl.php of my theme.

It's a simpler fix but seemed to work fine when I tested it.

xjm’s picture

Component: node.module » node system
Issue summary: View changes

(Merging "node system" and "node.module" components for 8.x; disregard.)

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.

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.

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should 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.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should 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.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.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: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should 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.

pameeela’s picture

Status: Needs work » Closed (duplicate)
Issue tags: +Bug Smash Initiative
Related issues: +#2004252: node.html.twig template

The id attribute was removed from the node template in #2004252: node.html.twig template