PHP code example of think-digital-agency / contao-live-preview

1. Go to this page and download the library: Download think-digital-agency/contao-live-preview 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/ */

    

think-digital-agency / contao-live-preview example snippets


// src/Service/NewsPreviewUrlResolver.php
use Doctrine\DBAL\Connection;
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
use ThinkDigital\ContaoLivePreview\Service\PreviewUrlResolverInterface;

#[AsTaggedItem(priority: 0)] // höher = früher; optional
class NewsPreviewUrlResolver implements PreviewUrlResolverInterface
{
    public function __construct(private readonly Connection $db) {}

    public function resolve(string $table, int $id): ?array
    {
        if ('tl_news' !== $table) {
            return null; // nicht meine Tabelle → nächster Resolver
        }

        $pid = $this->db->fetchOne(
            'SELECT a.jumpTo FROM tl_news n JOIN tl_news_archive a ON a.id = n.pid WHERE n.id = ?',
            [$id],
        );

        return $pid ? ['pageId' => (int) $pid, 'alias' => ''] : null;
    }

    // Nur der bundle-eigene Resolver braucht das — hier null genügt.
    public function resolveRootPage(): ?array
    {
        return null;
    }
}