src/Twig/EntityKeywordExtension.php line 17

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Twig/EntityKeywordExtension.php
  4. //----------------------------------------------------------------------
  5. namespace App\Twig;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFilter;
  9. use App\Entity\Common\EntityKeyword;
  10. class EntityKeywordExtension extends AbstractExtension
  11. {
  12.     public function __construct(ManagerRegistry $doctrine)
  13.     {
  14.         $this->em $doctrine->getManager();
  15.     }
  16.     public function getFilters(): array
  17.     {
  18.         return [
  19.             new TwigFilter('entity_keyword', [$this'entityKeywordFilter']),
  20.         ];
  21.     }
  22.     public function entityKeywordFilter($text)
  23.     {
  24.         $keywordsRep $this->em->getRepository(EntityKeyword::class);
  25.         $keywords $keywordsRep->findBy(array(),array(
  26.             'value'        =>    'ASC'
  27.         ));
  28.         foreach ($keywords as $key)
  29.         {
  30.             $text str_replace($key->getValue(), '<span class="text-success">' $key->getValue() . '</span>'$text);
  31.             // Also replace \n with <br> for display purposes only
  32.             $text str_replace("\n"'<br>'$text);
  33.         }
  34.         return $text;
  35.     }
  36. }