src/Twig/SafeContentLoadExtension.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use Sulu\Bundle\WebsiteBundle\Twig\Content\ContentTwigExtensionInterface;
  4. use Twig\Extension\AbstractExtension;
  5. use Twig\TwigFunction;
  6. class SafeContentLoadExtension extends AbstractExtension
  7. {
  8.     private ContentTwigExtensionInterface $contentTwig;
  9.     public function __construct(ContentTwigExtensionInterface $contentTwig)
  10.     {
  11.         $this->contentTwig $contentTwig;
  12.     }
  13.     public function getFunctions()
  14.     {
  15.         return [
  16.             new TwigFunction('safe_content_load', [$this'safeLoad']),
  17.         ];
  18.     }
  19.     /**
  20.      * Loads content like sulu_content_load but returns null instead of
  21.      * crashing when the live node is a templateless stub (stale search
  22.      * index entry whose document is not actually published).
  23.      */
  24.     public function safeLoad($uuid)
  25.     {
  26.         try {
  27.             return $this->contentTwig->load($uuid);
  28.         } catch (\Throwable $e) {
  29.             return null;
  30.         }
  31.     }
  32. }