Jump to content

Change Image-Description via API


yesjoar
 Share

Recommended Posts

Hey there,

I have an image field, that only can have one image.

Is it possible to change the image description without change the uploaded file?

I want upload an image in the frontend and set a description.

But I need the possibility to change the description without upload the image again.

$p->setOutputFormatting(false);
$p->image->description = "Test";
$p->save()
$p->setOutputFormatting(true);

This returns: 

Error: Exception: Item 'description' set to Pageimages is not an allowed type

And ideas?

Best,

Kai

  • Like 1
Link to comment
Share on other sites

Hey,

The trick is that you have to call trackChange() before changing image/file properties.

Here's a piece that works for me:

$page_id = '9999'; // set to a real ID
$image_field_name = 'images';
$file_name = '10072882.jpg';

$p = $pages->get($page_id);
$p->of(false);

$img = $p->$image_field_name->get("name=$file_name");

$p->$image_field_name->trackChange("description");

$new_desc = "Surfing bird";

$img->description = $new_desc;

$p->save($image_field_name);
 

Hope it works for you too.

  • Like 4
Link to comment
Share on other sites

Hi yesjoar,

Not sure about the trackChanges as Valery mentioned. But if output formatting is off, then Pw returns an array of images regardless of your max image setting.

$p->setOutputFormatting(false);
$image = $p->image->first();
$image->description = "Test";
$p->save()
$p->setOutputFormatting(true); 
  • Like 7
Link to comment
Share on other sites

  • 9 years later...

I'm pretty sure @Kai no longer needs an answer to this problem, but I'm documenting here that I hit it, too. The clue is in the error message:

On 2/17/2014 at 11:46 AM, yesjoar said:
Error: Exception: Item 'description' set to Pageimages is not an allowed type

You (and I) were trying to set description on the Pageimages (note plural) object, which is invalid; you can only set it on a singular Pageimage object.

For me, the confusion came because I have my field configured for max 1 image, and 'Automatic' on the formatted value, meaning I should expect a singular Pageimage object or Null.

However, as the help text says on the field config page "When output formatting is off, the value is always a Pageimages array."

So my bad (import) code was:

$pwPage->image = $localPath;
$pwPage->image->description = 'My ALT Text';
$pwPage->save();

And the fixed code is:

$pwPage->image = $localPath;
$pwPage->image->first()->description = 'My ALT Text';
$pwPage->save();

Hope this helps another searcher (probably me, next time I do it!)

Link to comment
Share on other sites

In recent versions we have $page->get("images.first") and get("images[]"):

nn9rOvk.png

Note that getUnformatted is NOT the same as [] --> the unformatted value will hold temporary images whereas images[] will not.

See https://processwire.com/talk/topic/26952-get-image-in-the-context-of-a-hook/ and https://processwire.com/talk/topic/27528-weekly-update-– 2-september-2022/

 

 

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