Генератор ссылок
Краткое содержание
Генераторы ссылок используются для динамического генерации веб-связей для объектов и автоматически вызываются, когда объекты Связаны в редактируемых ссылках документа, типах документов ссылок и тегах ссылки объекта.
Кроме того, они также включают вкладку предварительного просмотра для объектов данных.
Генераторы ссылок определяются на уровне класса, есть два способа сделать это.
Либо просто укажите имя класса или имя службы Symfony (обратите внимание на префикс).

services:
# ---------------------------------------------------------
# Link Generators for DataObjects
# ---------------------------------------------------------
App\Website\LinkGenerator\CategoryLinkGenerator:
public: true
App\Website\LinkGenerator\ProductLinkGenerator:
public: true
...
Sample Link Generator Implementation
<?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\Bundle\EcommerceFrameworkBundle\Model\DefaultMockup;
class ProductLinkGenerator extends AbstractProductLinkGenerator implements LinkGeneratorInterface
{
public function generate(object $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:
public function generate(object $object, array $params = []): string
{
//...
}
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.
Example:
public function generate(Concrete $object, array $params = []): string
{
if (isset($params['document']) && $params['document'] instanceof Document) {
// param contains context information
$documentPath = $params['document']->getFullPath();
}
...
}
Example Document
{P1}
$d = Document\Link::getById(203);
echo($d->getHref());
would produce the following output
/en/shop/Products/Cars/Sports-Cars/Jaguar-E-Type~p9
Use in Views
path() / url()
<ul class="foo">
{% for car in carList %}
<li><a href="{{ path(car) }}">{{ car.getName() }}</a></li>
{% endfor %}
</ul>
pimcoreUrl
<ul class="foo">
{% for car in carList %}
<li><a href="{{ path('pimcore_element', {'element': car}) }}">{{ car.getName() }}</a></li>
{% endfor %}
</ul>
Вы можете предложить улучшение документации или задать вопрос в комментариях.
Если вам нужна полноценная консультация — вы можете заказать её на нашем сайте.