Jump to content

Problem with setting image custom field content


DaveP
 Share

Recommended Posts

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

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

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 );

 

  • Like 3
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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.

2019-11-29_082526.png.30f30438bf30ce23971c2257fbc305d3.png

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();

 

  • Like 3
  • Thanks 1
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...