*
* @license Licensed under the General Public License version 2.0 or later.
*/
/**
* Implements hook_filter().
*/
function url_replace_filter_filter($operation, $delta = 0, $format = -1, $text = '') {
if (intval($delta) !== 0) {
watchdog('url_replace_filter', '@function invoked with non-zero delta: @delta. This should never happen.', array(
'@function' => __FUNCTION__,
'@delta' => $delta,
), WATCHDOG_ERROR);
}
switch ($operation) {
case 'list':
return array(0 => 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 $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) {
$empty = 0;
$form['url_replace_filter_' . $format] = array(
'#type' => 'fieldset',
'#title' => t('URL Replace Filter'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#theme' => 'url_replace_filter_settings_form',
'#tree' => TRUE,
);
$form['#submit'][] = '_url_replace_filter_settings_form_submit';
$settings = _url_replace_filter_get_settings($format);
$index = 0;
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;
}
/**
* Submit handler for _url_replace_filter_settings() form.
*
* Remove useless empty settings to keep variable as small as possible.
*/
function _url_replace_filter_settings_form_submit($form, &$form_state) {
$format = $form_state['values']['format'];
$format_id = 'url_replace_filter_' . $format;
if (!isset($form_state['values'][$format_id])) {
return;
}
// Remove empty sets.
foreach ($form_state['values'][$format_id] as $key => $value) {
if (empty($value['original']) && empty($value['replacement'])) {
unset($form_state['values'][$format_id][$key]);
}
}
if (empty($form_state['values'][$format_id])) {
drupal_set_message(t('URL Replace filter configuration is empty : you could remove it from this input format.', array(
'!edit' => check_url(url('admin/settings/filters/1')),
)), 'warning');
}
}
/**
* Helper to build rows in the table built by _url_replace_filter_settings().
*
* @param array $form
* The form array.
* @param int $format
* The format index.
* @param int $index
* The format index.
* @param string $original
* The original value to be replaced.
* @param string $replacement
* The replacement for the original value.
*/
function _url_replace_filter_row_form(array &$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,
);
}
/**
* Helper for _url_replace_filter_process().
*
* @param int $format
* The id of the format.
*
* @return mixed
* The replacement information for the format.
*/
function _url_replace_filter_get_settings($format) {
return variable_get('url_replace_filter_' . $format, array(
0 => array('original' => '', 'replacement' => ''),
));
}
/**
* Return the list of input formats containing the URL Replace filter.
*
* @return array
* The list of input formats, keyed by format_id.
*/
function _url_replace_filter_get_formats() {
$filter_id = 'url_replace_filter/0';
$formats = filter_formats();
$ret = array();
foreach ($formats as $format_id => $format) {
$format_filters = filter_list_format($format_id);
$format->{$filter_id} = isset($format_filters[$filter_id]);
$ret[$format_id] = $format;
}
return $ret;
}
/**
* Theme for the filter settings form.
*
* @param array $form
* A form array.
*
* @return string
* The themed form.
*/
function theme_url_replace_filter_settings_form(array $form = array()) {
$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; } /** * Implements hook_form_FORM_ID_alter(). * * Insert configuration links in modules page. */ function url_replace_filter_form_system_modules_alter(array &$form) { // No way to know what alternate themes do with this form, so play it safe. if (isset($form['#theme']) && $form['#theme'] != 'confirm_form' && $form['#theme'] != 'module_filter_system_modules') { return; } // Users without filter access can not access the module settings. if (!user_access('administer filters')) { return; } $machine_name = 'url_replace_filter'; $human_name = $form['name'][$machine_name]['#value']; // Description might have been altered, so do not rely on its default value. $description = isset($form['description'][$machine_name]['#value']) ? $form['description'][$machine_name]['#value'] : $human_name; $format_ids = _url_replace_filter_get_formats(); $links_array = array(); $filter_id = "$machine_name/0"; foreach ($format_ids as $format_id => $format) { if ($format->{$filter_id}) { $text = '!link (present)'; $link = l($format->name, "admin/settings/filters/$format_id/configure"); } else { $text = '!link (absent)'; $link = l($format->name, "admin/settings/filters/$format_id"); } $links_array[$format_id] = t($text, array('!link' => $link)); } $links = implode(', ', $links_array); $form['description'][$machine_name]['#value'] = $description . ' ' . t('(Configure in !urls)', array( '!urls' => $links, )); } /** * Implements hook_theme(). */ function url_replace_filter_theme() { $ret = array( 'url_replace_filter_settings_form' => array( 'arguments' => array('form' => NULL), ), ); return $ret; }