The API doc for hook_element_info() is extremely outdated---it only talks about form elements (even though the "elements" provided through this hook are now types for renderable arrays, cf. system_element_info() and http://drupal.org/node/930760 .

It also does not document the vital #theme and #theme_wrapper properties that are used in such more general elements.

I'd be willing to roll a patch in the next couple of days to get the doc for this function updated, unless someone more experienced than me wants to step in.

Comments

jhodgdon’s picture

Please do roll a patch, thanks!

sven.lauer’s picture

Status: Active » Needs review
StatusFileSize
new5.97 KB

Here is a first stab at a patch. It is quite lengthy, but this is in part because it is rather unintuitive that #types can be DECLARED just by specifying their default values (it is a very ELEGANT way to do it, but not quite intuitive). And it seems this hook, or alternatively an associated handbook page is the place to do it. Hence the lengthy explanation of the examples. I could be convinced that some of this information is too detailed for a doc block.

I also included a short paragraph about this being the not-quite-equivalent of hook_elements() in D6, which is something that is not done often in the API doc, so maybe this is against common practice. Please advise ;)

Also, I did not break the lines within @link ... @endlink directives intentionally, as I was not sure whether the parser can handle line breaks in there. I'd be happy to re-roll with line breaks.

Feedback much appreciated.

sven.lauer’s picture

Another thing: I think the sample hook implementation here could use a more useful example---maybe a subset of system_element_info()? Is the doc queue the right place to talk about something like that?

jhodgdon’s picture

The sample hook implementation for this function should be discussed right here.

I don't have time at this moment to look at this patch in detail, but one thing I noticed right away is the first line, which doesn't conform to standards (should be one line, not wrapped, and start with a different verb -- please look at hook standards on http://drupal.org/node/1354 or I can elaborate later -- probably not for a few weeks though as I'm leaving tomorrow for DrupalCon London).

Also needs some proofing... anyway I will get back to this eventually, but you might make a second pass and make sure the whole thing makes sense and conforms to standards...

sven.lauer’s picture

StatusFileSize
new5.87 KB

Right, the initial line thing escaped my attention because I only edited it (I also blame the retention of the awkward / non-standard "allow" on that ;).

The attached patch fixes both things, and some of the omitted/doubled articles and punctuation. Also add a reference to theme() when mentioning overriding of theme functions.

Have fun at DrupalCon!

jhodgdon’s picture

Status: Needs review » Needs work

First line is better... I've found some time to review the patch:

+ * This hook allows modules to declare their own element types to be used
+ * in Form API arrays and renderable arrays. This is typically done by
+ * specifying default values for the element type which instruct the APIs how
+ * to treat the element.

This needs some wrapping attention... and "which" should always have a comma before it (or else it should be "that").

But this paragraph doesn't seem right to me. "allows modules to declare element types" -- yes. "this is done by specifying default values"??? That's not all you have to do to declare an element type, so that sentence doesn't make sense to me.

+ * After an element type has been registered via an implementation using this
+ * hook, elements of renderable arrays and form arrays can use the name of the
+ * element as the value of the #type property.
 

Should that first line be: "via an implementation of this hook"? And maybe in place of "can use the name" something like "can declare an element of this type by using the name"?

I like the example... but maybe if you're going to talk about the #tag property in the usage example, you should put that into the simplified declaration? Or otherwise explain how you would know that #tag is a property (like looking at the theme function, if that is where it is)? Oh I see, you explain that below... but I had sort of a "what?!? where did that come from" moment when I saw that, so maybe the explanation should be moved up?

+ *
+ * The most common case for types for use in renderable arrays is to specify
+ * a value for '#theme', as above, or '#theme_wrappers'. See the documentation
+ * of drupal_render() for details on these properties.

Case of what? Is this related to the previous discussion, or are you starting a new discussion?

+ * Drupal 6 had a similar hook, hook_elements(), whose work is now done by

We don't document D6 to D7 changes in D7/8 documentation. You can put a comment on the d6/7 corresponding function pages on api.drupal.org to link them together, and changes are documented on the API change pages/change nodes.

One more thing: I'm not sure if I agree with taking out the list of commonly-used array elements for types. You don't think this was useful?

jhodgdon’s picture

Back to the original report, it was asking #theme_wrapper and #theme to be *added*, not for the list of standard elements to be removed...

sven.lauer’s picture

I'll re-roll another version of this patch, some comments (possible reactions to these might help me improve these parts):

But this paragraph doesn't seem right to me. "allows modules to declare element types" -- yes. "this is done by specifying default values"??? That's not all you have to do to declare an element type, so that sentence doesn't make sense to me.

I know that sound weird/counter-intuitive, but that is exactly how it works, technically (unless I am completely misunderstanding things. But I think I would have noticed that implementing #types and reading the code): All you have to do to declare a #type is to implement this hook and specify default values for its #-properties. Once you have done this, you can use the #type.

Of course, in most cases, these default values will refer to callback functions that you'll want to implement for things to work correctly. But this is not necessarily the case. For example, I could "declare" a type by having the following in my hook_element_info()-implementation:

$types['div'] = array(
  '#theme' => 'html_tag',
  '#tag' => 'div',
  ),
);

And given that theme_html_tag() is implemented in core, I am done. I could use something like

$render['note'] =>array(
  '#type' => 'div',
  '#value' => "My content",
);

which would be rendered as:

<div>My content</div>

The example is artificial, but the point, more generally, is that you could be done with all your implementation work simply by making use of callback functions defined by other people.

Perhaps, we should avoid of #types being "declared" at all, for in a very real sense, there are not declared anywhere at all. Then again, what makes the render/form API aware of a #type is precisely the definition of this hook, so it does make some sense to speak of them being "declared" here.

I like the example... but maybe if you're going to talk about the #tag property in the usage example, you should put that into the simplified declaration? Or otherwise explain how you would know that #tag is a property (like looking at the theme function, if that is where it is)? Oh I see, you explain that below... but I had sort of a "what?!? where did that come from" moment when I saw that, so maybe the explanation should be moved up?

Point taken. When I read the text for the first re-roll, I actually thought something similar---that I should mention the #tag in the text before.

Part of the reason for choosing THIS example (besides 'html_tag' being very simple) is that '#tag' is NOT part of the "declaration" of the type. Because the properties of a type are not declared in the implementation of this hook, only default values are provided. They are really not declared anywhere (but specified in the documentation of the theme functions, in most cases). I'll try to think of a way to make this more clear.

jhodgdon’s picture

Well, I still think the statement about "all you need to do" is a bit misleading, since the normal case is that you would need to write some theme or process types of functions. Maybe it could be clarified a bit?

sven.lauer’s picture

When I re-roll the patch, I will try and be somewhat clearer on this, by adding to/rewriting the initial paragraph to mention the implementation of callback functions.

jhodgdon’s picture

Issue summary: View changes

This needs to incorporate:
#348581: API documentation of hook_elements
#2234571: hook_element_info does not mention #value_callback
(which I'm marking as duplicates in a moment).

jhodgdon’s picture

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.

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.

quietone’s picture

Status: Needs work » Closed (outdated)