There are some variables that should just not be declared in hook_theme() or they run the risk of conflicting with attributes used by drupal_render() and/or the FAPI.

Used internally by drupal_render() - definitely don't use these for theme variable names:

access
printed
cache
type
defaults_loaded
pre_render
children
theme
render_children
markup
states
attached
theme_wrappers
post_render
prefix
suffix

Used internally by theme() - definitely don't use these in hook_theme() unless you really know what you're doing:

theme_hook_original
theme_hook_suggestion
theme_hook_suggestions
directory
attributes
title_attributes
content_attributes

Not covered above, but has special meaning within the core FAPI - generally discouraged to avoid human misunderstandings, strongly discouraged for theme hooks commonly used in/around forms unless you're implementing exactly what is documented at https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...

after_build
ajax
array_parents
autocomplete_path
collapsed
collapsible
cols
default_tab
default_value
delta
description
disabled
element_validate
empty
empty_option
empty_value
field_prefix
field_suffix
group
header
js_select
languages
maxlength
multiple
options
parents
placeholder
process
required
resizable
return_value
rows
size
title
title_display
tree
value_callback
weight

This should all be clearly documented in the docblock for hook_theme as many themers would be unaware of all the items in the full list.

Comments

jhodgdon’s picture

Maintaining this list in hook_theme() seems like it might create a problem -- if some property gets added to the Render API or Form API, I doubt it would get added to the hook_theme() documentation.

I think it would be better to say something about why you want to avoid using form/render API properties for your theme variable names, and link to where these properties are listed and documented. The properties are listed/documented in hook_element_info() -- which is also being worked on in:
#2015177: Update documentation around hook_element_info() to not say/imply element types are limited to the Form API
but since you're the one working on it there, you probably know that. :) And a link to the Form API reference page might also be useful?

thedavidmeister’s picture

@jhodgdon - this is all true.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

thedavidmeister’s picture

Title: Document a list of "reserved" variables in hook_theme that are already used by drupal_render and the FAPI » Document a list of "reserved" variable names for hook_theme that are already used by drupal_render, theme() and the FAPI

Maybe the right approach to take here is one of test coverage rather than documentation? or maybe we need both?

The proposed docs in the linked issue in #1 does have some attributes, but nowhere near as many as are currently in the issue summary, and the issue summary doesn't cover theme() internals yet so it's going to get bigger.

Edit: I'm updating the issue summary now, it should be more comprehensive shortly.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

jhodgdon’s picture

Test coverage will not help people writing hook_theme() in contributed modules.

So... maybe hook_theme() should say something like:

When choosing variable names for your theme hooks, avoid standard properties that are used by drupal_render(), theme(), or the (link)Form API(/link).

[the idea being that people would look at the documentation in those 3 places for the list of standard properties]

But... wait a minute. Does it really matter if you have a variable called "foo" and there is also a standard form/theme/render property called "#foo"?

Eric_A’s picture

I've taken a quick look at the third list. Some of these properties are owned by FAPI. Most are just data properties which have meaning for certain element types. The first category is not to be used by any theme hook that is meant to be used by a FAPI element. The second category is to be matched by theme hooks. It is not like you can swap out theme hooks blindly.

thedavidmeister’s picture

#4

But... wait a minute. Does it really matter if you have a variable called "foo" and there is also a standard form/theme/render property called "#foo"?

It does if you want to convert theme() calls to drupal_render($build) calls because they end up being the same thing and your variable ends up having two meanings (one is probably unintended).

#5

Some of these properties are owned by FAPI. Most are just data properties which have meaning for certain element types.

Yeah, it might be worth splitting this list into two lists - one for things that are actually part of the FAPI and those that are just used by certain element types because the latter is "safer".

The first category is not to be used by any theme hook that is meant to be used by a FAPI element

The first category applies to any render array, not just those in the FAPI.

The second category is to be matched by theme hooks.

This issue is only talking about hook_theme() so we're not really considering #markup style render arrays that bypass theme() atm.

It is not like you can swap out theme hooks blindly.

Currently the documentation does not really reflect this concept, that's the point of this issue. How on earth is someone who is using Drupal for the first time supposed to know that there's a bunch of variable names you could set in hook_theme() that have conflicts with the Render API itself? Why can't I swap them out blindly? How do I find out which variable names to avoid?

jhodgdon’s picture

OK...

