Add function to twig renderer to get all snips of a tag
This commit is contained in:
parent
3a16e4609f
commit
d64cadb47b
@ -2,7 +2,10 @@
|
||||
|
||||
namespace App\Service\SnipParser\Twig;
|
||||
|
||||
use App\Dto\SnipFilterRequest;
|
||||
use App\Entity\Snip;
|
||||
use App\Repository\SnipRepository;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
@ -13,6 +16,7 @@ class SnipTwigExtension extends AbstractExtension
|
||||
public function __construct(
|
||||
#[Autowire(lazy: true)] private readonly RouterInterface $router,
|
||||
#[Autowire(lazy: true)] private readonly SnipRepository $snipRepo,
|
||||
#[Autowire(lazy: true)] private readonly Security $security,
|
||||
) {}
|
||||
|
||||
public function getFunctions(): array
|
||||
@ -22,6 +26,7 @@ class SnipTwigExtension extends AbstractExtension
|
||||
new TwigFunction('snipLink', $this->snipLink(...), [
|
||||
'is_safe' => ['html'],
|
||||
]),
|
||||
new TwigFunction('snipsByTag', $this->snipsByTag(...)),
|
||||
];
|
||||
}
|
||||
|
||||
@ -40,4 +45,14 @@ class SnipTwigExtension extends AbstractExtension
|
||||
}
|
||||
return sprintf('<a class="btn btn-sm btn-primary" href="%s">%s</a>', $this->snipPath($id), $snip);
|
||||
}
|
||||
|
||||
private function snipsByTag(string $tag): array
|
||||
{
|
||||
$request = new SnipFilterRequest(SnipFilterRequest::VISIBILITY_ALL, tag: $tag);
|
||||
$snips = $this->snipRepo->findByRequest($this->security->getUser(), $request);
|
||||
return array_map(fn(Snip $snip) => [
|
||||
'id' => $snip->getId(),
|
||||
'name' => $snip->getName(),
|
||||
], $snips);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user