Jump to content

Recommended Posts

Posted

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
Posted

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
Posted

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
  • 9 years later...
Posted

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...