How to add assets to data object via api

Hello,

I exported all my data object and assets via a php script but I am not able to add pictures to each data objects galleries.

Is there something like that “$object->addPictureToObjectGallery(…);” available ?

Hi

first you need to create Hotspotimage object and add it to the list

   $items = [];
   $hotspot = new DataObject\Data\Hotspotimage();
   $hotspot->setImage($img);
   $items[] = $hotspot;

then you setItems to the ImageGallery

 $gallery = $object->getYourGallery();
 $gallery->setItems($items);
1 Like

Thank you Krzysztof

It works fine but I have a problem if I want to add a single photo to an existing gallery : it erases the gallery to replace it with the last loaded photo.

I’m not sure how to fix this …

Hi

try sth like this:

$gallery = $object->getYourGallery();
$currentItems = $gallery->getItems();
array_push($currentItems, $hotspot);
$gallery->setItems($currentItems);

Thanks again Krzysztof