Skip to content

Commit

Permalink
Add terms_link twig function to output terms links
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Feb 28, 2024
1 parent 190db68 commit a7502a8
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Action/ShowTermsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(

public function __invoke(string $slug): Response
{
$terms = $this->termsRepository->findOneByChannelAndSlug(
$terms = $this->termsRepository->findOneByChannelAndLocaleAndSlug(
$this->channelContext->getChannel(),
$this->localeContext->getLocaleCode(),
$slug,
Expand Down
25 changes: 24 additions & 1 deletion src/Repository/TermsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function findByClassAndChannelAndLocale(string $class, ChannelInterface $
return $objs;
}

public function findOneByChannelAndSlug(ChannelInterface $channel, string $locale, string $slug): ?TermsInterface
public function findOneByChannelAndLocaleAndSlug(ChannelInterface $channel, string $locale, string $slug): ?TermsInterface
{
$obj = $this->createQueryBuilder('o')
->addSelect('translation')
Expand All @@ -57,4 +57,27 @@ public function findOneByChannelAndSlug(ChannelInterface $channel, string $local

return $obj;
}

public function findOneByChannelAndLocaleAndCode(
ChannelInterface $channel,
string $locale,
string $code,
): ?TermsInterface {
$obj = $this->createQueryBuilder('o')
->addSelect('translation')
->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale')
->andWhere(':channel MEMBER OF o.channels')
->andWhere('o.code = :code')
->andWhere('o.enabled = true')
->setParameter('locale', $locale)
->setParameter('channel', $channel)
->setParameter('code', $code)
->getQuery()
->getOneOrNullResult()
;

Assert::nullOrIsInstanceOf($obj, TermsInterface::class);

return $obj;
}
}
4 changes: 3 additions & 1 deletion src/Repository/TermsRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ interface TermsRepositoryInterface extends RepositoryInterface
*/
public function findByClassAndChannelAndLocale(string $class, ChannelInterface $channel, string $locale): array;

public function findOneByChannelAndSlug(ChannelInterface $channel, string $locale, string $slug): ?TermsInterface;
public function findOneByChannelAndLocaleAndSlug(ChannelInterface $channel, string $locale, string $slug): ?TermsInterface;

public function findOneByChannelAndLocaleAndCode(ChannelInterface $channel, string $locale, string $code): ?TermsInterface;
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<import resource="services/form.xml"/>
<import resource="services/provider.xml"/>
<import resource="services/renderer.xml"/>
<import resource="services/twig.xml"/>
</imports>
</container>
18 changes: 18 additions & 0 deletions src/Resources/config/services/twig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_sylius_terms.twig.terms_extension" class="Setono\SyliusTermsPlugin\Twig\TermsExtension">
<tag name="twig.extension"/>
</service>

<service id="setono_sylius_terms.twig.terms_runtime" class="Setono\SyliusTermsPlugin\Twig\TermsRuntime">
<argument type="service" id="sylius.context.channel"/>
<argument type="service" id="sylius.context.locale"/>
<argument type="service" id="setono_sylius_terms.repository.terms"/>

<tag name="twig.runtime"/>
</service>
</services>
</container>
2 changes: 2 additions & 0 deletions src/Resources/views/shop/terms/link.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{# @var terms \Setono\SyliusTermsPlugin\Model\TermsInterface #}
<a href="{{ path('setono_sylius_terms_shop_show_terms', {'slug': terms.slug}) }}">{{ terms.name }}</a>
21 changes: 21 additions & 0 deletions src/Twig/TermsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusTermsPlugin\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class TermsExtension extends AbstractExtension
{
/**
* @return list<TwigFunction>
*/
public function getFunctions(): array
{
return [
new TwigFunction('terms_link', [TermsRuntime::class, 'link'], ['needs_environment' => true, 'is_safe' => ['html']]),
];
}
}
40 changes: 40 additions & 0 deletions src/Twig/TermsRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusTermsPlugin\Twig;

use Setono\SyliusTermsPlugin\Repository\TermsRepositoryInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;

final class TermsRuntime implements RuntimeExtensionInterface
{
public function __construct(
private readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext,
private readonly TermsRepositoryInterface $termsRepository,
) {
}

/**
* Returns a <a href="...">...</a> link to the terms if the given terms code exists
* else it returns an empty string
*/
public function link(Environment $env, string $code, string $template = null): string
{
$terms = $this->termsRepository->findOneByChannelAndLocaleAndCode(
$this->channelContext->getChannel(),
$this->localeContext->getLocaleCode(),
$code,
);

$template ??= '@SetonoSyliusTermsPlugin/shop/terms/link.html.twig';

return $env->render($template, [
'terms' => $terms,
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div class="four wide column">
<h4 class="ui inverted header">{{ 'sylius.ui.your_store'|trans }}</h4>
<div class="ui inverted link list">
<a href="#" class="item">{{ 'sylius.ui.about'|trans }}</a>
{{ terms_link('terms_and_conditions') }}
<a href="#" class="item">{{ 'sylius.ui.privacy_policy'|trans }}</a>
<a href="{{ path('sylius_shop_contact_request') }}" class="item">{{ 'sylius.ui.contact_us'|trans }}</a>
</div>
</div>

0 comments on commit a7502a8

Please sign in to comment.