Jump to content

Getting an image from a custom field


Marinus
 Share

Recommended Posts

Hi, I've added a new field in my admin called 'coverimage'.

The fieldtype is 'Image'.

Now, in my page linked with the template where I added the fieldtype, I have been able to upload 1 image (I set the limit to '1').

However when I try to call the image, I get an error.

I am able to call the other image field with this script:

include("./head.inc"); 

echo "<div class='project_container'>";
foreach($page->children($selector) as $child)
{
echo "

<div class='project_container_thumbs'><center>";
$image = $child->images->first();
 $thumb = $image->size(215, 215); 
 echo "<a href='$child->url'><img class='project_thumb_image' src='$thumb->url'><BR/>
 <p class='thumb_image_title'>$child->title</p></a>";
echo "</center></div>

";
}
echo "</div>";
include("./foot.inc");

However, when I change the $image selector to this:

$image = $child->coverimage;

It doesn't work. Maybe I'm just missing a small point, but when I have created a fieldtype, and linked it to a template, and then have added content to that field, shouldn't I just be able to call it in this way, or is it more complicated than I think?

- Marinus

Edit;

When trying out stuff, I found out that this code outputs a result, it outputs the file name of the image linked in the 'coverimage' custom field:

$fields->get('$child->coverimage')

I guess there should be some way of getting the url of the image instead of the filename?

Edit #2;

I have found out this works to get the URL:

{$child->coverimage->url}

However, applying it like so, doesn't work!!! Argh, I am out of ideas :)

$image = $fields->get('$child->coverimage->url');
 $thumb = $image->size(215, 215); 
 echo "<a href='$child->url'><img class='project_thumb_image' src='$thumb->url'><BR/>
 <p class='thumb_image_title'>$child->title</p><br/>{$child->coverimage->url}</a>";
Link to comment
Share on other sites

Well, yes, I managed to do that, but somehow when I add the 'coverimage' data into the $thumb the page gives an error. I am lost :)

When I try to echo your code, I get this error:


Error: Exception: Unknown Selector operator: '$' -- was your selector value properly escaped? (in /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/Selectors.php line 168)

#0 /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/Selectors.php(193): Selectors->create('', '$', 'child->coverima...')
#1 /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/Selectors.php(62): Selectors->extractString('$child->coverim...')
#2 /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/WireArray.php(992): Selectors->__construct('$child->coverim...')
#3 /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/WireArray.php(1058): WireArray->filterData('$child->coverim...', false)
#4 /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/WireArray.php(1092): WireArray->filter('$child->coverim...')
#5 /Users/marinusdebeer/Sites/domains/processwire/htdocs/wire/core/WireArray.php(1102): WireArray->find('$child->coverim...')
#6 /Users/marinusdebeer/Sites/domains/processwire/

This error message was shown because you are logged in as a Superuser. Error has been logged.

I don't know why it won't work, as the script works the other way. Would really like to use my custom fields! :) I can also work around it, using multipleimages field and just have one image in there, but I don't like workarounds because then I still don't know how to make this work.

Link to comment
Share on other sites

Something really weird happened, I was trying out different things, also things that didn't work before, but now all of a sudden, this code works really well:

$image = $child->coverimage;
 $thumb = $image->size(215, 215); 
 echo "<a href='$child->url'><img class='project_thumb_image' src='$thumb->url'><BR/>
 <p class='thumb_image_title'>$child->title</p></a>";

It does what I want!

But I don't know what's different?

Link to comment
Share on other sites

It's now working properly because you are providing to the $thumb variable the coverimage field without the url, and that's how it is supposed to be when you have to generate the thumb through the size method, otherwise it souldn't work.

Edit: Think about it...to generate a thumbnail the size method has to know which image has to be resized (so its name, not the url...).

Got it?

  • Like 1
Link to comment
Share on other sites

It's now working properly because you are providing to the $thumb variable the coverimage field without the url, and that's how it is supposed to be when you have to generate the thumb through the size method, otherwise it souldn't work.

Edit: Think about it...to generate a thumbnail the size method has to know which image has to be resized (so its name, not the url...).

Got it?

Yes! I do :)

However, in my first post, I have copied literally this:

$image = $child->coverimage;

And it didn't work the first time around.. I must have missed something!

Thanks for your help :)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...