Skip to main content
Version: 2023.2

Technical Details

Migration of existing Data

To add metadata collections to assets based on existing metadata, use following command. It will check for existing metadata entries if they have a prefix like License.something and then adds the corresponding collection (in this case License) to the asset.

php bin/console asset-metadata-class-definitions:populate

Assign collections to an asset programmatically

To assign metadata collections to assets, use the Collections model as follows:

$col = new Pimcore\AssetMetadataClassDefinitionsBundle\Model\Collections();

// assign Asset ID
$col->setAssetId(290);
// or assign Asset directly
$asset = new \Pimcore\Model\Asset();
$asset->setKey('testImage.png');
$asset->setParentId(1);
$col->setAsset($asset);

$col->setCollections(["License", "Credits"]);

// either only apply collections to asset without saving it
$col->applyToAsset();

// or apply collection to asset and save the asset right away
$col->save();

// setting metadata attributes it self is the same as with standard metadata
$asset = Pimcore\Model\Asset::getById(290);
$asset->addMetadata('CarImages.title', 'input', $title, $language);
$asset->save();

Get collections from an asset programmatically

$asset = Pimcore\Model\Asset::getById(290); 
$collections = \Pimcore\AssetMetadataClassDefinitionsBundle\Model\Collections::getByAssetId($asset->getId());
$collectionNames = $collections->getCollections();

Add a definition programmatically

$configuration = new \Pimcore\AssetMetadataClassDefinitionsBundle\Model\Configuration();
$configuration->setName("License");
$configuration->setPrefix("License");
$configuration->setIcon("/bundles/pimcoreadmin/img/object-icons/02_red.svg");

$panel = new \Pimcore\AssetMetadataClassDefinitionsBundle\Model\ClassDefinition\Layout\Panel();
$panel->setTitle("panel title");


$input = new \Pimcore\AssetMetadataClassDefinitionsBundle\Model\ClassDefinition\Data\Input();
$input->setName("myinputfield");

$panel->addChild($input);

$configuration->setLayoutDefinitions($panel);
$configuration->save();