So:
a) I don't think we want to add these lists to the documentation of hook_theme() at all.
b) We don't need to make another list in this issue of the properties referenced on the Form API Reference page. They're already listed there.
c) The properties used by drupal_render() should be mentioned in the drupal_render() documentation. The properties used by theme() should be mentioned there.
d) Therefore, I think none of the properties listed in this issue should be explicitly mentioned in hook_theme() docs. We should just tell about the general principle of "avoid reserved property names" and tell where to find out what those reserved property names would be.

Right?

thedavidmeister’s picture

@jhodgdon, I'm happy with that. However we organise it it just needs to be easy to understand and find when you're looking for it. Needing to read through the code of theme() line by line to discover the 'directory' key in $variables is pretty bad.

jhodgdon’s picture

Good. My main concern leading to #7 is maintainability of a big list of "reserved words" far away from the code that actually reserves them. The Form API Reference already has this problem... I've been trying for a couple of years to change that:
#1617948: [policy for now] New standard for documenting form/render elements and properties
#100680: [meta] Make API module generate Form API documentation
Maybe this will be the year, but I don't have much hope.

thedavidmeister’s picture

Title: Document a list of "reserved" variable names for hook_theme that are already used by drupal_render, theme() and the FAPI » Document "reserved variables" for renderables to avoid conflicts with drupal_render(), the FAPI, theme() and #theme_wrappers

As per a comment I just left at #2030243: Remove theme_file_upload_help() from the theme system, it seems to me like it might even make sense to say that the variable names of all theme hooks intended for use as #theme_wrappers (those that respect/render #children) should be unique within core and should be avoided in custom/contrib code.

The reasoning being:

- #theme does not care about #theme_wrappers
- #theme_wrappers do not care about #theme, they only care about #children
- #theme_wrappers do not even care about other #theme_wrappers
- IIRC it is not documented anywhere that certain #theme hooks are incompatible with theme hooks intended to be used as #theme_wrappers
- If we require the use of nested child elements to avoid conflicts between #theme and #theme_wrappers, then the whole concept of #theme_wrappers is redundant beyond a very modest performance boost
- The behaviour of render arrays where a single attribute is used by both #theme and #theme_wrappers or multiple #theme_wrappers is undefined

thedavidmeister’s picture

thedavidmeister’s picture

#2042285: #theme_wrappers should be able to have a unique set of variables per wrapper hook just landed, so everything in the issue summary that just references variables used by #theme or #theme_wrappers hooks in the FAPI can be ripped out.

It is now possible to "blindly" swap out #theme_wrappers hooks on an element with no negative side effects :)

Once that is done I think we can turn the final docs update into a novice issue and it should get cleaned up pretty fast after that :)

jhodgdon’s picture

See #7 for what I think is the plan to address this issue. I don't think we need to update the lists of properties in the issue summary here, because I don't think we're going to list them all but rather reference other lists. Right?

thedavidmeister’s picture

reference other lists

I think this issue should also ensure the "other lists" exist and are up to date.

jhodgdon’s picture

That seems like a separate issue. For instance, you already updated the drupal_render() docs on a seprate issue; if the others are not documenting what properties they use, let's file separate issues for them (or you can file separate issues now saying "verify that...").

thedavidmeister’s picture

Title: Document "reserved variables" for renderables to avoid conflicts with drupal_render(), the FAPI, theme() and #theme_wrappers » Document "reserved variables" for renderables to avoid conflicts with drupal_render(), the FAPI, and theme()

is it a different issue? the issue title just says we need to document reserved attributes for renderables in general, it's not referencing a specific docblock or function.

jhodgdon’s picture

That would be OK, except that if you limit the scope of this issue, it's possible that each sub-issue could be handled by a novice with a small amount of time on their hands instead of this being a huge project that will take many reviews to see if it's completely accurate.

thedavidmeister’s picture

I agree with you, but I'm just proposing a slightly different approach to limiting the scope of the issue.

I think we could start by removing each item from the issue summary that we can now ignore thanks to #2042285: #theme_wrappers should be able to have a unique set of variables per wrapper hook - like "attributes" for example, and then we can make novice issues out of what's left. I have a feeling much of what is listed under FAPI can now be ignored.

thedavidmeister’s picture

Issue summary: View changes

Updated issue summary.

sun’s picture

jibran’s picture

Title: Document "reserved variables" for renderables to avoid conflicts with drupal_render(), the FAPI, and theme() » Document "reserved variables" for renderables to avoid conflicts with drupal_render(), the FAPI, and _theme()

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.

star-szr’s picture

Thanks @andypost, I think this is separate but related (note it was filed by the same person in the same month). Looks to me like the related issue is almost done other than a formatting cleanup.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.