Scenario: I am using the Fontello provider and have created my own bundle. I am using several of the sub modules but the icon CSS for my bundle is not being added on every page.

I am adding the necessary classes to via a Form Alter and I would like to add the necessary CSS in a nice way. I can't find any function were I could do a icon_add_css(array("bundle")); or something like that. I am calling the theme function and throwing away the results but that seems cludgy.

Is there a better way to do it? Or should I flip this to a Feature Request so that it can be added in a future release (and continue with my cludge for now)?

Thanks!
Dustin.

Comments

markhalliwell’s picture

Status: Active » Closed (works as designed)

I am adding the necessary classes

You shouldn't have to manually add the "necessary classes". You should be inserting the icon via theme(), not throwing away the result.

Yes, there is a better way to do this. The Icon API is designed is to automatically include the CSS only when it's required, ie: via template_preprocess_icon().

I'm guessing here, but it sounds like this is what you're doing?

$form['#attributes']['class'] = array('icon', 'fontello', 'icon-help');

You should instead inject the entire icon markup, probably via #prefix or #suffix or even as it's own markup element:

$form['#prefix'] = theme('icon', array('bundle' => 'my_bundle', 'icon' => 'my_icon'));
// or
$form['my_icon'] = array(
  '#markup' => theme('icon', array('bundle' => 'my_bundle', 'icon' => 'my_icon')),
);
// or in reality, you could just use FAPI like this:
$form['my_icon'] = array(
  '#theme' => 'icon',
  '#bundle' => 'my_bundle',
  '#icon' => 'my_icon',
);
dustin@pi’s picture

Perfect. Thanks Mark!

The $form['#prefix'] = theme ... is what I should have been doing (at least for my particular case).