<?php
namespace App\EventListener;
use Pimcore\AssetMetadataClassDefinitionsBundle\Model\Collections;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Model\Asset;
/**
* Class AssetMetadataListener
* @package App\EventListener
*/
class AssetMetadataListener
{
/**
* @param ElementEventInterface $event
* @throws \Exception
*/
public function onPostUpdate(ElementEventInterface $event)
{
if (!$event instanceof AssetEvent) {
return;
}
$asset = $event->getAsset();
$this->addMetadataCollection($asset->getId());
$this->populateDataToCollection($asset);
}
/**
* @param $assetId
*/
protected function addMetadataCollection($assetId)
{
$col = new Collections();
$col->setAssetId($assetId);
$col->setCollections(['CarImages', 'License']);
$col->applyToAsset();
}
/**
* @param Asset $asset
* @throws \Exception
*/
protected function populateDataToCollection(Asset $asset)
{
$asset->addMetadata('License.name', 'input', 'OP v1.0.1');
$asset->addMetadata('License.type', 'select', 'Open Source');
$asset->update();
}
}