t('URL Replace Filter')); case 'description': return t('Allows administrators to replace the base URL in <img> and <a> elements.'); case 'settings': return _url_replace_filter_settings($format); case 'process': $text = _url_replace_filter_process($text, $format); return $text; default: return $text; } } /** * Filter the given text. */ function _url_replace_filter_process($text, $format) { $settings = _url_replace_filter_get_settings($format); foreach ($settings as $index => $setting) { if ($setting['original']) { $pattern = '!((]*href)|(]*src))\s*=\s*"'. preg_quote($setting['original']) .'!iU'; if (preg_match_all($pattern, $text, $matches)) { $replacement = str_replace('%baseurl', rtrim(base_path(), '/'), $setting['replacement']); foreach ($matches[0] as $key => $match) { $text = str_replace($match, $matches[1][$key] .'="'. $replacement, $text); } } } } return $text; } /** * Settings for the HTML filter. */ function _url_replace_filter_settings($format) { $form['#tree'] = TRUE; $form['url_replace_filter_'. $format] = array( '#type' => 'fieldset', '#title' => t('URL Replace Filter'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#theme' => 'url_replace_filter_settings_form', ); $settings = _url_replace_filter_get_settings($format); foreach ($settings as $index => $setting) { _url_replace_filter_row_form($form, $format, $index, $setting['original'], $setting['replacement']); if (!$setting['original']) { $empty++; } } // Append some empty fields while ($empty < 3) { $index++; $empty++; _url_replace_filter_row_form($form, $format, $index, '', ''); } return $form; } function _url_replace_filter_row_form(&$form, $format, $index, $original, $replacement) { $form['url_replace_filter_'. $format][$index]['original'] = array( '#type' => 'textfield', '#size' => 50, '#default_value' => $original, ); $form['url_replace_filter_'. $format][$index]['replacement'] = array( '#type' => 'textfield', '#size' => 50, '#default_value' => $replacement, ); } function _url_replace_filter_get_settings($format) { return variable_get('url_replace_filter_'. $format, array(0 => array('original' => '', 'replacement' => ''))); } function theme_url_replace_filter_settings_form(&$form) { $header = array(t('Original'), t('Replacement')); foreach (element_children($form) as $index) { $row = array(); foreach (element_children($form[$index]) as $key) { $row[] = drupal_render($form[$index][$key]); } $rows[] = $row; } $output .= '

'. t('This filter allows you to replace the base URL in <img> and <a> elements.') .'

'; $output .= theme('table', $header, $rows); $output .= t('

Enter original base URLs and their replacements. Matching is case-insensitive. You may use %baseurl in the replacement string to insert your site\'s base URL (without the trailing slash).

Warning: To avoid unexpected results, you must include trailing slashes in both the original and replacement strings.

Warning: Replacements are executed in the order you give them. Place the most specific URLs first. For example, http://example.com/somepath/ should be replaced before http://example.com/.

If you need more replacement rules, more fields will be added after saving the settings.

'); $output .= drupal_render($form); return $output; }