getkirby-v2 / toolkit

This is the deprecated toolkit for Kirby v2.
http://getkirby.com
81 stars 50 forks source link

Missing subfolders in path of absolute url when using the image field #189

Closed Tapefabrik closed 8 years ago

Tapefabrik commented 8 years ago

When using a panel field of the type "image" i would expect the field function "url()" to return the absolute path of the image, like stated in the Documentation.

But instead of returning

http://www.example.com/kirby-folder/content/page/image.jpg

the function returns

http://www.example.com/kirby-folder/image.jpg

surely, you can use a workaround by using the field function image() and use the field value as parameter, but shouldn't url() behave like mentioned above anyway?

Thiousi commented 8 years ago

This is a commonly discussed topic that may seem like a bug at first but really isn't. The Toolkit has no idea what type of field was used to enter content in a page. It doesn't even know if it was added via the panel or via any other means. There is no information in the content file that indicates what is an image and what is text. To that effect, it is the developer's responsibility to transform what is merely the name of a file into a media element so the url() function can be used.

Tapefabrik commented 8 years ago

Thanks for the valuable feedback! Anyway i find it weird to not even get the message that this is not a valid media / file object for the function, isn't it?

Thiousi commented 8 years ago

Well the url function is used to construct a url but to verify that a page or asset exists at that url. This the the intended behavior of the URL function I believe.

If you try to use image() on the other hand, it will return an error if what you're passing to the function is not an image in the context of the function.

Tapefabrik commented 8 years ago

Thanks for the feedback!

lukasbestle commented 8 years ago

You can also use this:

$image = $page->myimagefield()->toFile();
if($image) {
  // image exists
  echo $image->url();
}

Please note that this is not part of the Toolkit but of Kirby CMS.