Henrik Posted September 6, 2015 Share Posted September 6, 2015 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 More sharing options...
tpr Posted September 6, 2015 Share Posted September 6, 2015 Try $wire->pages->get('template=my_template')... instead. Your code is getting the field itself but without page context so it contains no data. 1 Link to comment Share on other sites More sharing options...
Henrik Posted September 7, 2015 Author Share Posted September 7, 2015 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:350Stack 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 More sharing options...
Martijn Geerts Posted September 7, 2015 Share Posted September 7, 2015 /** * 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. 2 Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 7, 2015 Share Posted September 7, 2015 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. 1 Link to comment Share on other sites More sharing options...
Henrik Posted September 7, 2015 Author Share Posted September 7, 2015 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! 1 Link to comment Share on other sites More sharing options...
Henrik Posted September 7, 2015 Author Share Posted September 7, 2015 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 More sharing options...
LostKobrakai Posted September 7, 2015 Share Posted September 7, 2015 $wire->pages->find("images.count>0"); But keep in mind, that this will get you a PageArray, which you need to loop over, while your current selector only grabs the first page matching the selector. 1 Link to comment Share on other sites More sharing options...
Henrik Posted September 8, 2015 Author Share Posted September 8, 2015 Thank you! This additional information was important for me. I just realized that I can't use these two selectors. I use now page->id. This should work Thanks a lot! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now