PHP code example of ntriga / pimcore-link-generator

1. Go to this page and download the library: Download ntriga/pimcore-link-generator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ntriga / pimcore-link-generator example snippets


/**
* @Route("{path}/n{objectId}/{objectSlug}", name="news-detail", defaults={"path"=""}, 

namespace Ntriga\FrontBundle\LinkGenerator;

use Ntriga\PimcoreLinkGenerator\NtrigaLinkGenerator;
use Pimcore\Model\DataObject\News;

class NewsLinkGenerator extends NtrigaLinkGenerator
{
    /*
     * Here you can set the name of the property that is used to store the parent document
     */
    protected function getDefaultDocumentPropertyName(): string
    {
        return 'news_document';
    }

    /*
     * Here you can set the class name of the object
     */
    protected function getObjectClassName(): string
    {
        return News::class;
    }
    
    /*
     * Here you can set the route name of the action that will be used to generate the link
     */
    protected function getRouteName(): string
    {
        return 'news-detail';
    }
    
    /*
     * Optional
     * Here you can set the name of the method that will be used to generate the slug
     * Default is "getName"
     * If you have a slug input field, this will be used instead (not a field of the type slug)
     */
    protected function getObjectDefaultSlugField(): string
    {
        return 'getTitle';
    }
}



namespace Ntriga\FrontBundle\Twig\Extension;

use Ntriga\FrontBundle\LinkGenerator\NewsLinkGenerator;
use Pimcore\Model\DataObject\News;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class NewsExtension extends AbstractExtension
{
    public function __construct(
        protected NewsLinkGenerator $newsLinkGenerator
    ) {}

    public function getFunctions()
    {
        return [
            new TwigFunction('app_news_detaillink', [$this, 'generateLink']),
        ];
    }

    public function generateLink(News $item): string
    {
        return $this->newsLinkGenerator->generate($item, []);
    }
}

// Single parameter
$link = $productLinkGenerator->generate($object, [
    'queryParams' => ['color' => 'red']
]);
// Result: /en/products/my-product?color=red

// Multiple parameters
$link = $productLinkGenerator->generate($object, [
    'queryParams' => [
        'color' => 'red',
        'size' => 'large',
        'category' => 'shirts'
    ]
]);
// Result: /en/products/my-product?color=red&size=large&category=shirts

// Combined with other parameters
$link = $productLinkGenerator->generate($object, [
    'document' => $document,
    '_locale' => 'en',
    'queryParams' => ['color' => 'red']
]);
// Result: /en/products/my-product?color=red