From 2cf312964a6a52e410641bc6587571c24d99193b Mon Sep 17 00:00:00 2001 From: niph Date: Sun, 24 Mar 2024 14:30:58 +0100 Subject: [PATCH] Add callout text and background color to template --- .../CssClassBuilderInterface.php | 2 +- src/CssBuilder/CssStyleBuilder.php | 21 +++++++++++++++++++ src/CssBuilder/CssStyleBuilderInterface.php | 10 +++++++++ .../SemanticUiCssClassBuilder.php | 2 +- src/Resources/config/services.xml | 1 + .../config/services/css_class_builder.xml | 2 +- .../config/services/css_style_builder.xml | 12 +++++++++++ src/Resources/config/services/twig.xml | 1 + .../Shop/Product/Callout/_callout.html.twig | 2 +- src/Twig/CalloutExtension.php | 1 + src/Twig/CalloutRuntime.php | 9 +++++++- tests/Twig/CalloutRuntimeTest.php | 2 +- 12 files changed, 59 insertions(+), 6 deletions(-) rename src/{CssClassBuilder => CssBuilder}/CssClassBuilderInterface.php (77%) create mode 100644 src/CssBuilder/CssStyleBuilder.php create mode 100644 src/CssBuilder/CssStyleBuilderInterface.php rename src/{CssClassBuilder => CssBuilder}/SemanticUiCssClassBuilder.php (88%) create mode 100644 src/Resources/config/services/css_style_builder.xml diff --git a/src/CssClassBuilder/CssClassBuilderInterface.php b/src/CssBuilder/CssClassBuilderInterface.php similarity index 77% rename from src/CssClassBuilder/CssClassBuilderInterface.php rename to src/CssBuilder/CssClassBuilderInterface.php index da79f8a..af0caec 100644 --- a/src/CssClassBuilder/CssClassBuilderInterface.php +++ b/src/CssBuilder/CssClassBuilderInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Setono\SyliusCalloutPlugin\CssClassBuilder; +namespace Setono\SyliusCalloutPlugin\CssBuilder; use Setono\SyliusCalloutPlugin\Model\CalloutInterface; diff --git a/src/CssBuilder/CssStyleBuilder.php b/src/CssBuilder/CssStyleBuilder.php new file mode 100644 index 0000000..57123ab --- /dev/null +++ b/src/CssBuilder/CssStyleBuilder.php @@ -0,0 +1,21 @@ +getColor()) { + $style .= sprintf(' color: %s;', $callout->getColor()); + } + if ($callout->getBackgroundColor()) { + $style .= sprintf(' background-color: %s;', $callout->getBackgroundColor()); + } + + return $style; + } +} diff --git a/src/CssBuilder/CssStyleBuilderInterface.php b/src/CssBuilder/CssStyleBuilderInterface.php new file mode 100644 index 0000000..fe9592e --- /dev/null +++ b/src/CssBuilder/CssStyleBuilderInterface.php @@ -0,0 +1,10 @@ + + diff --git a/src/Resources/config/services/css_class_builder.xml b/src/Resources/config/services/css_class_builder.xml index 5fa4b5d..e5a8103 100644 --- a/src/Resources/config/services/css_class_builder.xml +++ b/src/Resources/config/services/css_class_builder.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> + class="Setono\SyliusCalloutPlugin\CssBuilder\SemanticUiCssClassBuilder"> + + + + + + + + diff --git a/src/Resources/config/services/twig.xml b/src/Resources/config/services/twig.xml index 3b54270..1293dca 100644 --- a/src/Resources/config/services/twig.xml +++ b/src/Resources/config/services/twig.xml @@ -17,6 +17,7 @@ + diff --git a/src/Resources/views/Shop/Product/Callout/_callout.html.twig b/src/Resources/views/Shop/Product/Callout/_callout.html.twig index 0e00879..ec3d825 100755 --- a/src/Resources/views/Shop/Product/Callout/_callout.html.twig +++ b/src/Resources/views/Shop/Product/Callout/_callout.html.twig @@ -1,2 +1,2 @@ {# @var \Setono\SyliusCalloutPlugin\Model\CalloutInterface callout #} -
{{ render_callout(callout) }}
+
{{ render_callout(callout) }}
diff --git a/src/Twig/CalloutExtension.php b/src/Twig/CalloutExtension.php index 14e9511..f56f71d 100644 --- a/src/Twig/CalloutExtension.php +++ b/src/Twig/CalloutExtension.php @@ -20,6 +20,7 @@ public function getFunctions(): array new TwigFunction('get_callout_assignment_delay', $this->getDelay(...)), new TwigFunction('render_callout_class_attribute', [CalloutRuntime::class, 'renderCalloutClassAttribute']), new TwigFunction('render_callout', [CalloutRuntime::class, 'renderCallout'], ['is_safe' => ['html']]), + new TwigFunction('render_callout_style', [CalloutRuntime::class, 'renderCalloutStyle']), ]; } diff --git a/src/Twig/CalloutRuntime.php b/src/Twig/CalloutRuntime.php index dc03002..19b6c97 100644 --- a/src/Twig/CalloutRuntime.php +++ b/src/Twig/CalloutRuntime.php @@ -6,7 +6,8 @@ use Setono\SyliusCalloutPlugin\Checker\Eligibility\CalloutEligibilityCheckerInterface; use Setono\SyliusCalloutPlugin\Checker\RenderingEligibility\CalloutRenderingEligibilityCheckerInterface; -use Setono\SyliusCalloutPlugin\CssClassBuilder\CssClassBuilderInterface; +use Setono\SyliusCalloutPlugin\CssBuilder\CssClassBuilderInterface; +use Setono\SyliusCalloutPlugin\CssBuilder\CssStyleBuilderInterface; use Setono\SyliusCalloutPlugin\Model\CalloutInterface; use Setono\SyliusCalloutPlugin\Model\ProductInterface; use Setono\SyliusCalloutPlugin\Provider\RenderingCalloutProviderInterface; @@ -21,6 +22,7 @@ public function __construct( private readonly CssClassBuilderInterface $cssClassBuilder, private readonly RenderingCalloutProviderInterface $renderingCalloutProvider, private readonly CalloutRendererInterface $calloutRenderer, + private readonly CssStyleBuilderInterface $cssStyleBuilder, ) { } @@ -76,4 +78,9 @@ public function renderCallout(CalloutInterface $callout): string { return (string) $this->calloutRenderer->render($callout); } + + public function renderCalloutStyle(CalloutInterface $callout): string + { + return $this->cssStyleBuilder->build($callout); + } } diff --git a/tests/Twig/CalloutRuntimeTest.php b/tests/Twig/CalloutRuntimeTest.php index ee38eb5..7740e77 100644 --- a/tests/Twig/CalloutRuntimeTest.php +++ b/tests/Twig/CalloutRuntimeTest.php @@ -9,7 +9,7 @@ use Prophecy\PhpUnit\ProphecyTrait; use Setono\SyliusCalloutPlugin\Checker\Eligibility\CalloutEligibilityCheckerInterface; use Setono\SyliusCalloutPlugin\Checker\RenderingEligibility\CalloutRenderingEligibilityCheckerInterface; -use Setono\SyliusCalloutPlugin\CssClassBuilder\CssClassBuilderInterface; +use Setono\SyliusCalloutPlugin\CssBuilder\CssClassBuilderInterface; use Setono\SyliusCalloutPlugin\Model\Callout; use Setono\SyliusCalloutPlugin\Model\CalloutInterface; use Setono\SyliusCalloutPlugin\Model\ProductInterface;