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.

Comments

effulgentsia’s picture

Status: Active » Needs review
StatusFileSize
new9.23 KB

Here's the patch. Some notes:

  • This patch makes no attempt to unset 'class' or 'id' from the attributes, as is done on a precursor to this patch : http://drupal.org/node/493030#comment-1995662. This is because some themers would prefer the ability to add class and id, and adjust their tpl file accordingly. So, instead, we leave it to module authors to behave and not add class and id to the attributes_array variable.
  • This patch also includes field.module exposing a $item_attributes variable, which is an array keyed on $delta. This is because for some fields, the rdf attributes need to be different on an item by item basis.
catch’s picture

No way we can just replace $classes with $attributes rather than adding it?

effulgentsia’s picture

StatusFileSize
new1.28 KB

Here's a tester module to aid review.

@catch: i think themers like the ability to add their own classes by writing

<div class="extra-class <?php print $classes; ?>"<?php print $attributes; ?>>
...
</div>

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:

// In template.php
function THEMENAME_preprocess(&$variables) {
  $variables['attributes_array']['class'] = $variables['classes_array'];
}

// In *.tpl.php
<div<?php print $attributes; ?>>

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?

catch’s picture

Issue tags: +RDF, +RDFa

That'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.

effulgentsia’s picture

Just wanted to summarize where we are with this, because I'm gonna ask some themers to chime in on this issue:

  • We want D7 to have RDF support, which is why this issue is marked critical; we don't want to ship D7 without RDF support.
  • This is not the only RDF issue. Other RDF issues are tagged with RDF or RDFa, so if you're interested, look through those.
  • The Drupal-RDF team decided that the best way to add RDF support to Drupal is using RDFa, meaning to add attributes to XHTML markup tags (span, div, etc.). Simply adding semantic tags (e.g, "<rdf:description>") would break XHTML validation, and I'm not qualified to speak on what the issues are with other RDF techniques. But if the RDF developers decided RDFa is the way to go, that's good enough for me.
  • If we're gonna go through all the trouble of figuring out how to let modules apply rdf attributes to desired xhtml elements in a way that still gives themers the control they need, we might as well generalize this to all attributes with respect to the theme layer. This keeps the theme layer from hard-coding anything specific to RDF, allows the RDF module to manage what it needs within the appropriate attributes variables, and also provides a new tool for other modules to apply other attributes.
  • With the above in mind, the issue boils down to how do we want to standardize on modules managing attributes that need to be applied to particular xhtml elements?
  • The biggest request I heard from themers is for modules to NOT add markup elements in a way that's not exposed to theme functions and template files. In other words, modules creating div or span elements whose only purpose is to hold attributes is the least desired solution as far as themers are concerned.
  • I also heard from several folks that introducing new template files (like node-wrapper.tpl.php or node-title.tpl.php) should be avoided if possible. There seems to be a general pattern that themers' workflow is such that doing more in a single template file is easier than managing many template files. I'm not a themer, so I have no opinion about this, and am just relaying what I heard. If anyone disagrees with this, please chime in.
  • The above considerations are what led to the patch in comment #1. I believe webchick's biggest concern with this patch will be whether some themers will be unhappy with the added complexity of the new variables: $attributes, $title_attributes, and $item_attributes (on field.tpl.php only). So, themers: please add your thoughts about this.
  • Themers: please also comment on what I said in comment #3: that you don't want 'id' and 'class' in the attributes variable by default, so that you can deal with those in your tpl.php file independently, but that for those of you that want to merge them in, you want the flexibility to do so in your template.php's preprocess function(s).
  • Just to be clear, I'm not particularly committed to any of these decisions. I'm just trying to express what I believe to be majority opinion. Please comment both with agreements and disagreements.
todd nienkerk’s picture

Status: Reviewed & tested by the community » Needs review

Alex: Awesome! Thanks for tackling this, and thanks for seeking our input during the sprint. I'm very happy with the implementation we discussed.

I believe webchick's biggest concern with this patch will be whether some themers will be unhappy with the added complexity of the new variables: $attributes, $title_attributes, and $item_attributes (on field.tpl.php only). So, themers: please add your thoughts about this.

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 $attribute variables. 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.

