Skip to content

Commit

Permalink
Add setono_sylius_wishlist_has_wishlist twig function
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Nov 19, 2024
1 parent 17d50ba commit c95466a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Resources/config/services/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<service id="Setono\SyliusWishlistPlugin\Twig\Runtime">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
<argument type="service" id="doctrine"/>

<tag name="twig.runtime"/>
</service>
Expand Down
1 change: 1 addition & 0 deletions src/Twig/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function getFunctions(): array
{
return [
new TwigFunction('setono_sylius_wishlist_on_wishlist', [Runtime::class, 'onWishlist']),
new TwigFunction('setono_sylius_wishlist_has_wishlist', [Runtime::class, 'hasWishlist']),
];
}
}
18 changes: 16 additions & 2 deletions src/Twig/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@

namespace Setono\SyliusWishlistPlugin\Twig;

use Doctrine\Persistence\ManagerRegistry;
use Setono\Doctrine\ORMTrait;
use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Twig\Extension\RuntimeExtensionInterface;

final class Runtime implements RuntimeExtensionInterface
{
public function __construct(private readonly WishlistProviderInterface $wishlistProvider)
{
use ORMTrait;

public function __construct(
private readonly WishlistProviderInterface $wishlistProvider,
ManagerRegistry $managerRegistry,
) {
$this->managerRegistry = $managerRegistry;
}

public function onWishlist(ProductInterface|ProductVariantInterface $product): bool
Expand All @@ -27,4 +34,11 @@ public function onWishlist(ProductInterface|ProductVariantInterface $product): b

return false;
}

public function hasWishlist(): bool
{
$wishlist = $this->wishlistProvider->getWishlists()[0];

return $this->getManager($wishlist)->contains($wishlist);
}
}

0 comments on commit c95466a

Please sign in to comment.