I've been writing modules and little hacks for Drupal for a while. I'm playing with data structures, and am confused as to the convention for '#properties' versus 'elements'. I don't seem to be able to find anything useful on the subject, but seemingly people know what the convention is, so here's me asking...
As an example, let's say I have a data structure that holds a title and either a $form style structure, or a callback that returns one. There may also be some other config type info in there. Here's my data structure as it is now:
<?php
$mystructure = array(
'title' => t('My Title'),
'form' => $my_form,
'colour' => 'ff0000',
'dropshadow' => TRUE,
);
?>or, as a variation:
<?php
$mystructure = array(
'title' => t('My Title'),
'callback' => 'mymodule_generate_form',
'colour' => '00ff00',
'dropshadow' => FALSE,
);
?>...and so on.
So the specific question is: should those associative array keys be '#title' and '#callback' or just as they are above?
More generally, how does one decide if an array key should be '#key' or just 'key'?
Comments
I think the answer centers around nesting
I think if you have a flat structure like your example using 'key' is fine and you will find a number of examples of it Drupal. But if like the forms API your structure is nested and at each level some keys are "children" and some are properties using '#key' for the properties avoids the issue of name collision, someone adding a child element does not need to worry that a form elements name collides with a property name.
Hmm... I think I'm getting it...?
If I get this right, what you're saying is that 'key' is okay for most things. However, if you're making a hierarchical data structure, then '#key' is a better bet.
So in my scenario, where I'm putting a whole $form style structure into an array, I might actually be more correct in using '#key'.
Okay, I lied - I'm still confused.
I can see in forms that it's all '#key', which is great. However, if you're using theme_table, you can set up a cell with an array something like array('data' => 'cell contents', 'colspan' => 5) (in other words, just using 'key'). Is this because there's an inconsistency, or because it's intended that way?
You might say its past and present
The user of theme_table is older that the form API so in part it is an inconsistency. At the same time theme_table does not have the nesting issue that the form API does so '#key' is not needed. That said I recall seeing something about changing the way tables are generated and the result would use the same generation engin as the form API so keys would use '#key'