Themers: please also comment on what I said in comment #3: that you don't want 'id' and 'class' in the attributes variable by default, so that you can deal with those in your tpl.php file independently, but that for those of you that want to merge them in, you want the flexibility to do so in your template.php's preprocess function(s).

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.

moshe weitzman’s picture

Status: Needs review » Reviewed & tested by the community

I 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.

dries’s picture

I 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.

Everett Zufelt’s picture

I 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?

scor’s picture

Status: Needs review » Reviewed & tested by the community

@Everett Zufelt: with the current patch, a node markup would look like this (note the new attributes @about, @typeof and @property):

    <div id="node-1" class="node node-article node-promoted node-teaser clearfix" about="node/1" typeof="sioc:Item foaf:Document">
      <h2 property="dc:title"><a href="node/1">title of the post</a></h2>
      <div class="meta">
        <span class="submitted">
          Submitted by <a href="user/1" title="View user profile." class="username">admin</a> on Thu, 09/10/2009 - 07:56
        </span>
      </div>
      <div class="content">
        <div class="field field-name-body field-type-text-with-summary field-label-hidden clearfix">
          <div class="field-items">
            <div class="field-item even" property="content:encoded">
              <p>Content of the post...</p>
            </div>
          </div>
        </div>
      </div>
    </div>
scor’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new9.58 KB

The 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:

  $variables['attributes_array']['class'][] = 'customclass';

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).

effulgentsia’s picture

@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.

scor’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new9.23 KB

I 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.

dries’s picture

Status: Reviewed & tested by the community » Fixed

Committed to CVS HEAD. Thanks!

effulgentsia’s picture

Priority: Critical » Normal
Status: Fixed » Needs review
Issue tags: +Documentation
StatusFileSize
new9.94 KB

Awesome! 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.

effulgentsia’s picture

Related 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.

effulgentsia’s picture

Title: Add attributes and title_attributes variables for tpl files so that RDFa can be implemented » Document $attributes, $title_attributes, and $content_attributes template variables
Status: Needs review » Needs work

I 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?

effulgentsia’s picture

changing tags to reflect current state of issue

effulgentsia’s picture

Status: Needs work » Needs review
StatusFileSize
new18.22 KB

Meh. I still question the wisdom of so much duplication. But we do it for $classes/$classes_array. So here it is for these variables.

Jaza’s picture

StatusFileSize
new15.91 KB

All 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:

  1. replaces all wording "String of XHTML attributes" to "String of RDFa attributes" (this is still technically just as accurate, only more specific, since RDFa is a representation of RDF data in valid XHTML form)
  2. removes the lines "By default, this variable does not include the id and class attributes, so that the template can separately control how these are printed. At its discretion, the theme may use a preprocess function to merge the id and/or class into this variable, in which case the template should not also print them separately." We don't need this text if we make it clear that only RDFa attributes can/should be in this variable.

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.

effulgentsia’s picture

Although 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.

scor’s picture

#608036-12: Add content_attributes variable for tpl files, so RDF can be implemented better. introduced $attributes in 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.

effulgentsia’s picture

#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".

Jaza’s picture

@effulgentsia:

Re: use of $attributes for 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 $attributes through 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 $attributes to 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.php file, 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).

effulgentsia’s picture

@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)?

retester2010’s picture

Status: Needs review » Needs work
Issue tags: +Needs documentation

The last submitted patch, drupal-doc-theme-attributes-569362-21.patch, failed testing.

Everett Zufelt’s picture

There 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).

Anonymous’s picture

Component: theme system » documentation

There 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.

David Hernández’s picture

Assigned: Unassigned » David Hernández
Issue tags: +Needs backport to D7, +Amsterdam2014

Working on this. This issue needs backport to D7, as the $content_attributes is not documented there neither (See https://www.drupal.org/node/2332087 )

David Hernández’s picture

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

Updating this to D8 first

David Hernández’s picture

StatusFileSize
new3.02 KB
new14.68 KB

As 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.

David Hernández’s picture

Assigned: David Hernández » Unassigned
Status: Needs work » Needs review

Changing status for review and unassigning

Status: Needs review » Needs work

The last submitted patch, 33: drupal-doc-theme-attributes-d7-569362-33.patch, failed testing.

The last submitted patch, 33: drupal-doc-theme-attributes-d8-569362-33.patch, failed testing.

jhodgdon’s picture

Thanks for reviving this issue!

There are a few problems with this patch, I think:

a) block.html.twig

