<?php
//----------------------------------------------------------------------
// src/Twig/EntityKeywordExtension.php
//----------------------------------------------------------------------
namespace App\Twig;
use Doctrine\Persistence\ManagerRegistry;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use App\Entity\Common\EntityKeyword;
class EntityKeywordExtension extends AbstractExtension
{
public function __construct(ManagerRegistry $doctrine)
{
$this->em = $doctrine->getManager();
}
public function getFilters(): array
{
return [
new TwigFilter('entity_keyword', [$this, 'entityKeywordFilter']),
];
}
public function entityKeywordFilter($text)
{
$keywordsRep = $this->em->getRepository(EntityKeyword::class);
$keywords = $keywordsRep->findBy(array(),array(
'value' => 'ASC'
));
foreach ($keywords as $key)
{
$text = str_replace($key->getValue(), '<span class="text-success">' . $key->getValue() . '</span>', $text);
// Also replace \n with <br> for display purposes only
$text = str_replace("\n", '<br>', $text);
}
return $text;
}
}