Jump to content

Textarea: Insert image path problem


owzim
 Share

Recommended Posts

Hope this is the right place for this.

Following scenario:

PW is running on my localhost:

http://localhost/mysite

when I insert an image into the textarea the path is like this: "/mysite/site/assets/files/1028/berlin.jpg"

Then I changed to a development host name, which point to the same directory:

http://mysite.dev

Then all the inserted images do no work anymore, because obviously "/mysite/site/assets/" does not exists, it then should be "/site/assets/"

Since the image path is written in this plain form into the db, this kind of inflexible.

Any solution to this?

Edit: It sure could be done easily with a .htacces rewrite:

RewriteRule ^mysite/(.*) $1

but I'd consider this hacky then.

Link to comment
Share on other sites

I am not sure on the recommended way of avoiding this scenario in the first place, but you could run some SQL to change those links.

Something like:

UPDATE field_body SET data = REPLACE( data, '/mysite', '' )
 

This is untested and as with any manual DB manipulation, make sure you have a good backup first.

Link to comment
Share on other sites

know this is kind of obvious, but the last time i did that i just ran a replacement on the content with the api..

<?php 
		$ps = $pages->find("template=basic-page");	
		foreach($ps as $p) {
			$p->setOutputFormatting(false);
			$new = str_replace('mysite/', '', $p->body);
			$p->body = $new;
			$p->save();
		}
?>
  • Like 2
Link to comment
Share on other sites

Hey Macrura - good idea - for some reason I always resort to sql when it comes to these one off manipulations.

Off topic, but I wonder if a module that allowed you to run code snippets would be a good idea. It could perhaps even have a library of examples to deal with common issues like this, and the ability to store/share your own snippets. Kind if along the lines of Nik's excellent SelectorTest. Ideally it would report all the changes made etc.

Link to comment
Share on other sites

well i picked up the idea from Soma to always have a 'tools.inc' file in my template folder.

Then in that file i paste in the code to run, load that page, and the commands execute. I'm keeping these various snippets in codebox snippets.

I agree that this method seems a little arcane, but it works well; makes it very easy to manipulate your data...just have to be careful since you're mass editing..

Back in my joomla days i used nonumber.nl DB replacer, which enabled you to do this kind of thing, and be able to preview the changes to the data, which was nice;

this PW api technique is more powerful though; i make a lot of backups to my sites when i'm working on them, so in the event something goes awry i can revert..

so to sum up - YES! it would be great to have this in the backend, and even be able to preview the changes.. if someone out there likes to write a module...i would support it in any way i could.

Link to comment
Share on other sites

when I insert an image into the textarea the path is like this: "/mysite/site/assets/files/1028/berlin.jpg"

...

Then all the inserted images do no work anymore, because obviously "/mysite/site/assets/" does not exists, it then should be "/site/assets/"

Since the image path is written in this plain form into the db, this kind of inflexible.

Any solution to this?

Hi owzim, I'm a bit curious, but you have set up local and online-dev host yourself? If yes, you shouldn't setup them that different when using absolute image-pathes in textareas ;)

But to be serious again, - and besides the good solutions from the others on how to adjust it, - you may take a look to somas Images Manager for future projects:

...

Or you can enter the id directly:

{image=1033, width=100}
Once inserted into a textarea field it will get parsed when saved and loaded automaticly. It will store an abstract id tag in Database and convert it back to the image HTML tag. So after first save you'll see the image inserted in a Wysiwyg and be able to resize and place it as usual. ...
Link to comment
Share on other sites

I develop all my sites of subdirectories on localhost, but when migrating them they usually aren't running off subdirs. I usually load the DB sql file into my text editor and do a search/replace "/mysite/" => "/", before migrating the DB to the live site. But it won't be long before we have this task taken care of internally without any need to do replacements. 

  • Like 2
Link to comment
Share on other sites

...  

PW is running on my localhost:

http://localhost/mysite

...

Ooh, - now I've got it right: the problem are not the ImagePathes in Textareas but _all_ pathes because you setup different sites like http://localhost/mysite1/ , http://localhost/mysite2/ , ...

---

You may use Apache with vHosts and generic names like: http://pw1.machine.local/ , http://pw2.machine.local/ , http://pw3.machine.local/ , ...

---

Maybe one could use basic httpd.conf (without http-vhosts.conf) and edit two lines in it:

#DocumentRoot "C:/Apache2/htdocs"

DocumentRoot "C:/WEBSITES/pw1/htdocs"

#DocumentRoot "C:/WEBSITES/pw2/htdocs"

and

#<Directory "C:/Apache2/htdocs">

<Directory "C:/WEBSITES/pw1/htdocs">

#<Directory "C:/WEBSITES/pw2/htdocs">

to match the systempath of the site you are actually working on?

---

or you may have one Docroot and Directory in httpd.conf pointing to for example "C:/Apache2/website/htdocs" and in filesystem you have folders for your sites like

C:/Apache2/website.PW1/htdocs

C:/Apache2/website.PW2/htdocs

C:/Apache2/website.PW3/htdocs

 and you alter the path to C:/Apache2/website/htdocs for the site you work on. This only needs one edit and Apache restart isn't necessary.

--- I go with the vhosts :-) 

Link to comment
Share on other sites

Ooh, - now I've got it right: the problem are not the ImagePathes in Textareas but _all_ pathes because you setup different sites like

I think that file/image/page URLs in textareas are the only place where the issue can occur. It happens just because editors like TinyMCE are storing ready-to-output HTML. Everywhere else in ProcessWire (except cache files), everything is stored as references, so it doesn't matter where the site is running. Our eventual built-in solution will just ensure that any local URLs in textareas are always saved as if the site were running from root (via the sleepValue function), and prepend any subdirs to root paths at runtime (via the wakeupValue function). 

  • Like 1
Link to comment
Share on other sites

... Our eventual built-in solution will just ensure that any local URLs in textareas are always saved as if the site were running from root (via the sleepValue function), and prepend any subdirs to root paths at runtime (via the wakeupValue function).

Ryan, that's great!

It's that kind of great things one will find lots when starting to work with PW.  :wub:

In the past there were so much things that couldn't achvied or only with clumpsy workarounds. I just have to learn to think more positive and first try to find the most simplest solution, than look into PW to realise that it is almost in, or sometimes to get surprised that there even is a more comfortable and easier solution than that I could imagine.

Edited by horst
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...