1

I'm trying to output the full asset URL. Using Craft 3.0.0-RC7.1

In .env:

ASSETS_BASE_PATH=/Users/user/code/sitename/web
ASSETS_BASE_URL=http://sitename.test

In config/volumes.php:

'siteAssets' => [
    'path' => getenv('ASSETS_BASE_PATH') . '/assets/images',
    'url' => getenv('ASSETS_BASE_URL') . '/assets/images',
],

In my template:

<meta property="og:image" content="
{{ entry.socialSharingImage.one.getUrl('socialSharing') }}
" />

Output:

<meta property="og:image" content="
/index.php?p=actions/assets/generate-transform&amp;transformId=58">

As you see, no site url at the start. Any ideas?

It seems the getUrl() method returns the path. Prepending it with {{ siteURL }} creates two /'s e.g. http://sitename.test//asseturl.jpg. Same results in dev and production.

Thanks!

3

You can slice the / from the end of {{ siteUrl }} and combine it with getUrl relative path:

<meta property="og:image" content="{{ siteUrl|slice(0,-1) }}{{ entry.fieldName.one().getUrl('kafel620') }}">

This will produce expected path:

<meta property="og:image" content="http://site.local/assets/images/ramPamPam/headers/_kafel620/tagiatelle.jpg">
| improve this answer | |
1

Prepending it with {{ siteURL }} creates two /'s e.g. http://sitename.test//asseturl.jpg

It would do that if you've defined your site URL (either in Settings->Sites->Base URL or using the CRAFT_SITE PHP constant) with a trailing slash at the end of it.

i.e. http://sitename.test/

Changing it to http://sitename.test would work.

| improve this answer | |
  • Thanks @Brad Bell - however using the @web alias forces a trailing slash upon saving the site settings page. Presumably I would have to set the URL explicitly to avoid this. – Adam Menczykowski May 10 '18 at 14:01
  • Also, explicitly setting the domain name in Site config also forces a trailing slash on save – Adam Menczykowski May 10 '18 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.