Attempting Drupal 5.x support
ray007 - March 15, 2007 - 07:18
| Project: | URL Replace Filter |
| Version: | 5.x-1.0 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
Description
Are there any plans for a drupal 5 release?
The module description sounds like something I could already have used several times, but all my installations are drupal 5 ...

#1
I'll port it if I need it on a 5.x site... But anyone is welcome to submit a patch. Should be pretty easy to port. ;-)
#2
I am trying to make a version 5.1 module for this package. However, I have only just started using Drupal and I am all at sea. I have made an info file as follows:
; $Id$name = URL Replace Filter
description = "Allows administrators to replace base URLs in <img> and <a> elements."
And I have gone through the help page for updating a 4.7 module to 5.1, making all the changes I can find that are referred to there. My end result is:
<?php
// $Id: url_replace_filter.module,v 1.1 2007/03/14 18:02:43 davidlesieur Exp $
/**
* Implementation of hook_filter().
*/
function url_replace_filter_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
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 $index => $setting) {
if ($setting['original']) {
$pattern = '!((<a\s[^>]*href)|(<img\s[^>]*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 .= '<p>'. t('This filter allows you to replace the base URL in <img> and <a> elements.') .'</p>';
$output .= theme('table', $header, $rows);
$output .= t('<p>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).</p><p><strong>Warning</strong>: To avoid unexpected results, you must include trailing slashes in both the original and replacement strings.</p><p><strong>Warning</strong>: Replacements are executed in the order you give them. Place the most specific URLs first. For example, <em>http://example.com/somepath/</em> should be replaced before <em>http://example.com/</em>.</p><p>If you need more replacement rules, more fields will be added after saving the settings.</p>');
$output .= drupal_render($form);
return $output;
}
?>
I have obtained partial success. It turns up in admin/build/modules, but I can't find any other effect upon the site. As I have never used drupal 4.7, I don't know exactly what effect this module should give, but I assume from the code that a menu of rewrites is presented (somewhere) so replacements can be inserted.
Does anyone else want to have a look and see if there is some obvious thing I am doing wrongly?
Thanks for any help!
Oh, and here's a diff with the old 4.7 .module file:
5,14d4< * Implementation of hook_help().
< */
< function url_replace_filter_help($section) {
< switch ($section) {
< case 'admin/modules#description':
< return t('Allows administrators to replace the base URL in <img> and <a> elements.');
< }
< }
<
< /**
65c55
< '#collapsed' => TRUE,
---
> '#collapsed' => FALSE,
106c96
< $row[] = form_render($form[$index][$key]);
---
> $row[] = drupal_render($form[$index][$key]);
113c103
< $output .= form_render($form);
---
> $output .= drupal_render($form);
#3
#4
The patch is now committed, and a release for Drupal 5 created. Thanks!
I have added installation instructions which should help finding how to configure the filter.
#5
I see what I was doing wrong now. Your new help file is very clear. Many thanks for writing the module!
#6