Jump to content

[solved] storing image properties description


rushy
 Share

Recommended Posts

Bit of a newbie type question, even though I've been using PW for quite a while, I've not had to manipulate assets from the front end before.  I now have a need to update image properties from the front end and I'm trying to update an image description and tags when clicking on a link. I get the selected image and for example I can delete it with the code below. But I am missing some basic understanding when updating image description field as nothing happens, no errors but the description field remains empty. Any idea what steps I am missing? Many thanks. 

// how to update image description?
$al = $pages->get($album);
$pgfile = $al->images->getFile($file);

$al->of(false);
$pgfile->description = "Test description";
$al->save();

// to delete an image - this works
$al->of(false);
$al->images->delete($file);
$al->save();

 

Link to comment
Share on other sites

Hi, here is some code written in the browser that may do it. You should doublecheck if your $file variable is the right file basename!

$p = $pages->get($album);								// get the page
$p->of(false);

$p->images->trackChange('description');       			// prepare page to keep track for changes

$selectedImage = $p->images->getFile($fileBasename);  	// Get the Pagefile having the given !basename!, or null if not found.
$selectedImage->description = "Test description";		// set new description

$p->save('images');                           			// save the page
$p->of(true);

 

  • Like 2
Link to comment
Share on other sites

Thanks Horst.  My file basename is correct as I am able to echo out all the properties of the image, but I did not know about trackChange perhaps that is the missing bit.

$pgfile = $al->images->getFile($file);
if($pgfile) {
	echo "basename => $pgfile->basename \n";		
	echo "description => $pgfile->description \n";
	echo "ext => $pgfile->ext \n";
	echo "filename => $pgfile->filename \n";
	echo "filesize => $pgfile->filesize \n";
	echo "filesizeStr => $pgfile->filesizeStr \n";
}

The above echoes out all the properties as expected , but assigning description to something does not update it when I save page. I guess I'm only changing a local copy here and it's not "connected" to the page. I imagine the trackChange makes that happen. I will try it and report back.

Many thanks

  • Like 1
Link to comment
Share on other sites

I confirm that this works - when I called trackChange before save the change to the image description field was savedA. Thanks again Horst.

As a footnote, when updating tags I found that I can use $image->addTag('foo,bar,baz') without trackChange and tags were saved because it's a dedicated function to add tags.

Regards

  • Like 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

×
×
  • Create New...