Currently, if an object is used as a render element in a theming function, a fatal error occurs in theme.inc as it treats the $variables array as,.... well, an array.

It would be nice to be able to do something like this as more and more items are converted to objects:


/**
 * Implements hook_theme().
 */
function currency_api_theme() {
  return array(
    // Used in theming the base currency properties.
    'currency_number_code' => array(
      'render element' => 'currency',
    ),
  );
}

function theme_currency_number_code(StdClass $currency) {
  return $currency->number;
}

theme('currency_number_code', $currency);

This is much nicer from a developer point of view c/f

theme('currency_number_code', (array) $currency);
theme('currency_number_code', array('currency' => $currency));

Cheers

Comments

joelpittet’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

That is quite a big architectural change. I don't think we want to do this, maybe D9 proposal but definitely not in D8. It's nice to have expectations of an array of variables coming into the template.

I'd suggest

function theme_currency_number_code($variables) {
  return $variables['currency']->number;
}

but really this should be a currency.html.twig file and strongly suggest using theme functions as they are likely deprecated going forward for security reasons and other DX issues.