function _filefield_paths_replace_path in File (Field) Paths 7
Run regular expression over all available text-based fields.
Parameters
$old:
$new:
$entity:
1 call to _filefield_paths_replace_path()
- filefield_paths_filefield_paths_process_file in modules/
filefield_paths.inc - Implements hook_filefield_paths_process_file().
File
- ./
filefield_paths.module, line 412 - Contains core functions for the File (Field) Paths module.
Code
function _filefield_paths_replace_path($old, $new, $entity) {
$info = parse_url($old);
if (isset($info['path'])) {
$info['host'] .= $info['path'];
}
// Generate all path prefix variations.
$prefixes = _filefield_paths_replace_path_get_prefixes($info['scheme'], TRUE);
$prefixes = implode('|', $prefixes);
// Generate all image style path variations.
$styles['raw'] = "styles/REGEX/{$info['scheme']}/";
$styles['urlencode'] = urlencode($styles['raw']);
foreach ($styles as &$style) {
$style = str_replace(array(
'/',
'REGEX',
), array(
'\\/',
'(.*?)',
), $style);
}
$styles = implode('|', $styles);
// General all path variations.
$paths['raw'] = preg_quote($info['host'], '/');
$paths['urlencode'] = preg_quote(urlencode($info['host']), '/');
$paths['drupal_encode_path'] = preg_quote(drupal_encode_path($info['host']), '/');
$paths = implode('|', $paths);
// Newer versions of the Image module add an 8 character token which is
// required if the image style hasn't been generated yet.
$itok = '';
if (defined('IMAGE_DERIVATIVE_TOKEN')) {
$itok = '((?:[\\?|&](?:\\S+?&)*|(?:%3F|%26)(?:\\S+?%26)*)' . IMAGE_DERIVATIVE_TOKEN . '(?:=|%3D)(\\S{8}))*';
}
// Build regular expression pattern.
$pattern = "/({$prefixes})({$styles})*({$paths}){$itok}/";
// Create an anonymous function for the replacement via preg_replace_callback.
$callback = function ($matches) use ($new, $old) {
return filefield_paths_replace_path_callback($matches, $new, $old);
};
if (!$callback) {
watchdog('filefield_paths', 'Unable to create an anonymous function to find references of %old and replace with %new.', array(
'%old' => $old,
'%new' => $new,
));
return;
}
$fields = field_info_fields();
foreach ($fields as $name => $field) {
if ($field['module'] == 'text' && isset($entity->{$field['field_name']}) && is_array($entity->{$field['field_name']})) {
foreach ($entity->{$field['field_name']} as &$language) {
foreach ($language as &$item) {
foreach (array(
'value',
'summary',
) as $column) {
if (isset($item[$column])) {
$item[$column] = preg_replace_callback($pattern, $callback, $item[$column]);
}
}
}
}
}
}
}