Problem/Motivation
template_preprocess_*()
functions claim in their comments to implement hook_preprocess_*()
functions (e.g. user_preprocess_block() Implements hook_preprocess_block().
), which is unhelpful (if not misleading) since the latter don't actually exist in the codebase or the documentation. Hence it can't be looked up by name or auto-linked in the API to its definition. The result for newcomers is confusion and wasteful misdirection.
Proposed resolution
We propose amending the documentation standard according to the following example:
/**
* Implements hook_preprocess_HOOK() for theme_panels_pane().
*/
function mymodule_preprocess_panels_pane(&$variables) {}
Note: This standard has now been adopted, and can be seen at http://drupal.org/node/1354#hookimpl
Remaining tasks
A patch needs to be written to implement the change across the Drupal 8 core codebase.
Comment | File | Size | Author |
---|---|---|---|
#51 | drupal-hook_preprocess_hook-1443202-51.patch | 20.17 KB | TravisCarden |
#50 | drupal-1443202-50.patch | 20.29 KB | TravisCarden |
#48 | drupal-1443202-48.patch | 20.17 KB | TravisCarden |
#48 | interdiff.txt | 4.36 KB | TravisCarden |
#43 | 1443202-43.patch | 18.63 KB | xjm |
Comments
Comment #1
xjmIt seems to me we might want to add a more explicit standard for how to document these at http://drupal.org/node/1354#themeable. A link to
hook_preprocess_HOOK()
(which, btw, I think is mislabeled anyway) would be clearer. The information the user really needs is in the documentation fortheme()
, I think.Comment #2
jhodgdonAgreed, we need a better standard, and agreed, this function should not say it is implementing a nonexistent hook.
For FORM_ID_alter hooks, I think our standard is something like:
Implements hook_form_FORM_ID_alter() for form_whatever_it_is().
Perhaps we could do that here as well, or maybe something like:
Implements a theme() preprocess function for blocks.
Thoughts?
Comment #3
tim.plunkettAssuming we like
hook_preprocess_HOOK
, I thinkImplements hook_preprocess_HOOK() for PLURAL_HOOK.
, likeComment #4
jhodgdon#3 -- That's fine for hook_preprocess_HOOK(), but it doesn't do much for the other functions listed on the theme() documentation page that don't have special hook docs pages... Oh actually I guess all of them that belong in modules do, so maybe this is fine...
OK, I'm +1 on #3. Any other opinions?
Comment #5
xjmI think #3 is good also.
Comment #6
tim.plunkettThe only problem with pluralizing the hook is when the hook isn't just a single word.
In the case of
function MYMODULE_preprocess_panels_pane(&$variables) {}
Implements hook_preprocess_HOOK() for panels_pane.
(just the hook)Implements hook_preprocess_HOOK() for panels_panes.
(attempt to pluralize the hook)Implements hook_preprocess_HOOK() for panels panes.
(attempt to pluralize and English-ify the hook)Comment #7
tim.plunkettThe hook_form_FORM_ID_alter standard is closest to #6.3 from above, so I guess that is the correct way to go?
But that leads to
Since FullCalendar is a camelCased proper noun (not my doing, btw).
I'd be personally most happy with #6.1, just take the $hook part and no pluralizing at all.
Comment #8
sunHonestly, I'm not sure how much of a problem this is in the first place.
But anyway, if you guys insist that it's a problem, then I'd go with the following: (similar to #6.1, but more explicit)
This
(thus, eliminating a potential @see)
Comment #9
tim.plunkettOhhh! Very good. That makes SO much sense.
#8++
Comment #10
jhodgdon#8 is OK; in the standard we should say to use whatever.tpl.php though if it's a template not a function, right?
Comment #11
TravisCarden CreditAttribution: TravisCarden commented#8 answers my primary concern in this issue, which is the confusion caused newcomers by referring to non-existent functions in documentation and the difficulty for them of finding the function signature or usage examples. #8++
Comment #12
tim.plunkett#10, I'm not sure, because you still call those template files with
theme('whatever', array());
.Comment #13
jhodgdonRE #12, yes, but you can't call theme_whatever(). You need to call theme('whatever') to get either the theme_whatever() or the whatever.tpl.php function.
So probably we should really do instead of #8:
With the current API module, this should (!!) automatically link to either theme_panels_pane() or panels-pane.tpl.php (whichever actually exists, with a preference for the latter).
Comment #14
sunIn that case, I'd rather go with
for theme_foo_bar()
orfor foo.tpl.php
.Functional code à la
for theme('foo')
doesn't make much sense for me, as it's only really understood by API module's parser.Comment #15
jhodgdonI'm happy with #8/#14 or #13. #13 has the advantage of being what you would actually call in code, but #14 has the advantage of letting you know what the actual function is if you are living in the editor rather than on api.drupal.org. So probably a slight preference for #14.
Comment #16
tim.plunkettI couldn't find this issue earlier, retitling.
Comment #17
jhodgdonAh... So the two proposals for documentation headers for preprocess functions are:
a)
This assumes the theme_panels_pane() function exists; if it's actually a template you would say "for panels-pane.tpl.php".
b)
So far, I think sun and I have both voted for (a) -- the advantage is it links to the actual function/template where the variables and such are documented. Any dissenting opinions?
Comment #18
tim.plunkettI'm wholly in support of #17a.
Comment #19
TravisCarden CreditAttribution: TravisCarden commentedI completely agree. I think (a) is the better choice because it maps exactly to a real function that can be hyperlinked in the documentation.
Comment #20
jhodgdonTentatively setting the policy to RTBC. If no one objects in the next few days, I'll edit http://drupal.org/node/1354 to put this policy in. Then we might consider a patch...
Comment #21
TravisCarden CreditAttribution: TravisCarden commentedI'd love to try my hand at a patch when the time comes, if that's agreeable.
Comment #22
jhodgdonTravis: Sounds good!
Comment #23
webchickCould the summary please be updated to what the proposal is?
Comment #23.0
webchickUpdated issue summary.
Comment #24
TravisCarden CreditAttribution: TravisCarden commentedYes. Done.
Comment #25
webchickAh-ha! Yes, that makes total sense. Thanks! :)
Comment #26
jhodgdonOK, the new policy has been added to http://drupal.org/node/1354#hookimpl and I also put links in the theme sections to there.
Next up: a patch is needed. I'll update the issue summary.
Comment #26.0
jhodgdonUpdated issue summary.
Comment #27
TravisCarden CreditAttribution: TravisCarden commentedPerfect. I'll get to work on a patch.
Comment #28
TravisCarden CreditAttribution: TravisCarden commentedWorking on this turned up another little oddity: The comments on
hook_entity_view_alter()
in entity.api.php say, "Alternatively, [a module] could also implement hook_preprocess_ENTITY()." Not only doeshook_preprocess_ENTITY()
not exist, neither doestheme_ENTITY()
. In fact, this is the only mention of such a function in the entire code base. My patch is ready, excepting this change, which I'll include if someone can tell me what it should say.Comment #29
tim.plunketthook_preprocess_ENTITY and theme_ENTITY refer to a specific theme_HOOK and hook_preprocess_HOOK where HOOK is one of the ENTITY types. So, comment, node, user, taxonomy_term, etc.
It probably doesn't need to be documented separately, but it doesn't hurt.
Comment #30
TravisCarden CreditAttribution: TravisCarden commentedSo what would you do with this comment to keep from referring to another non-existent function?
Attached is what I've done with the other instances.
NOTE: I also noticed that
overlay_preprocess_maintenance_page()
had a@see
directive pointing at itself, so I removed it.Comment #31
jhodgdonWhat I would do with that comment in #30:
Alternatively, it could also implement hook_preprocess_ENTITY().
==>
Alternatively, it could also implement hook_preprocess_HOOK() for the particular entity type template, if there is one (e.g., node.tpl.php).
This whole line was a bit suspect... For instance, there is no user.tpl.php or taxonomy.tpl.php, so it wouldn't have worked to do hook_preprocess_ENTITY for those entity types.
So... looking at the patch in #30, you have the right idea. However, some of the "theme_" functions are actually nonexistent. For instance, there is no theme_block() -- that line should be
+ * Implements hook_preprocess_HOOK() for block.tpl.php.
You'll need to go through the patch and see which are really functions and which are templates.
Unfortunately, a simple search on api.drupal.org won't help you, because of a point of history that we are *nearly* ready to get rid of. There's this lame file called theme.php in the Documentation repository that has existed for ages to point people from nonexestent theme_xyz() functions to the proper xyz.tpl.php file. theme_block() is one of the fake functions in there. Basically, any "theme function" that is listed on that page needs to be replaced by the tpl.php file in your patch.
As soon as we finish with
#716496: Theme functions group needs some updates
I'll be getting rid of theme.php and we will no longer have this confusion.
Comment #32
TravisCarden CreditAttribution: TravisCarden commentedAh! Tricky. ;) Here's an updated patch; thanks.
Comment #33
jhodgdonMuch better! I looked through this and found this little issue:
- Should be one space not to after .
- * in the middle of the line
Otherwise, I think this is ready to go. Thanks!
Comment #34
jhodgdonsorry that was in
Comment #35
TravisCarden CreditAttribution: TravisCarden commentedOops! Good catch. Here you go.
Comment #36
tim.plunkettAll of the implementations in locale and rdf were using MODULE_preprocess_HOOK, which is close but still wrong. Grabbed those too.
Comment #37
jhodgdonWe are unfortunately over thresholds for critical/major issues right now, and I know this will conflict with at least one patch in the queue
#787652: Forum vocabulary alterations possibly obsolete -- possible to delete forum vocab
So I won't be able to commit it for now, and it will need a reroll once that patch lands.
However, on the good side, the patch looks really good. :) I reviewed it carefully and didn't find a single thing wrong with it (as picky as I am).
Comment #38
TravisCarden CreditAttribution: TravisCarden commentedWell I'll be happy to re-roll the patch whenever you're ready, @jhodgdon. I'm consciously trying to "level up" in terms of contribution, and as a first step, I'd like to see this patch through.
Comment #39
xjmInteresting; so we don't actually have any
theme_foo()
for these in core? Templates only?I agree that #36 looks great.
Comment #40
no_commit_credit CreditAttribution: no_commit_credit commentedNinja-removing some
t()
on the assertions we're updating. No other changes.Comment #42
xjmOops, looks like it needs reroll already.
Comment #43
xjmRebased.
Comment #44
xjmMoo.
Comment #45
jhodgdonRE #39 - we never have both a theme_* function and a *.tpl.php. In hook_theme() you indicate one or the other.
Comment #46
catch#43: 1443202-43.patch queued for re-testing.
Comment #48
TravisCarden CreditAttribution: TravisCarden commentedLooks like
locale_preprocess_block()
has gone away. Here's an updated patch.Comment #50
TravisCarden CreditAttribution: TravisCarden commentedAnd another change came through in the meantime. Again:
Comment #51
TravisCarden CreditAttribution: TravisCarden commentedMore changes upstream, and
Implements hook_preprocess_block().
sneaked into language module.Comment #52
xjmStill good.
Comment #53
jhodgdonWith this many files being touched, committing this will have to wait until we're under thresholds, unless someone wants to volunteer to figure out which needs review / RTBC critical/major issues in 8.x with patches need rerolling, and reroll them all after I apply this patch. :(
Comment #54
TravisCarden CreditAttribution: TravisCarden commentedAlas, I'm not in a position to make such a commitment. But I'll happily reroll this patch as necessary at the appropriate time. Just say when! Thanks.
Comment #55
jhodgdonOK, we have a new policy on "large" patches. I plan to hit the "retest" link on Thursday May 3rd on this patch (i.e., wait until after the next 7.x release is out, to avoid delaying it, since many 8.x patches go directly to 7.x).
Then, if there are no "avoid commit conflicts" tagged issue in the queue that conflict, I'll give this a final review and commit it. If anyone wants to delay, now's your chance to request a delay (e.g., wait until this issue is in, which should be very soon). Or if there are some "avoid commit conflicts" issues, maybe we can get a volunteer or two to reroll just those patches.
Comment #56
jhodgdon#51: drupal-hook_preprocess_hook-1443202-51.patch queued for re-testing.
Comment #57
jhodgdonThe patch still applied, and there were no uncommitted "avoid patch conflicts" patches in the queue... so I *finally* committed this patch. Thanks to all who participated, and for all of your patience in trying to do this kind of cleanup in a fast-moving development environment!
I think we decided we wouldn't try to port this to D7, so I'm just marking it fixed.
Comment #59
tim.plunketthttps://drupal.org/node/1354/revisions/view/2415402/2546068 removed this standard, and I can't find where it was moved to.
Comment #60
jhodgdonThat standard got removed inadvertently (sorry about that!) but a new one is being discussed since that old one was never really formalized and didn't work well with Twig apparently:
#1913208: [policy] Standardize template preprocess function documentation
Comment #60.0
jhodgdonpart 1 has been done, update summary accordingly
Comment #61
TravisCarden CreditAttribution: TravisCarden commented