<?php
namespace App\Twig;
use Sulu\Bundle\WebsiteBundle\Twig\Content\ContentTwigExtensionInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class SafeContentLoadExtension extends AbstractExtension
{
private ContentTwigExtensionInterface $contentTwig;
public function __construct(ContentTwigExtensionInterface $contentTwig)
{
$this->contentTwig = $contentTwig;
}
public function getFunctions()
{
return [
new TwigFunction('safe_content_load', [$this, 'safeLoad']),
];
}
/**
* Loads content like sulu_content_load but returns null instead of
* crashing when the live node is a templateless stub (stale search
* index entry whose document is not actually published).
*/
public function safeLoad($uuid)
{
try {
return $this->contentTwig->load($uuid);
} catch (\Throwable $e) {
return null;
}
}
}