(note: this includes #41 since it's not merged yet, but required for this to work)
This PR allows to add custom mappings to the ingested Elasticsearch documents, for example adding category images (which Coreshop supports, but VSF does not, AFAIK).
It works like this:
developer decorates the document mapper in their Pimcore services:
@ES\Document()
*/
class Category extends \CoreShop2VueStorefrontBundle\Document\Category
{
/**
@ES\Property(type="keyword", name="image")
*/
public $image;
public function setImage(?string $image): void
{
$this->image = $image;
}
}
4. add the custom mapper (configured in 1):
```php
namespace AppBundle\VueStorefront\DocumentMapper;
use AppBundle\VueStorefront\Document\Category;
use CoreShop2VueStorefrontBundle\Bridge\DocumentMapperInterface;
use Pimcore\Model\DataObject\ProductCategory;
class CategoryDocumentMapper implements DocumentMapperInterface
{
/**
(note: this includes #41
since it's not merged yet, but required for this to work)This PR allows to add custom mappings to the ingested Elasticsearch documents, for example adding category images (which Coreshop supports, but VSF does not, AFAIK).
It works like this:
AppBundle
to ingested bundles for ES:use ONGR\ElasticsearchBundle\Annotation as ES;
/**
@ES\Document() */ class Category extends \CoreShop2VueStorefrontBundle\Document\Category { /**
public function setImage(?string $image): void { $this->image = $image; } }
use AppBundle\VueStorefront\Document\Category; use CoreShop2VueStorefrontBundle\Bridge\DocumentMapperInterface; use Pimcore\Model\DataObject\ProductCategory;
class CategoryDocumentMapper implements DocumentMapperInterface { /**
@var DocumentMapperInterface */ private $categoryMapper;
public function __construct(DocumentMapperInterface $categoryMapper) { $this->categoryMapper = $categoryMapper; }
public function supports($object): bool { return $this->categoryMapper->supports($object); }
/**
@param ProductCategory $object */ public function mapToDocument($object, ?string $language = null): Category { /* @var Category $document / $document = $this->toChildObject($this->categoryMapper->mapToDocument($object, $language), Category::class);
}
private function toChildObject($object, $className) { return unserialize(sprintf( 'O:%d:"%s"%s', strlen($className), $className, strstr(strstr(serialize($object), '"'), ':') ), [$className]); } }