Jump to content

Recommended Posts

Posted

Hello, I don't know how to set the description when uploading multiple images through the api.

So far images get uploaded nicely.


// instantiate the class and give it the name of the HTML field
$u = new WireUpload('uploads');

// tell it to only accept 5 files
$u->setMaxFiles(5);

// tell it to rename rather than overwrite existing files
$u->setOverwrite(false);

// have it put the files in their final destination. this should be okay since
// the WireUpload class will only create PW compatible filenames
$u->setDestinationPath($page->images->path);

// tell it what extensions to expect
$u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));

// execute() returns an array, so we'll foreach() it.
foreach($u->execute() as $filename) {
$page->images->add($filename);
}

// save the page
$page->save();

Posted

It took a while, but I found the sulution:

foreach($u->execute() as $key => $filename) {
$page->images->add($filename);
$page->images->eq($key)->description = 'what you want to say about the image';
}
  • Like 1
Posted

How about

foreach($u->execute() as $filename) {

$img = $page->images->add($filename);

$img->description = "what you want to say about the image";

}

Edit: guess not. :) But it should also be possible to create a pagefile and add description before adding.

$page->setOutputFormatting(false); // or $page->of(false);
foreach($u->execute() as $filename) {
 $img = new Pageimage($page->images, $filename);
 $img->description = "what you want";
 $page->images->add($img);
}
$page->save();
$page->setOutputFormatting(true); // or $page->of(true);

Not tested but should work. I'm sure Ryan will come up with something else. But the solution you found is totally valid and also nice.

Posted

Just made a little test and changed code to add setOutputFormatting to false. This is required to get it to work.

  • Like 1
Posted

Actually in the real scenario the image is saved to in an other "parent", that parent & children doesn't have to render at all. For the forum it is good to have a complete working version of the actual page.

Posted

You should also be able to grab the last image and add the description to it:

$page->images->add($filename); 
$page->images->last()->description = "description";
  • Like 8

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