/** * @param ProductInterface $product * * @return object|\Sonata\MediaBundle\Model\MediaInterface */ protected function getGalleryForProduct(ProductInterface $product) { $galleryReference = sprintf("gallery_%s", $product->getSku()); if ($this->hasReference($galleryReference)) { return $this->getReference($galleryReference); } $gallery = $this->getGalleryManager()->create(); $gallery->setName($product->getSlug()); $gallery->setEnabled(true); $gallery->setDefaultFormat('product_catalog_preview'); $gallery->setContext('product_catalog'); $this->setReference($galleryReference, $gallery); $this->getGalleryManager()->update($gallery); $product->setGallery($gallery); return $gallery; }