theme(INSTANCE, $var) VS theme_INSTANCE($var)

The following is most common for functions like theme('table', $var) that are included by default in Drupal core instead of calling straight theme_table($var). Since theme_table is stored in includes/theme.inc file it is always available. It might be stored in a file not always loaded though. In that case theme('table', $var) would be our only way. What's the difference though?

If we check the theme_table() function (@see https://api.drupal.org/api/drupal/includes!theme.inc/function/theme_table/7) we will read the following lines:
$header = $variables['header'];
$rows = $variables['rows'];
$attributes = $variables['attributes'];
$caption = $variables['caption'];
$colgroups = $variables['colgroups'];
$sticky = $variables['sticky'];
$empty = $variables['empty'];


This requires the header, rows, attributes, capton, colgroups, sticky and empty keys to exist even with no content. If a key is not found, an error is written to the watchdog table. Each error is of course a request to the database to insert the error message for display by the admin. If we want to create a table without any erros to the database or to the error_log file of our server, we have to make sure all the indexes above exist! Or we can recall what we define in any hook_theme() implementation (@see https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7).

In our return array, as stated in the documentation, we may return a variables index key. This key must be a variable where "each array key is the name of the variable, and the value given is used as the default value if the function calling theme() does not supply it. Template implementations receive each array key as a variable in the template file (so they must be legal PHP variable names). Function implementations are passed the variables in a single $variables function argument.". Therefore, it is better to call theme('table', ...) or theme('item_list', ...) instead of theme_table(...) or theme_item_list(...) since this way we allow the function to be correctly called, having all the values required set at least to their default values!

Comments

Popular Posts