You are here

icon_filter.module in Icon API 8

Same filename and directory in other branches
  1. 7 modules/icon_filter/icon_filter.module

icon_filter.module Provides a filter for text fields to replace an icon token with icon markup.

File

modules/icon_filter/icon_filter.module
View source
<?php

/**
 * @file
 * icon_filter.module
 * Provides a filter for text fields to replace an icon token with icon markup.
 */
define('ICON_FILTER_REGEX', '/\\[icon:([^:]*):([^\\]]*)\\]/');

/**
 * Implements hook_filter_info().
 */
function icon_filter_filter_info() {
  $filters['icon'] = array(
    'title' => t('Convert [icon:%bundle:%icon] tags'),
    'description' => t('Converts all [icon:%bundle:%icon] tags into the correct markup necessary for displaying a specific icon. Replace the %bundle with the bundle machine name and replace %icon with the icon machine name.'),
    'process callback' => '_icon_filter',
    'tips callback' => '_icon_filter_tips',
  );
  return $filters;
}

/**
 * Tips callback for icon filter.
 */
function _icon_filter_tips($filter, $format, $long = FALSE) {
  if ($long) {
    return t('Converts all [icon:%bundle:%icon] tags into the correct markup necessary for displaying a specific icon. Replace the %bundle with the bundle machine name and replace %icon with the icon machine name.');
  }
  else {
    return t('Convert [icon:%bundle:%icon] tags.');
  }
}

/**
 * Process callback for icon filter.
 */
function _icon_filter($text, $filter, $format, $langcode, $cache, $cache_id) {
  $icons = array();
  if (preg_match_all(ICON_FILTER_REGEX, $text, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {
      if (!isset($icons[$match[0]])) {

        // @FIXME
        // theme() has been renamed to _theme() and should NEVER be called directly.
        // Calling _theme() directly can alter the expected output and potentially
        // introduce security issues (see https://www.drupal.org/node/2195739). You
        // should use renderable arrays instead.
        //
        //
        // @see https://www.drupal.org/node/2195739
        // $icons[$match[0]] = theme('icon', array('bundle' => $match[1], 'icon' => $match[2]));
      }
    }
  }
  foreach ($icons as $search => $replace) {
    if (!empty($replace)) {
      $text = str_replace($search, $replace, $text);
    }
  }
  return $text;
}

/**
 * Implements hook_field_prepare_view().
 *
 * Processes fields to attach bundle resources (if necessary).
 */
function icon_filter_preprocess_field(&$variables) {
  $element =& $variables['element'];
  if (!empty($element['#formatter'])) {
    foreach ($element['#items'] as &$item) {

      // Some modules don't store the items as an array. Skip those here.
      if (!is_array($item)) {
        continue;
      }
      if (!empty($item['format']) && ($filters = filter_list_format($item['format'])) && !empty($filters['icon']->status)) {
        if (preg_match_all(ICON_FILTER_REGEX, $item['value'], $matches, PREG_SET_ORDER)) {
          foreach ($matches as $match) {
            if (!isset($icons[$match[0]]) && ($bundle = icon_bundle_load($match[1]))) {
              icon_process_attached($bundle);
            }
          }
        }
      }
    }
  }
}

Functions

Namesort descending Description
icon_filter_filter_info Implements hook_filter_info().
icon_filter_preprocess_field Implements hook_field_prepare_view().
_icon_filter Process callback for icon filter.
_icon_filter_tips Tips callback for icon filter.

Constants

Namesort descending Description
ICON_FILTER_REGEX @file icon_filter.module Provides a filter for text fields to replace an icon token with icon markup.