I'm using this code in several themes and it never causes an issue. Why is this causing an out-of-memory in a bootstrap sub-theme?

function mytheme_links__node($variables) {

  foreach ($variables['links'] as $name => $values) {
    switch ($name) {
      case 'node-readmore':
        $variables['links'][$name]['attributes']['class'][] = 'btn';
        $variables['links'][$name]['attributes']['class'][] = 'btn-default';
        break;
    }
  }

  return theme('links', $variables);
}

Comments

hass created an issue. See original summary.

hass’s picture

Issue summary: View changes
markcarver’s picture

Category: Bug report » Support request
Status: Active » Closed (works as designed)

I'm using this code in several themes and it never causes an issue.

Because most themes don't fix core's broken theme system which allows for stuff, like your above bad code, to slip through its cracks.

Why is this causing an out-of-memory in a bootstrap sub-theme?

You're causing recursion by invoking the exact same theme hook:
theme('link', $variables).

By using theme(), you're sending everything back through the theme system.

The reason this causes recursion is because $variables still contains theme_hook_suggestions, which will ultimately cause this same theme function to be invoked.

A "solution" would be to simply add the following before returning (but not recommended as it has the potential to still backfire horribly):

// Don't cause recursion.
$variables['theme_hook_suggestions'] = array();
return theme('links', $variables);

---

That being said, if all you're doing is trying to add classes, then you should always do so in a preprocess function, never in a theme function:

(you should also probably utilize _bootstrap_add_class())

/**
 * Implements hook_preprocess_THEME_HOOK__SUGGESTION().
 */
function mytheme_preprocess_links__node(&$variables) {
  foreach ($variables['links'] as $name => $values) {
    if ($name === 'node-readmore') {
      _bootstrap_add_class(array('btn', 'btn-default'), $variables['links'][$name]);
    }
  }
}
hass’s picture

Thanks for the hints and the code example.

Sometimes preprocess does not allow things you need to do... but you are right - the classes can be added easier here. :-)