DaveP Posted November 26, 2019 Share Posted November 26, 2019 I'm interacting with an external API to download external images, saving them and some related info, using the new image custom fields. This error appears when trying to save a text field programmatically. Error: Exception: Item 'title' set to ProcessWire\Pageimages is not an allowed type Are there any gotchas I'm not aware of? Link to comment Share on other sites More sharing options...
dragan Posted November 26, 2019 Share Posted November 26, 2019 Can you show us some code? Link to comment Share on other sites More sharing options...
DaveP Posted November 27, 2019 Author Share Posted November 27, 2019 The 'photo' field is all set up with a 'field-photo' template. Everything works in PW back end but setting values programmatically isn't having any. The code is like this. $page->of(false); $page->photo = 'https://example.com/img.jpg'; $page->save(); $page->photo->textfield = 'Some text'; $page->save(); $page->of(true); That's an abbreviated version. Link to comment Share on other sites More sharing options...
rick Posted November 27, 2019 Share Posted November 27, 2019 I use a modified excerpt from @Robin S's module to add an image from a url like so: $field_value = $p->getUnformatted( $yourField->name ); if ( $yourField->maxFiles == 1 && count( $field_value ) ) $field_value->removeAll(); $pageimage = new Pageimage( $field_value, $url ); $field_value->add( $pageimage ); 3 Link to comment Share on other sites More sharing options...
DaveP Posted November 28, 2019 Author Share Posted November 28, 2019 @rick Thanks for that - I'll give it a try. I'm beginning to think that either I'm remembering how to interact with image fields incorrectly or my 'photo' field, which is set to contain 1 image ('photo' not 'photos') somehow isn't respecting that setting. And therefore that the error is unconnected to the new image field template setup. 1 Link to comment Share on other sites More sharing options...
rick Posted November 28, 2019 Share Posted November 28, 2019 37 minutes ago, DaveP said: @rick Thanks for that - I'll give it a try. I'm beginning to think that either I'm remembering how to interact with image fields incorrectly or my 'photo' field, which is set to contain 1 image ('photo' not 'photos') somehow isn't respecting that setting. And therefore that the error is unconnected to the new image field template setup. Using the API I find cumbersome having to reproduce the individual tasks in sequence that the admin performs by default when trying to accomplish the same goal. I rarely remember how to interact so I have many 'procedures' written as comments in my source to make it easier to find. The trials of old age. ? The original error is because of referencing the 'collection' rather than the individual item. You'll notice in that code snippet that it checks for your field maxfiles setting *and whether there is an image present. If so it removes the existing image before adding the new image to the page image object. 1 Link to comment Share on other sites More sharing options...
Robin S Posted November 28, 2019 Share Posted November 28, 2019 7 hours ago, DaveP said: which is set to contain 1 image ('photo' not 'photos') somehow isn't respecting that setting The thing to remember is that this setting is called the "Formatted value", i.e. the value when output formatting is on. When output formatting is off the value of an image field is always a WireArray. And in your code, you specifically turn output formatting off before you work with the field (as you must). On 11/28/2019 at 12:36 AM, DaveP said: The code is like this. $page->of(false); $page->photo = 'https://example.com/img.jpg'; If you treat the image field value as an array I think it will work as expected. Something like: $page->of(false); $page->photo->removeAll(); $page->photo->add('https://example.com/img.jpg'); $page->save(); $image = $page->photo->last(); $image->textfield = 'Some text'; $page->save(); 3 1 Link to comment Share on other sites More sharing options...
DaveP Posted November 29, 2019 Author Share Posted November 29, 2019 Pretty sure that's it, @Robin S. I always turn off formatting before any CRUD type operations via the API. Probably more in a cargo cult kind of a way than I should?. 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