Index: pathfilter_auto.info =================================================================== --- pathfilter_auto.info (revision 0) +++ pathfilter_auto.info (revision 0) @@ -0,0 +1,4 @@ +name=Path Fiter Auto +description=Automatically converts URLs to use the pathfilter syntax. +dependencies=pathfilter + Index: pathfilter_auto.module =================================================================== --- pathfilter_auto.module (revision 0) +++ pathfilter_auto.module (revision 0) @@ -0,0 +1,113 @@ +\""; + $patterns[] = "|\"$base_path\"|"; + $replacements[] = "\"internal:\""; + + // Replace paths which use ?q= syntax (not clean urls). + $patterns[] = "|\"$base_url/?\?q=([^&\"]+)[^\"]*\"|"; + $replacements[] = "\"internal:$1\""; + $patterns[] = "|\"$base_path\?q=([^&\"]+)[^\"]*\"|"; + $replacements[] = "\"internal:$1\""; + + // Replace paths which use clean url syntax. This will include files, when + // the file directory is a subdirectory of Drupal's root. + $patterns[] = "|\"$base_url/([^\?\"]+)[^\"]*\"|"; + $replacements[] = "\"internal:$1\""; + $patterns[] = "|\"$base_path([^\?\"]+)[^\"]*\"|"; + $replacements[] = "\"internal:$1\""; + + + $count = 0; + $after = preg_replace($patterns, $replacements, $before); + if ($after != $before) { + //dpm($after, 'pathfilter_auto_replace after replacement'); // debug + $before = $after; // Replace the text that was passed in. + return TRUE; + } + +} + +/** + * Implementation of hook_form_alter. + * + * Here, we ensure our validation callback is invoked for all forms. + */ +function pathfilter_auto_form_alter($form_id, &$form) { + + $validate_data = array('pathfilter_auto_validate_callback' => array()); + if (is_array($form['#validate'])) + $form['#validate'] = $validate_data + $form['#validate']; + else + $form['#validate'] = $validate_data; + } + +function pathfilter_auto_validate_callback($form_id, $values, $form) { + //dpm(func_get_args(), 'pathfilter_auto_validate_callback'); + + _pathfilter_auto_validate($form); +} + +/** + * This function finds all textareas in a form and performs our replacement on + * only those fields. + */ +function _pathfilter_auto_validate(&$elements, $k = NULL) { + foreach (element_children($elements) as $key) { + if (isset($elements[$key]) && $elements[$key]) { + _pathfilter_auto_validate($elements[$key], $key); + } + } + if ($elements['#type'] == 'textarea') { + //dpm($elements, '_pathfilter_auto_validate'); + $text = $elements['#value']; + $replaced = pathfilter_auto_replace($text); + if ($replaced) { + form_set_value($elements, $text); + // TODO: perhaps set a message here to let the user know replacement has occurred. + } + } +} + +?> \ No newline at end of file