Link Generator | Pimcore Development Documentation (2024)

Link Generators are used to dynamically generate web-links for objects and are automatically called when objectsare linked in document link editables, link document types and object link tags.

Additionally they are also enabling the preview tab for data objects.

Link generators are defined on class level, there are two ways to do this.

Either simply specify the class name or the name of a Symfony service (notice the prefix).

<?php

namespace App\Website\LinkGenerator;

use App\Model\Product\AccessoryPart;
use App\Model\Product\Car;
use App\Website\Tool\Text;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\ProductInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\DataObject\ClassDefinition\LinkGeneratorInterface;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;

class ProductLinkGenerator extends AbstractProductLinkGenerator implements LinkGeneratorInterface
{
public function generate(Concrete $object, array $params = []): string
{
if (!($object instanceof Car || $object instanceof AccessoryPart)) {
throw new \InvalidArgumentException('Given object is no Car');
}

return $this->doGenerate($object, $params);
}

public function generateWithMockup(ProductInterface $object, array $params = []): string
{
return $this->doGenerate($object, $params);
}

protected function doGenerate(ProductInterface $object, array $params): string
{
return DataObject\Service::useInheritedValues(true, function () use ($object, $params) {
return $this->pimcoreUrl->__invoke(
[
'productname' => Text::toUrl($object->getOSName() ? $object->getOSName() : 'product'),
'product' => $object->getId(),
'path' => $this->getNavigationPath($object->getMainCategory(), $params['rootCategory'] ?? null),
'page' => null
],
'shop-detail',
true
);
});
}
}

Note: If you want to support mockups or arbitrary objects you can change the typehint to:

The link generator will receive the referenced object and additional data depending on the context.This would be the document (if embedded in a document), the object if embedded in an object including the tag or field definition as context.

public function generate(Concrete $object, array $params = []): string
{
if (isset($params['document']) && $params['document'] instanceof Document) {
// param contains context information
$documentPath = $params['document']->getFullPath();
}
...
}
Link Generator | Pimcore Development Documentation (2024)
Top Articles
Latest Posts
Article information

Author: Duncan Muller

Last Updated:

Views: 6582

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Duncan Muller

Birthday: 1997-01-13

Address: Apt. 505 914 Phillip Crossroad, O'Konborough, NV 62411

Phone: +8555305800947

Job: Construction Agent

Hobby: Shopping, Table tennis, Snowboarding, Rafting, Motor sports, Homebrewing, Taxidermy

Introduction: My name is Duncan Muller, I am a enchanting, good, gentle, modern, tasty, nice, elegant person who loves writing and wants to share my knowledge and understanding with you.