with the width and class
attributes set. A
tag of class "caption" is also added with the caption text obtained
from the title. It removes the class attribute from the
![]()
tag.
![]()
tags missing alt attributes are also marked with warning text to draw attention to
this fact.
@info
http://drupal.org/node/264932
@author
John Fletcher
@contact
http://drupal.org/user/236219/contact
*/
/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function image_caption_filter_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#image_caption_filter":
$output = '
' . t("Adds captions to images") . '
';
break;
}
return $output;
} //function image_caption_filter_help
function image_caption_filter_admin() {
$form = array();
$form['image_caption_filter_classes'] = array(
'#type' => 'textfield',
'#title' => t('Classes to be searched for image captions'),
'#default_value' => variable_get('image_caption_filter_classes', 'image-left,image-right'),
'#size' => 80,
'#description' => t('Enter a comma-separated list of classes. The filter will only operate on images which have one of these CSS classes and have a title attribute and a width set.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
function image_caption_filter_menu() {
$items = array();
$items['admin/settings/image_caption_filter'] = array(
'title' => 'Image caption filter',
'description' => t('Configure image caption filter settings.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('image_caption_filter_admin'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
//filter hook implementation
function image_caption_filter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return array(0 => t('Image caption filter'));
case 'description':
return t('Adds captions to images via a filter.');
case 'prepare':
// Nothing to prepare
return $text;
case "process":
//Look for
![]()
tags and run the do_img_titles function on the
![]()
tag
$text = preg_replace_callback('|(
)|s', 'do_img_titles', $text);
return $text;
default:
return $text;
}
} //function image_caption_filter_filter
//helper function to do the actual manipulation
function do_img_titles($img_tag_matches) {
// code changes:
// - to prevent Undefined offset: 1 notices
// - minor optimizations (this code gets executed for each image tag in each node and block): bail out on any negative condition
// other possible optimization: make $active_class_array static and process the list of variables only once
$img_tag = $img_tag_matches[0];
$return_text = $img_tag;
//the classes we will operate on
$active_classes = variable_get("image_caption_filter_classes", 'image-left,image-right');
//clean out whitespace and create array
$active_classes_cleaned = str_replace(' ', '', $active_classes);
$active_class_array = explode(',', $active_classes_cleaned);
// only execute this filter on img tags with (at least) one of the classes we are interested in
$has_class = preg_match ('/class=\"(.+?)\"/i', $img_tag, $matches) > 0;
if ($has_class) {
$class = $matches[1];
// formally, class is a space separated list of classes, but we allow all horizontal whitespace in any quantity
// that's why we use preg_split instead of explode
$classes = preg_split('/\h+/', $class, null, PREG_SPLIT_NO_EMPTY);
if (count(array_intersect($classes, $active_class_array)) > 0) {
// only execute this filter on img tags that have a title attribute
$has_title = preg_match ('/title=\"(.+?)\"/i', $img_tag, $matches) > 0;
if ($has_title) {
$title = $matches[1];
// search for width specified as an inline style or width attribute,
// if no width specified, don't output it on the outer span, assume width will be handled with css external to this module/filter
$width = '';
if (preg_match ('/width:\s*(\d+)px/i', $img_tag, $matches) == 1 || preg_match ('/width=\"(\d+?)\"/i', $img_tag, $matches) == 1) {
$width = $matches[1];
$width = " style=\"width: {$width}px\"";
}
// remove the class from the image tag
$img_tag = preg_replace ('/class=\"(.+?)\"/i', '', $img_tag);
// add the outer and caption span
$return_text = "$img_tag$title";
}
}
}
return $return_text;
} //function do_img_titles
?>