- * - attributes: HTML attributes for the containing element.
+ * - attributes: String of RDFa attributes populated by modules, intended to

Ummm... I don't think that all of the attributes on a block are RDFa attributes, are they, by far?

b) block.html.twig

+ * - attributes: String of RDFa attributes populated by modules, intended to
+ *   be added to the main container tag of this template. This is either an
+ *   empty string or a string containing a leading space, so it should be
+ *   printed without a leading space before what precedes it.

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:

<div{{ attributes.addClass(classes) }}>

c) block.html.twig

+ * - $title_attributes: Same as $attributes, except applied to the main title
+ *   tag that appears in the template.
+ * - $content_attributes: Same as $attributes, except applied to the main
+ *   content tag that appears in the template.

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

+ * - title_attributes: Same as attributes, except applied to the main title
+ *   tag that appears in the template.
 

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.

David Hernández’s picture

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

Ok, 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.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, this version looks good to me!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 39: drupal-doc-theme-attributes-d8-569362-39.patch, failed testing.

Status: Needs work » Needs review
jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Back 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.

webchick’s picture

Assigned: Unassigned » jhodgdon

It's been a day, but not sure what sort of time you wanted. Assigning to jhodgdon. :)

  • jhodgdon committed ca16cd4 on 8.0.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...
jhodgdon’s picture

Version: 8.0.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed to 8.0.x. Thanks! Moving to 7.x for backport of documentation, as much as is applicable anyway.

effulgentsia’s picture

Was this applied everywhere it was intended to?

  • comment.html.twig says content_attributes: List of classes for the styling of the comment content.
  • field.html.twig says content_attributes: HTML attributes for the content., which is different from the "Same as ..." pattern that the patch introduced.
jhodgdon’s picture

The 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?

jhodgdon’s picture

Assigned: jhodgdon » Unassigned
Issue tags: -Needs documentation

This was assigned to me for committing...

  • jhodgdon committed ca16cd4 on 8.1.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...

  • Dries committed 2729f1c on 8.3.x
    - Patch #569362 by scor, effulgentsia, catch: add attributes and...
  • jhodgdon committed ca16cd4 on 8.3.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...

  • Dries committed 2729f1c on 8.3.x
    - Patch #569362 by scor, effulgentsia, catch: add attributes and...
  • jhodgdon committed ca16cd4 on 8.3.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...
pol’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new12.19 KB

Here's the patch for D7.

  • Dries committed 2729f1c on 8.4.x
    - Patch #569362 by scor, effulgentsia, catch: add attributes and...
  • jhodgdon committed ca16cd4 on 8.4.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...

  • Dries committed 2729f1c on 8.4.x
    - Patch #569362 by scor, effulgentsia, catch: add attributes and...
  • jhodgdon committed ca16cd4 on 8.4.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...
Delphine Lepers’s picture

Status: Needs review » Reviewed & tested by the community
stefan.r’s picture

Patch looks fine, thanks! To be fixed on commit:

  1. +++ b/modules/field/theme/field.tpl.php
    @@ -23,7 +25,10 @@
    - *
    

    missing newline

  2. +++ b/themes/garland/comment.tpl.php
    @@ -1,5 +1,3 @@
    -<?php
    -?>
    
    +++ b/themes/garland/node.tpl.php
    @@ -1,5 +1,3 @@
    -<?php
    -?>
    

    Unrelated changes :)

pol’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new11.57 KB

Hi!

Here's the updated patch.

stefan.r’s picture

Status: Needs review » Reviewed & tested by the community
David_Rothstein’s picture

Status: Reviewed & tested by the community » Needs work
+ * - $attributes: array of HTML attributes populated by modules, intended to
+ *   be added to the main container tag of this template.

This 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.)

pol’s picture

Going to work on that, patch coming in the following hours.

pol’s picture

Status: Needs work » Needs review
StatusFileSize
new13.9 KB

Here's the updated patch.

  • Dries committed 2729f1c on 9.1.x
    - Patch #569362 by scor, effulgentsia, catch: add attributes and...
  • jhodgdon committed ca16cd4 on 9.1.x
    Issue #569362 by David Hernández, Jaza, effulgentsia: Document...

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.