Jump to content

Getting first image of an image field in ajax php file


Henrik
 Share

Recommended Posts

Hi,
 
I'm building my first site with PW. A great CMS btw! :)

I'm stuck here, where I try to get this ajax php file working.

I can't seem to figure out how to get the first image of $imageField.

With the following code a get this error:

Fatal error</b>: Uncaught exception 'WireException' with message 'Method Field::first does not exist or is not callable in this context'...

I also tried: $imageField->eq(0) ... and other ... but nothing seems to work.

Could somebody give me a hint how to get the first image of this images-field?

Thank you!

(Note: This is not the full code of this ajax php file - just the lines where I am stuck.)

<?php

// bootstrap ProcessWire
// Because this file isn't - and can't be - in the templates directory
// see: 
include('../index.php');

$usedTemplate = $wire->templates->get("my_template");
$imageField = $usedTemplate->fields->get("images");
$firstImage = $imageField->first();
echo $firstImage->url;

?>
Link to comment
Share on other sites

Thank you, tpr! I understand that I got just the field without page context so I changed the code by your suggestion in:

<?php

// bootstrap ProcessWire
// Because this file isn't - and can't be - in the templates directory
// see: 
include('../index.php');

$usedTemplate = $wire->pages->get('template=my_template');
$imageField = $usedTemplate->fields->get('images');
$firstImage = $imageField->first();
echo $firstImage->url;

?>

But got the same error in the ajax-response in console:

Fatal error</b>: Uncaught exception 'WireException' with message 'Method Field::first does not exist or is not callable in this context' in /homepages/... /wire/core/Wire.php:350
Stack trace:
#0 [internal function]: Wire->___callUnknown('first', Array)
#1 /homepages/... /wire/core/Wire.php(387): call_user_func_array(Array, Array)
#2 /homepages/... /wire/core/Wire.php(325): Wire->runHooks('callUnknown', Array)
#3 /homepages/... /wire/core/Wire.php(329): Wire->__call('callUnknown', Array)
#4 /homepages/... /wire/core/Wire.php(329): Field->callUnknown('first', Array)
#5 /homepages/... /my_files/_your_screen_is.php(21): Wire->__call('first', Array)
#6 /homepages/... /my_files/_your_screen_is.php(21): Field->first()
#7 {main}
thrown in <b>/homepages/... /wire/core/Wire.php</b> on line <b>350

Seems the error has to do with the syntax of the call "->first()" !?

How would I call the first image the right way?

Thanks for any help!

Link to comment
Share on other sites

/**
 * It's not the used template, it is 1 page with the template my_template
 * You need to know that Page is the connection to your field value.
 */
$usedTemplate = $wire->pages->get('template=my_template');

/**
 * You get the images field from the Fields variable. The Field you have now is the Field    
 * what is the 'bloilerplate' of the image field. The Field Object is not the place from 
 * where the you want the data from. It's is there to use for Modules and lower level stuff. 
 * Most likely, you will not often need the use of field Objects.
 *
 * Data in ProcessWire comes from the Page connection. See Page as the glue and 
 * the pointer to the field value.
 */

$imageField = $usedTemplate->fields->get('images');

/**
 * There's no first() method in the context you provide here. As we discussed above you 
 * You have a reference to a PageImage object. You see in the Exception that there is no 
 * first() method
 */

$firstImage = $imageField->first();

There's good documentation written about the Page variable, that will give you insight in how you can get your data. Next there's good documentation about images. I would suggest the read the docs carefully, it's all well written and a joy to read.

  • Like 2
Link to comment
Share on other sites

The below is essentially the same as Martijn's, but less descriptive.

You're working with a wrong concept on how pw works. $usedTemplate is a Page (while the name suggests it being a template) that holds the value(s) of the field "image", but you get them by calling either $usedTemplate->images or $usedTemplate->get("images"). $usedTemplate->fields will get you all the field objects (not values), that are defined for the template the page is using. The field object doesn't hold any values, but describes properties and methods than the field needs internally to work.

  • Like 1
Link to comment
Share on other sites

Thank you, Martijn, LostKobrakai! Got it now. I called on the field itself instead of the values.

Until now I had no problem working with templates, fields and pages. Because this file wasn't in the templates-directory I got confused somehow.

However, it's working now :)

Thank you all for your help!

  • Like 1
Link to comment
Share on other sites

Another question came up, because I would like to use the images-field as the selector instead of the template-name.

If this gets me the page(s) with template-name "my_template".

$wire->pages->get('template=my_template');

How can I get the page(s) where the template has the field "images"?

Thank you!

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...