from Products.CMFCore.utils import getToolByName

ACTIONICON_RAMCACHE_ID = 'actioncons'
IMG_TAG = '<img src="%s" width="%s" height="%s" alt="%s" />'

def renderActionIcon(self, category=None, action_id=None,
                     base_url='', alt=''):
    """Renders the action's icon"""

    if category is None or action_id is None:
        return None

    aitool = getToolByName(self, 'portal_actionicons', None)
    if aitool is None:
        return None

    cache = self.getCache(ACTIONICON_RAMCACHE_ID)
    # compute the cache index
    index = (action_id, category, base_url, alt)

    img_tag = cache.getEntry(index)
    # the icon is not in the cache
    if img_tag is None:
        icon_path = aitool.queryActionIcon(category=category,
            action_id=action_id)
        if not icon_path:
            return



        # fall back to the icon path specified in CMFActionIcons
        img = self.unrestrictedTraverse(icon_path, default=None)
        if img is None:
            return None
        img_tag = IMG_TAG % (icon_path,
                             getattr(img, 'width', 0),
                             getattr(img, 'height', 0),
                             alt)
        if cache is not None:
            cache.setEntry(index, img_tag)
    return img_tag

from Products.CPSPortlets.PortletsTool import PortletsTool
PortletsTool.renderActionIcon = renderActionIcon