As content author creates a page, she wants to have something like <img src="public://picture.jpg" />
. Is it possible? the goal is to avoid hard-coded drupal installation path; that is she doesn't want to have <img src="/drupaltest/sites/all/file/picture.jpg" />
on test site and <img src="/sites/all/file/picture.jpg" />
on production site.
4 Answers
You should always be using relative paths in content, like <img src="/sites/all/file/picture.jpg" />
That way if you ever change the domain name (for example when you are on the dev environment or the site url changes) you don't have to deal with a lot of headache.
You just need to properly configure your test site to handle the /drupaltest/ part for itself.
The rest of the solutions currently are giving you PHP functions for dealing with file paths, so they won't help you when creating content (PHP really shouldn't be going in your content like that).
To configure to run in a sub-directory
There are a couple of ways you can do that. You could set up your development environment so that apache is pointing to the subdirectory for the web root for that site.
The least technical way though would be to:
- In your site's .htaccess file, uncomment the line that says
# RewriteBase /drupal
and make it sayRewriteBase /drupaltest
- Install the pathologic module.
- Configure the module so that your input formats use pathologic and add your path as
/
The pathologic module is useful anyway as it rewrites links in your content to use path aliases instead of paths like node/234.
Be careful when using path auto across multiple domain names, you might find you need to clear the cache whem moving between the testing & live sites to refresh the cached path related content.
-
you are spot on - that solutions with PHP functions are not relevant to my question. And also, that my main goal would be solved if I could just have
src=/sites/...
. The problem, though, that although I have base_url set to .../drupaltest, the path for some reason is evaluated from the root. So, the question that I should have asked is "how do I properly configure my site to handle /drupaltest/ part for itself"? Any pointers?– FelixCommented Jul 28, 2013 at 18:44 -
-
I was tinkering with .htaccess - but couldn't make it work. So, I ended up creating a link on test site for sites -> drupaltest/sites. Kludgy - but simple– FelixCommented Aug 26, 2013 at 5:57
-
I have done that same thing at times. It's a nice simple solution.– roobyCommented Aug 26, 2013 at 12:25
It seems like the question is not about PHP. You can use Token Filter module to make global token values available as an input filter.
<img src="[site:url]sites/default/files/example.jpg"/>
If you install and configure Token Variable module you can type some thing like that.
<img src="[site:url][variables:file_public_path]/example.jpg"/>
-
1It is not necessary to use token filter to add the site url to the start of a url. A properly configured site will handle relative links for you without that.– roobyCommented Jul 29, 2013 at 10:01
-
What kind of configuration you are talking about? If we move site to subdirectory we have to update all img elements in body field.– ya.teckCommented Jul 29, 2013 at 10:57
-
-
-
1That is one way (easier for the end user than entering a token every time they post a link + it handles aliases). Also if you have more control over your server you can point your domain name direct to the subdir.– roobyCommented Jul 29, 2013 at 11:55
You can also use
theme_image_style($uri)
if you don't want to use an image style.
https://api.drupal.org/api/drupal/modules!image!image.module/function/theme_image_style/7