It's about #1149910: drupal7 insert relative paths
Wold you add same behavior for colorbox enabled images for insert?

Comments

Status:Active» Needs review
StatusFileSize
new1.47 KB

It was easy. In the end of colorbox.theme.inc add colorbox_relative_url()

/*
* Inspiried by insert_create_url(). Cuts off base url if variable is set and makes links relative.
*/
function colorbox_relative_url($url) {
  // use insert.module's variable
  $absolute = variable_get('insert_absolute_paths', TRUE);
  if ($absolute) {
    return $url;
  } else {
    return str_replace($GLOBALS['base_url'], '', $url);
  }
}

and change template_preprocess_colorbox_insert_image()

/**
* Preprocess variables for the colorbox-insert-image.tpl.php file.
*/
function template_preprocess_colorbox_insert_image(&$variables) {
  $file = file_load($variables['item']['fid']);
  $variables['image_path'] = colorbox_relative_url(image_style_url($variables['item']['style_name'], $file->uri));
  if ($style_name = variable_get('colorbox_image_style', '')) {
    $variables['link_path'] = colorbox_relative_url(image_style_url($style_name, $file->uri));
  }
  else {
    $variables['link_path'] = colorbox_relative_url(file_create_url($file->uri));
  }
  $variables['class'] = $variables['widget']['settings']['insert_class'];
  $variables['gallery_id'] = '';
  switch (variable_get('colorbox_insert_gallery', 0)) {
    case 0:
    case 1:
    case 2:
      $variables['gallery_id'] = 'gallery-all';
      break;
    case 3:
      $variables['gallery_id'] = '';
      break;
  }
}

Status:Needs review» Closed (works as designed)

I will follow the changes in the Insert module.

StatusFileSize
new1.74 KB

This is my "backport" (attached) based on comment #1 for the last 6.x-1.x-dev. Needs lots of love (Drupal/PHP newbie) but solves my problem. My scenario: my site was installed in a subdirectory and I need the url relative to the root.

Thanks @fasdalf@fasdalf.ru!

diff -upr colorbox_original/colorbox.theme.inc colorbox/colorbox.theme.inc
--- colorbox_original/colorbox.theme.inc 2012-01-15 13:47:06.000000000 -0200
+++ colorbox/colorbox.theme.inc 2012-01-18 09:09:41.000000000 -0200
@@ -110,14 +110,14 @@ function theme_colorbox_imagefield($pres
  */
function template_preprocess_colorbox_insert_image(&$vars) {
   $vars['presetname'] = $vars['item']['presetname'];
-  $vars['image_path'] = imagecache_create_url($vars['presetname'], $vars['item']['filepath']);
+  $vars['image_path'] = _check_insert_url(imagecache_create_url($vars['presetname'], $vars['item']['filepath']));
   $vars['insert_class'] = $vars['widget']['insert_class'];
   if ($colorbox_presetname = variable_get('colorbox_imagecache_preset', 0)) {
-    $vars['link_path'] = imagecache_create_url($colorbox_presetname, $vars['item']['filepath']);
+    $vars['link_path'] = _check_insert_url(imagecache_create_url($colorbox_presetname, $vars['item']['filepath']));
   }
   else {
-    $vars['link_path'] = file_create_url($vars['item']['filepath']);
+    $vars['link_path'] = _check_insert_url(file_create_url($vars['item']['filepath']));
   }
   $vars['gallery_id'] = '';
@@ -132,3 +132,21 @@ function template_preprocess_colorbox_in
       break;
   }
}
+
+/**
+ * Inspiried by insert_create_url(). Cuts off base url if variable is set and makes links relative to the root.
+ */
+function _check_insert_url($url) {
+  // use insert.module's variable
+  $absolute = variable_get('insert_absolute_paths', TRUE);
+  if ($absolute) {
+    return $url;
+  }
+  else {
+    $url = str_replace($GLOBALS['base_url'], '', $url);
+    // Relative to the root (better?)
+    $url = $GLOBALS['base_path'] . trim($url, '\\/');
+
+    return $url;
+  }
+}
\ No newline at end of file

Any chance that this fix will be commited ?

BTW, you need to add base_path() to the trimmed URL in order this to work in all cases.