To implement RDFa, we need to have a way to add attributes to some of the markup elements throughout drupal (nodes, blocks, comments, fields). The challenge is to do this in a way that themers will be happy with. I discussed this with several of the RDF folks and several themers, and here's what I learned:
- There was universal agreement among the themers I talked with, that they do not want extra markup tags inserted, especially if it's out of their control, since that will potentially mess with their CSS rules. So, attempts to set #prefix and #suffix to have divs/spans around the needed objects to hold the rdf attributes were met with resistance.
- We want to minimize the introduction of extra tpl files. For example, creation of wrapper tpl files (similar to comment-wrapper.tpl.php) are not desired. Also, pulling elements out of a tpl file and giving them their own tpl file (for example, having a special tpl file for just the node title) is not desired.
With that in mind, my proposal is to expose $attributes and $title_attributes variables to all tpl files. And to ship all tpl files with these used. This is similar to how $classes is available to all tpl files. This means when themers override these tpl files, they'll need to make sure $attributes and $title_attributes are printed on the appropriate tag, or else the site will lose RDF information. However, the themers I talked with preferred the trade-off of having this extra responsibility but retaining full control over markup.
Patch coming momentarily.
| Comment | File | Size | Author |
|---|---|---|---|
| #62 | 569362.patch | 13.9 KB | pol |
| #58 | 569362.patch | 11.57 KB | pol |
| #53 | drupal-doc-theme-attributes-d7-569362-53.patch | 12.19 KB | pol |
| #39 | drupal-doc-theme-attributes-d8-569362-39.patch | 6.95 KB | David Hernández |
| #33 | drupal-doc-theme-attributes-d7-569362-33.patch | 14.68 KB | David Hernández |
Comments
Comment #1
effulgentsia commentedHere's the patch. Some notes:
Comment #2
catchNo way we can just replace $classes with $attributes rather than adding it?
Comment #3
effulgentsia commentedHere's a tester module to aid review.
@catch: i think themers like the ability to add their own classes by writing
And I don't think we want to universally force themers away from having that ability to add classes within the tpl file. However, I do know that some themers will do this:
Which I think is ok for medium to advanced themers to do if that's the workflow they like, and why I don't want the template_process() function unsetting $variables['attributes_array']['class'].
Thoughts?
Comment #4
catchThat's a good enough argument for me, and in general this patch looks great, so no objections. I don't have time to do an in-depth review tonight though, so leaving to others to rtbc.
Comment #6
effulgentsia commentedJust wanted to summarize where we are with this, because I'm gonna ask some themers to chime in on this issue:
Comment #7
todd nienkerk commentedAlex: Awesome! Thanks for tackling this, and thanks for seeking our input during the sprint. I'm very happy with the implementation we discussed.
This is a matter of trade-offs. The only other solution -- wrapping this information in
spans or other markup -- is completely undesirable, so our next-best option is to add$attributevariables. This, however, isn't really a big deal. If themers are working on sites that require RDF/RDFa data, they need to know how to properly support it. RDF/RDFa data is, after all, a matter of markup.The type of themers who would want to combine class, ID, and other attribute data into a single variable are savvy enough to know how to do so in preprocess/process functions. By adding elements' class and ID attributes separately, we provide an easy method for non-savvy themers to add these values directly to markup. This is essential to retaining a good mix of savvy and non-savvy theme accessibility.
Comment #8
moshe weitzman commentedI reviewed this at the sprint and +1 it. Since Todd is happy as well, lets proceed. This approach is consistent with our granular themeing in D7.
Comment #9
dries commentedI reviewed this patch and it looks good to me. I agree with the careful analysis in this issue -- it is the best solution. I'll leave this RTBC for a bit longer in case more people want to chime in.
Comment #10
Everett Zufelt commentedI would like to know more about the changes that this will make to the output markup for a page. Is there an example site that I can visit where this patch is implemented? What attributes are being added to elements here to store the RDF data?
Comment #11
scor commented@Everett Zufelt: with the current patch, a node markup would look like this (note the new attributes @about, @typeof and @property):
Comment #12
scor commentedThe patch in #1 is functional. I would like to suggest an extra measure to ensure we don't allow invalid HTML to be generated. Currently if one writes the following in a preprocess function:
it will display 2 class attributes in the same tag. The patch attached moves these classes in the ['classes_array'] which are inserted in the tpl file separately (reproducing the same behavior as pwolanin's block module patch).
Comment #13
effulgentsia commented@scor: please see my comment #3 for why I don't like the process() function unsetting $variables['attributes_array']['class']. I think it's better to document that module authors should never add to $variables['attributes_array']['class'], but that theme authors can if they want to, and if they do so, then they need to adjust their tpl file to match. We simply have some themers preferring one approach and some preferring another, and I see no reason to force it one way or the other. If you agree, please re-post the original patch. If you disagree, please say so, and let's see if we can get buy-in.
Comment #14
scor commentedI see your reasoning, which is to let the themers ensure they produce valid HTML. It also saves us some extra processing in the theme layer. setting back to RTBC with the initial patch.
Comment #15
dries commentedCommitted to CVS HEAD. Thanks!
Comment #16
effulgentsia commentedAwesome! Here's some documentation in the affected template files. I don't pretend to be a good documentation writer, so whoever is, please feel free to update the patch as you see fit.
Comment #17
effulgentsia commentedRelated issue to handle a couple spots this one didn't cover: #607456: RDF still needs one more mechanism to get attributes on a themer accessible element.
Comment #18
effulgentsia commentedI added documentation about this and $content_attributes (#608036: Add content_attributes variable for tpl files, so RDF can be implemented better.) to http://drupal.org/update/theme/6/7.
Setting this to "needs work", because an updated version of patch #16 (to include $content_attributes) is still needed. However, please see sun's comment from an unrelated issue: http://drupal.org/node/610204#comment-2184522. Are we really set on comments within each template file about how to use preprocess functions to affect these variables? Any way of having less redundancy to maintain while still giving people useful information where they need it most?
Comment #19
effulgentsia commentedchanging tags to reflect current state of issue
Comment #20
effulgentsia commentedMeh. I still question the wisdom of so much duplication. But we do it for $classes/$classes_array. So here it is for these variables.
Comment #21
Jaza commentedAll these new 'attributes' template variables are being used solely for RDFa. So let's make it clear in the docs that this is all they ARE being used for, and that this is all they SHOULD be used for. I don't see why we should suggest that users overload these variables with non-RDFa XHTML attributes (eg class and id attrs).
Updated patch, which is the same as that in #20, but which:
Personally, I'd also advocate renaming all the variable names to
$[foo_]rdfa_attributes[_array], but I'll just leave that as a suggestion for now.Comment #22
effulgentsia commentedAlthough RDFa is all that they are being used for in core, the intent is for this to be a generic facility for any module to add to. I don't know of a use-case that's been identified for modules other than rdf needing to add attributes (I sure hope modules only use this for semantic needs, and don't abuse it by adding thing like "onclick" or "style"), but over the next year or two, I expect some contrib modules to come up with something.
In the design of this, some advanced theme developers requested the ability to merge in id and class into it via preprocess functions, because that fits their development workflow better (#7 alludes to this a little bit). But that's for advanced theme developers, so I'm okay with not documenting it in the template files if that makes it cleaner for the people that this documentation is aimed at.
Comment #23
scor commented#608036-12: Add content_attributes variable for tpl files, so RDF can be implemented better. introduced
$attributesin user-profile.tpl.php and I think some other user*.tpl.php files are missing from that patch. I would suggest to grep HEAD to make sure all tpl containing these variables have been covered.Comment #24
effulgentsia commented#20 added the documentation to every template file that was already documenting $classes_array. I would question any template file that use $attributes without also using $classes (IMO, $classes should be added to user-profile.tpl.php if we've added $attributes to it).
But this leaves the larger problem that we actually have LOTS of template files, and they ALL potentially can use $classes and $attributes. I really don't like the idea of duplicating so much documentation in every file or picking and choosing which template files have it documented and which don't. I would much prefer an online handbook page that documents all the common variables, and have each template file have a line that says something like: "see http://drupal.org/whatever for information about variables available to all template files".
Comment #25
Jaza commented@effulgentsia:
Re: use of
$attributesfor other non-RDFa attributes (e.g. class and id). Comment #7 (which you pointed to) doesn't give a particularly strong argument - I'd summarise it as: "advanced themers _might_ prefer to inject data into$attributesthrough a preprocess function; and non-advanced themers almost certainly _won't_, they'll prefer instead to put other attributes directly in their templates." Templates should be accessible to people with HTML/CSS skills but not PHP skills. Being able to output class and id attributes directly in your template is designer-friendlier than having to write a preprocess function to do the same thing. IMHO, encouraging$attributesto contain _only_ RDFa data is more accessible to such people (and those are the people we want as Drupal themers, right?). So, I do ask once again that you accept my amended patch.Re: not documenting every variable of every template in detailed comment docs. Yes, it's a lot of overhead to maintain such docs - more than just putting a "see this handbook page for more info" line. But it's also extremely useful and convenient for themers. Once again, we want to make templating as easy as possible for non-coders. Is it so much to ask, that when a non-coder (i.e. someone whose PHP skills don't go far beyond
print'ing) opens up a.tpl.phpfile, that he/she can quickly see every variable that's available for printing in that template? I vote for maintaining the (growing) amount of duplication in template comment code, as the price of a superior themer experience. (Anyway, this is off-topic, so I'll stop now).Comment #26
effulgentsia commented@Jaza: Yeah, I agree with you about not mentioning that id/class can be added, and that #20 is wrong in the use of the phrase "XHTML attributes". And I see your point about the usefulness of the duplication across template files, and at any rate, we do it, and this isn't the issue to change it: since we do it for $classes, we should do it the same way for $attributes and friends.
My only concern with #21 is that I don't like the information saying it's RDF only. Even though none of the core modules do so, I see no reason why it would be wrong for a contrib module to add legitimate XHTML attributes like 'title', 'dir', 'lang', 'accesskey', or 'tabindex'. And is RDFa the only possible semantic addition to XHTML? Are there other namespaces that can be added to the document for extending it with semantic information, and if there are, I'm sure there will be contrib modules using it. Can you think of a way to re-phrase #21 to not rule out these possibilities (while also making it clear that id and class are special and not in this list)?
Comment #27
retester2010 commented#21: drupal-doc-theme-attributes-569362-21.patch queued for re-testing.
Comment #29
Everett Zufelt commentedThere are many things that these variables can be used for, including RDF, WAI-ARIA, adding a class to the title of a a block generated in code (w/o a template).
Comment #30
Anonymous (not verified) commentedThere are still lots of issues with people not understanding these variables. I'm going to switch this to documentation, which it seems to be at this point.
Comment #31
David Hernández commentedWorking on this. This issue needs backport to D7, as the $content_attributes is not documented there neither (See https://www.drupal.org/node/2332087 )
Comment #32
David Hernández commentedUpdating this to D8 first
Comment #33
David Hernández commentedAs seems that there is no consensus in the discussion about saying RDFa or other thing, I just done a quick reroll for D7 and created a patch with the content that was missing for Drupal 8.
Comment #34
David Hernández commentedChanging status for review and unassigning
Comment #38
jhodgdonThanks for reviving this issue!
There are a few problems with this patch, I think:
a) block.html.twig
Ummm... I don't think that all of the attributes on a block are RDFa attributes, are they, by far?
b) block.html.twig
Is this about it being a string etc. and the leading space really accurate? It seems like it's an array or object when it comes in, based on how the template uses it:
c) block.html.twig
These should not have $ on them. There are no $ on these variables in Twig.
d) content_attributes is not actually printed in the template block.html.twig that I can see?
e) comment.html.twig
There is no documentation in this template for attributes, just attributes.class, so saying "Same as attributes" doesn't really help?
f) All of these templates have copies of the documentation in the Bartik overrides of these templates, so the patch needs to fix those too. I personally think that it would be better if Bartik would not copy documentation when it overrides a template because of the need to maintain the docs in two places, but ... that's a separate discussion.
Comment #39
David Hernández commentedOk, I think I fixed all the comments from the review on #38. Here is a new patch. I also updated all the affected bartik templates. Once that patch is RTBC, I will update the D7 patch, if It's necessary.
Comment #40
jhodgdonThanks, this version looks good to me!
Comment #43
jhodgdonBack to RTBC. I would just commit it but there were many people participating; would like to give them one more chance to set back to needs work if necessary.
Comment #44
webchickIt's been a day, but not sure what sort of time you wanted. Assigning to jhodgdon. :)
Comment #46
jhodgdonCommitted to 8.0.x. Thanks! Moving to 7.x for backport of documentation, as much as is applicable anyway.
Comment #47
effulgentsia commentedWas this applied everywhere it was intended to?
content_attributes: List of classes for the styling of the comment content.content_attributes: HTML attributes for the content., which is different from the "Same as ..." pattern that the patch introduced.Comment #48
jhodgdonThe patch in #39 only has block, comment, and node templates in it. No field.html.twig. The patch did not change the content_attributes section of the comment templates.
So... the patch as supplied was committed... do we need a new patch?
Comment #49
jhodgdonThis was assigned to me for committing...
Comment #53
polHere's the patch for D7.
Comment #56
Delphine Lepers commentedComment #57
stefan.r commentedPatch looks fine, thanks! To be fixed on commit:
missing newline
Unrelated changes :)
Comment #58
polHi!
Here's the updated patch.
Comment #59
stefan.r commentedComment #60
David_Rothstein commentedThis variable is a string, not an array.
Also the patch seems to be missing some of the changes that were there before, including the one that caused #2332087: undocumented $content_attributes in node.tpl.php to be marked as a duplicate of this issue. The earlier Drupal 7 patch from David Hernández in #33 looks like it had more of those changes. (I don't think it's the end of the world if we commit something that isn't 100% comprehensive, but we should at least try to keep all the templates somewhat consistent.)
Comment #61
polGoing to work on that, patch coming in the following hours.
Comment #62
polHere's the updated patch.