Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Updated: Comment #25
Problem/Motivation
html_tag does not need to be theme function. It's bad for performance and not a good use case for alterable output via the theme system.
Proposed resolution
Convert theme_html_tag to a pre-render callback and remove the theme function.
Remaining tasks
n/a
User interface changes
n/a
API changes
Any calls to theme('html_tag');
or within a render array '#theme' => 'html_tag'
need to be converted to '#type' => 'html_tag'
. Drupal 8 core does not contain either of these and always uses '#type' => 'html_tag'
, but Drupal 7 core has calls to theme('html_tag');
and contributed modules probably contain both.
Before:
$output = array(
'#theme' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'Generator',
'content' => 'Drupal 8',
),
);
After:
$output = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'Generator',
'content' => 'Drupal 8',
),
);
Before:
$meta_generator = array(
'#tag' => 'meta',
'#attributes' => array(
'name' => 'Generator',
'content' => 'Drupal 8',
),
);
$output = theme('html_tag', array('element' => $meta_generator));
After:
$meta_generator = array(
'#type' => 'html_tag',
'#tag' => 'meta',
'#attributes' => array(
'name' => 'Generator',
'content' => 'Drupal 8',
),
);
$output = drupal_render($meta_generator);
Comment | File | Size | Author |
---|---|---|---|
#25 | 1825090-25.patch | 8.19 KB | star-szr |
#25 | interdiff.txt | 449 bytes | star-szr |
#24 | 1825090-interdiff-22-24.txt | 1.24 KB | thedavidmeister |
#24 | 1825090-24.patch | 8.75 KB | thedavidmeister |
#22 | 1825090-22.patch | 8.75 KB | thedavidmeister |
Comments
Comment #1
webchick+1 from me. According to git blame, this was added in #552478-142: Restrict "self-closing" tags to only void elements in drupal_pre_render_html_tag. It'd probably be good to get some of those folks to chime in on this issue.
Comment #2
sunPlease get your search right:
Also, postponing on #1833920: [META] Markup Utility Functions
Comment #3
c4rl CreditAttribution: c4rl commented@jenlampton: I believe you are asking the wrong question. Don't you mean to ask: "Why does this function need to go through the theme layer?" If so, then I believe it qualifies as a candidate for #1833920: [META] Markup Utility Functions and I would consider retitling this issue.
@sun: Thanks for finding some examples fo where this is used. I took a look at these use cases and it seems to me that the benefit is primarily having a renderable abstraction of a tag that can be digested by drupal_alter (rather than being things that themers want to touch when theming).
This discussion brings up a question in my mind with regard to renderables. I am pondering the following:
Hypothesis: Some renderables are useful for developers via drupal_alter, but do not necessarily have a theming purpose.
Corollary: Callbacks in renderables that produce markup may be of two types: Some simply need to construct HTML that does not have a use case for being overridden by the theme layer, others need to construct HTML that does have a use case for being overridden by the theme layer.
I'm interested to hear what others think about this. Let's just focus on goals for now, not specific code implementations.
Comment #4
thedavidmeister CreditAttribution: thedavidmeister commented+1 for this, turning this theme function into a simple, alterable tag() function like l() - the "auto-closing tag" thing is a nice convenience thing for developers so it would be good to not get rid of it entirely.
Comment #5
thedavidmeister CreditAttribution: thedavidmeister commentedThis might be all that's needed here.
Comment #6
thedavidmeister CreditAttribution: thedavidmeister commentedComment #7
thedavidmeister CreditAttribution: thedavidmeister commentedProfiled a completely default (fresh install) Drupal site with #5:
Comment #9
Fabianx CreditAttribution: Fabianx commentedThis is a very interesting approach to use #type => x for the "markup utility functions" and just re-use form API definitions.
Or would it still be helpful to be able to use #theme => x consistently, but the #theme system / render API internally would know that this is a function that should just return the markup / a structured variables array?
Nice speed improvement!
Would be RTBC once comments are addressed and tests are fixed.
Comment #10
chx CreditAttribution: chx commentedLove, love, love this issue.
Comment #11
thedavidmeister CreditAttribution: thedavidmeister commented#9 - I know c4rl retracted what he wrote up here https://github.com/c4rl/renderapi in general, but there was one bit that made sense to me:
So you'd just use '#type' consistently *everywhere* and the render/theme system would know when '#type' mapped to a theme function internally, rather than vice-versa where the developer needing to know ahead of time whether what they're implementing is "type" or "theme" or in the case of the proposed MUF, a "theme-type".
Comment #12
c4rl CreditAttribution: c4rl commentedYes, there's always seemed that there's some overlap with #type with respect to hook_element_info() and hook_theme(), but we need to make sure and not confuse what folks would expect from #type.
Though, it seems that #type sometimes invoking some #theme callback and sometimes invoking a MUF seems plausible. I have a feeling that some of this discussion pertains to bigger-picture ideas in #1843798: [meta] Refactor Render API to be OO.
Comment #13
thedavidmeister CreditAttribution: thedavidmeister commented#type => 'link' already invokes l() to generate '#markup', so there's that.
@c4rl, what exactly made you change your mind about wanting to use the type for all renderable arrays? I thought that would make things less confusing, not more.
Comment #14
c4rl CreditAttribution: c4rl commented#13 @thedavidmeister The direction here seems fine at the moment; I do like the idea that #type may or may not map to associated theme callbacks. Seems to me that we'd move toward combining purposes of hook_element_info() and hook_theme() as a consistent registry. In the OO-driven architecture I'd like to move toward, what we are calling "#type" is actually instead a class definition implementing a common interface.
Carry on :)
Comment #16
thedavidmeister CreditAttribution: thedavidmeister commented#13: 1825090-13.patch queued for re-testing.
Comment #17
thedavidmeister CreditAttribution: thedavidmeister commented#14 - cool. I'll open an actual issue sometime in the next few days for the '#theme' vs. '#type' thing and link to it from the render API refactor issue.
Comment #18
Fabianx CreditAttribution: Fabianx commentedLooks very good to me, #type is appropriate here, passes tests => RTBC
This will need a change notification.
Comment #19
star-szrI would love to see this in, great work @thedavidmeister! I think HtmlTagUnitTest should be renamed and docs slightly updated since it is a WebTestBase test.
Comment #20
thedavidmeister CreditAttribution: thedavidmeister commentedhow's this?
Comment #21
thedavidmeister CreditAttribution: thedavidmeister commentedi missed a spot.
Comment #22
thedavidmeister CreditAttribution: thedavidmeister commentedtry this.
Comment #23
star-szrLooking much better, just a few more doc tweaks and then I think this is ready to go.
Contains \Drupal… per https://drupal.org/node/1354#file. The standard used to be 'Definition of…'.
Comment needs to be updated :)
Tests (plural) per https://drupal.org/node/1354#functions.
Edited to clarify last point.
Comment #24
thedavidmeister CreditAttribution: thedavidmeister commentedComment #24.0
thedavidmeister CreditAttribution: thedavidmeister commentedmake link work?
Comment #24.1
star-szrUpdate issue summary to use template, reflect latest patch and include code samples
Comment #25
star-szrJust rolling in a one-line docs update.
Also updated the issue summary. RTBC!
Comment #25.0
star-szrAdd missing code tags
Comment #25.1
star-szrRemove unneeded code samples
Comment #25.2
star-szrAdd back theme('html_tag') example, missed the element.
Comment #25.3
star-szrMore fiddling and fixing, I think I'm done now :)
Comment #25.4
star-szrRemove drupal.org URLs from code samples because they get linked
Comment #25.5
star-szrUpdate API change section intro
Comment #26
catchSo, so pleased to see this one in the RTBC queue. Committed/pushed to 8.x, thanks!
Comment #27
YesCT CreditAttribution: YesCT commentedhttps://drupal.org/contributor-tasks/draft-change-notification
is instructions for drafting a change notice.
Anyone can try the first draft. :)
Just make a comment saying you are going to try, and ask questions if you have any.
Comment #28
Shyamala CreditAttribution: Shyamala commentedI am going to work on the change notice.
Comment #29
Shyamala CreditAttribution: Shyamala commentedSummary
html_tag is no longer available as a theme function. It has been converted to a pre-render callback instead to simplify and improve performance.
API change
Any calls to
theme('html_tag');
or within a render array'#theme' => 'html_tag'
need to be converted to'#type' => 'html_tag'
. Drupal 8 core does not use this, but contributed modules need to note this.Before:
After:
Before:
After:
Comment #30
star-szr@Shyamala - looks good, thank you! The only thing is I would change the summary to something more along these lines:
I don't think the performance is the main reason why we removed it, and I don't think we need to say where the pre-render callback lives :)
Comment #31
Shyamala CreditAttribution: Shyamala commented@Cottser edited #29 as suggested.
Comment #32
star-szrGreat, I'd say that's ready to post. When you post I would also wrap these in
<code>
tags:Thank you!
Comment #33
Shyamala CreditAttribution: Shyamala commentedupdated change notice at: https://drupal.org/node/2019739
Comment #34
star-szrGreat, thanks @Shyamala! Reverting priority.
Comment #35.0
(not verified) CreditAttribution: commentedDon't mind me.