fnode Posted September 20, 2011 Share Posted September 20, 2011 Hello, PW Community I have 2 fields, one called "fileimages" typeof [image], second field is called "assets" typeof [Page]. The field "assets" contains the children of "Static" Page, please see structure below. Home |- Static |- test_01 |- test_02 Setup > Fields |- fileimages // typeof Image |- assets // typeof Page # Containing the data of "Static" from above Setup > Templates |- page // Contains "assets" <?php // site/templates/page.php foreach ($page->assets as $inc) { echo $inc->title // prints test_01, test_02 echo $inc->fileimages->basename; // Getting no value } ?> The issue is that I cannot access a field inside of another field which contains data. # $inc->fileimages->basename. Great community! Thanks to Ryan for this awesome Framework! Link to comment Share on other sites More sharing options...
ryan Posted September 20, 2011 Share Posted September 20, 2011 Check your settings for the 'fileimages' field. What is the "max number of files" set at? If it's set at "1", then you should be able to access the field like in your example. If not, then you should set it to 1 in order to make your example work. To ProcessWire, all file/image fields are multi-file fields (Pagefiles). But as a convenience, it converts them to a single file field (Pagefile) if you specify that the field will only allow a max of 1 file. So if I'm correct, you either need to set it to 1, or treat it as a multi-file field, like this: echo $inc->fileimages->first()->basename; or foreach($inc->fileimages as $file) { echo $file->basename; } Let me know if I'm incorrect about this. It has come up a couple of times, so I'm thinking about adding some more clarification/configuration options in the field settings. Link to comment Share on other sites More sharing options...
fnode Posted September 20, 2011 Author Share Posted September 20, 2011 Hi, Ryan Thank you for your response. [Maximum files allowed] for "fileimages" is set to 0 for no limit. I change the "max number of files" and I got an error: Notice: Trying to get property of non-object in D:\production\websites\processwire\site\templates\page.php Link to comment Share on other sites More sharing options...
ryan Posted September 20, 2011 Share Posted September 20, 2011 What version of ProcessWire are you using? When you edit the page, do you see files/images on it? Can you confirm that the field is called 'fileimages' (lowercase) in your Setup > Fields? Do the pages in $page->assets have any page references of their own? I change the "max number of files" and I got an error: Notice: Trying to get property of non-object in D:\production\websites\processwire\site\templates\page.php On the surface at least, it sounds like there are no images on one of the $inc pages. I would expect this error if that were the case. Before accessing a file/image, you need to check that it's populated: <?php // single file field (max images=1) if($inc->fileimages) echo $inc->fileimages->basename; // multi file field (max images=0 or > 1) if(count($inc->fileimages)) echo $inc->fileimages->first()->basename; Not sure if that's the case here, but wanted to mention it just in case. It's always a good idea to check that a file field is populated before outputting it. Thanks, Ryan Link to comment Share on other sites More sharing options...
fnode Posted September 20, 2011 Author Share Posted September 20, 2011 Hi, I am using ProcessWire 2.0. Yes, I have populated the field "fileimages" and its lowercase. I attached 2 files, so you can see my structure for my website. <?php // Code below works great! if I want the first image. I need all images attached. foreach ($page->assets as $inc) { if(count($inc->fileimages)){ echo $inc->fileimages->first()->basename; // prints project_01.png } } ?> Link to comment Share on other sites More sharing options...
ryan Posted September 21, 2011 Share Posted September 21, 2011 Thanks for the screenshots. So that's good to know that the code snippet works for the first image–what happens if you replace it with this? <?php foreach($page->assets as $inc) { if(count($inc->fileimages)) foreach($inc->fileimages as $f) echo $f->basename . '<br />'; } If it fails, check your field settings for 'fileimages' and and 'assets', and uncheck any boxes for 'autojoin'. Link to comment Share on other sites More sharing options...
fnode Posted September 21, 2011 Author Share Posted September 21, 2011 Hi, Ryan It works perfectly! <?php // foreach($page->assets as $inc) { if(count($inc->fileimages)) foreach($inc->fileimages as $f) echo $f->basename . '<br />'; // prints project_01.png, project_02.png } Thank you, Ryan for the help! 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