diff --git a/composer.json b/composer.json
index d6497cc..bf391d2 100644
--- a/composer.json
+++ b/composer.json
@@ -1,43 +1,44 @@
{
- "name": "serge0design/pimcore-custom-twig",
- "license": "GPL-3.0-or-later",
- "type": "pimcore-bundle",
- "keywords": [
- "pimcore",
- "twig",
- "Filter",
- "Extension",
- "Test"
- ],
- "description": "A collection of twig Extensions/Filters and Tests for Pimcore 11.",
- "homepage": "https://github.com/serge0design/pimcore-custom-twig.git",
- "authors": [
- {
- "name": "serge.design - Serge",
- "email": "github@serge.design",
- "homepage": "https://serge-design.ch",
- "role": "Developer"
- }
- ],
- "autoload": {
- "psr-4": {
- "SergeDesign\\PimcoreCustomTwigBundle\\": "src/"
- }
- },
- "extra": {
- "pimcore": {
- "bundles": [
- "SergeDesign\\PimcoreCustomTwigBundle\\PimcoreCustomTwigBundle"
- ]
- }
- },
- "require": {
- "ext-intl": "*",
- "ext-mbstring": "*",
- "pimcore/pimcore": "^v11.1.0",
- "symfony/ux-twig-component": "^2.7",
- "twig/extra-bundle": "^3.5",
- "twig/string-extra": "^3.5"
- },
- "require-dev": {}
+ "name": "serge0design/pimcore-custom-twig",
+ "license": "GPL-3.0-or-later",
+ "type": "pimcore-bundle",
+ "keywords": [
+ "pimcore",
+ "twig",
+ "Filter",
+ "Extension",
+ "Test"
+ ],
+ "description": "A collection of twig Extensions/Filters and Tests for Pimcore 11.",
+ "homepage": "https://github.com/serge0design/pimcore-custom-twig.git",
+ "authors": [
+ {
+ "name": "serge.design - Serge",
+ "email": "github@serge.design",
+ "homepage": "https://serge-design.ch",
+ "role": "Developer"
+ }
+ ],
+ "autoload": {
+ "psr-4": {
+ "SergeDesign\\PimcoreCustomTwigBundle\\": "src/"
+ }
+ },
+ "extra": {
+ "pimcore": {
+ "bundles": [
+ "SergeDesign\\PimcoreCustomTwigBundle\\PimcoreCustomTwigBundle"
+ ]
+ }
+ },
+ "require": {
+ "ext-iconv": "*",
+ "ext-intl": "*",
+ "ext-mbstring": "*",
+ "pimcore/pimcore": "^v11.1.0",
+ "symfony/ux-twig-component": "^2.7",
+ "twig/extra-bundle": "^3.5",
+ "twig/string-extra": "^3.5"
+ },
+ "require-dev": {}
}
diff --git a/config/twig/extensions.yml b/config/twig/extensions.yml
index 96b000d..4b985cd 100644
--- a/config/twig/extensions.yml
+++ b/config/twig/extensions.yml
@@ -1,21 +1,23 @@
services:
- _defaults:
- autowire: true
- autoconfigure: true
- public: false
+ _defaults:
+ autowire: true
+ autoconfigure: true
+ public: false
- SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigBootstrapSvgIcon:
- tags: [ 'twig.extension' ]
+ SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigBootstrapSvgIcon:
+ tags:
+ - { name: twig.extension }
- SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigEditmodeLinkToObject:
- tags: [ 'twig.extension' ]
+ SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigEditmodeLinkToObject:
+ tags:
+ - { name: twig.extension }
- SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigLanguageSwitcher:
- tags: [ 'twig.extension' ]
-
- SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigQrCode:
- tags: [ 'twig.extension' ]
-
- SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigSvgFun:
- tags: [ 'twig.extension' ]
+ SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigLanguageSwitcher:
+ tags:
+ - { name: twig.extension }
+ SergeDesign\PimcoreCustomTwigBundle\Twig\Extension\TwigQrCode:
+ arguments:
+ $translator: '@Pimcore\Translation\Translator'
+ tags:
+ - { name: twig.extension }
diff --git a/config/twig/testing.yml b/config/twig/testing.yml
index a1ebf3b..b6e4ee9 100644
--- a/config/twig/testing.yml
+++ b/config/twig/testing.yml
@@ -1,14 +1,16 @@
services:
- _defaults:
- autowire: true
- autoconfigure: true
- public: false
+ _defaults:
+ autowire: true
+ autoconfigure: true
+ public: false
- SergeDesign\PimcoreCustomTwigBundle\Twig\Test\TwigTests:
- tags: [ 'twig.extension' ]
+ SergeDesign\PimcoreCustomTwigBundle\Twig\Test\TwigTests:
+ tags:
+ - { name: twig.extension }
- SergeDesign\PimcoreCustomTwigBundle\Twig\Test\TwigBundleChecker:
- tags: [ 'twig.extension' ]
- arguments:
- - '@service_container'
+ SergeDesign\PimcoreCustomTwigBundle\Twig\Test\TwigBundleChecker:
+ tags:
+ - { name: twig.extension }
+ arguments:
+ $bundles: '%kernel.bundles%'
diff --git a/docs/twig-filters.md b/docs/twig-filters.md
index 79e9017..377124b 100644
--- a/docs/twig-filters.md
+++ b/docs/twig-filters.md
@@ -1,6 +1,12 @@
## Twig Filter Examples
-twigFilterArrayFlip:
+test array
+
+```
+{% set array = ['apple', 'banana', 'cherry'] %}
+```
+
+twigFilterArrayFlip:
Exchanges all keys with their associated values in an array
```
@@ -9,7 +15,7 @@ Exchanges all keys with their associated values in an array
{% endfor %}
```
-twigFilterArrayReverse:
+twigFilterArrayReverse:
Return an array with elements in reverse order
```
@@ -18,7 +24,7 @@ Return an array with elements in reverse order
{% endfor %}
```
-twigFilterArrayShuffle
+twigFilterArrayShuffle
Return an array with elements in shuffled order
```
@@ -27,8 +33,8 @@ Return an array with elements in shuffled order
{% endfor %}
```
-twigFilterPassedTimeToNow
-usable for logins, orders, blogs, etc
+twigFilterPassedTimeToNow
+usable for logins, orders, blogs, etc.
Outputs something like: Last Loggin ( 1day ago )
```
@@ -42,6 +48,16 @@ twigFilterFileGetContents
{{ svgImagePath|twigFilterFileGetContents }}
```
+Formatting currency output
+
+```
+ {% set productPrice = 1234.56 %} {# Example price #}
+ {% set currencySymbol = 'CHF' %} {# Currency code #}
+ {% set locale = 'de_CH' %} {# Locale for formatting #}
+ {# Using the twigFilterFormatPrice filter to format the price #}
+ {{ productPrice|twigFilterFormatPrice(currencySymbol, 2, locale) }}
+```
+
#### Image
twigFilterImgThumbnail
@@ -63,6 +79,20 @@ Output inline Code: style="background-image: url('..');
{{ pimcoreImage|twigFilterCssBgImg('thumbnailName') }}
```
+Usage:
+
+```
+{% set pimcoreImage = pimcore_image("pimcoreImage") %}
+{% if editmode %}
+ {{ pimcoreImage|raw }}
+{% else %}
+ {% set pimcoreImage = pimcoreImage.image %}
+ {{ pimcoreImage|twigFilterImgThumbnail('thumbnailName', 'cssClass', 'altText', {"data-attr": "value"}) }}
+ {{ pimcoreImage|twigFilterImgThumbConfig('cssClass', 'altText', 100, 100, 100, 'png') }}
+ {{ pimcoreImage|twigFilterCssBgImg('contentimages') }}
+{% endif %}
+```
+
#### String
twigFilterGetMd5
@@ -80,7 +110,15 @@ twigFilterGetUniqid
twigFilterStringNormalizer
```
-{{ "String"|twigFilterStringNormalizer }}
+{% set string = '~¨^?\'"/-+.,;() &äöüÄÖÜßÉéÈèÊêEeËëÀàÁáÅåaÂâÃãªÆæCcÇçCcÍíÌìÎîÏïÓóÒòÔôºÕõŒOoØøÚúÙùÛûUuUuŠšSsŽžÑñ¡¿Ÿÿ_:' %}
+{{ string|twigFilterStringNormalizer }}
+```
+
+twigFilterNormalizeFolderName
+
+```
+{% set string = '~¨^?\'"/-+.,;() &äöüÄÖÜßÉéÈèÊêEeËëÀàÁáÅåaÂâÃãªÆæCcÇçCcÍíÌìÎîÏïÓóÒòÔôºÕõŒOoØøÚúÙùÛûUuUuŠšSsŽžÑñ¡¿Ÿÿ_:' %}
+{{ string|twigFilterNormalizeFolderName }}
```
twigFilterStrToLower
@@ -89,6 +127,18 @@ twigFilterStrToLower
{{ "String"|twigFilterStrToLower }}
```
+twigFilterStrToUpper
+
+```
+ {{ "string"|twigFilterStrToUpper }}
+```
+
+twigFilterStrCapitalize
+
+```
+ {{ "string"|twigFilterStrCapitalize }}
+```
+
twigFilterTruncate
```
@@ -106,7 +156,7 @@ twigFilterWordwrap
twigFilterHrefUrl
```
-{{ "https://url.com/"|twigFilterHrefUrl }}
+ {{ "https://url.com/"|twigFilterHrefUrl('css_class', "_blank") }}
```
twigFilterHrefEmail
@@ -127,11 +177,13 @@ twigFilterHrefWhatsApp
{{ href|twigFilterHrefWhatsApp }}
```
-twigFilterHrefSocialMedia
-based on Bootstrap Icons: https://icons.getbootstrap.com/
+twigFilterHrefSocialMedia
+based on Bootstrap Icons: https://icons.getbootstrap.com/
+r.i.p. twitter
```
-{{ href|twigFilterHrefSocialMedia }}
+{% set social = "https://twitter.com" %}
+{{ social|twigFilterHrefSocialMedia('twitter') }}
```
#### Time
diff --git a/docs/twig-tests.md b/docs/twig-tests.md
index e21c940..91bfb5d 100644
--- a/docs/twig-tests.md
+++ b/docs/twig-tests.md
@@ -10,7 +10,7 @@ Check if item is of MIME-Type image/svg+xml
```
twigTestIsArray\
-Check if item is array
+Check if item is an array
```
{% if item is twigTestIsArray %}
@@ -19,7 +19,7 @@ Check if item is array
```
twigTestIsBoolean\
-Check if item is bool
+Check if item is of bool
```
{% if item is twigTestIsBoolean %}
@@ -46,7 +46,7 @@ Check if item is countable
```
twigTestIsDir\
-Check if item is directory
+Check if item is a directory
```
{% if item is twigTestIsDir %}
@@ -55,7 +55,7 @@ Check if item is directory
```
twigTestIsFloat\
-Check if item is float
+Check if item is of float
```
{% if item is twigTestIsFloat %}
@@ -64,7 +64,7 @@ Check if item is float
```
twigTestIsInt\
-Check if is int
+Check if is of int
```
{% if item is twigTestIsInt %}
@@ -73,7 +73,7 @@ Check if is int
```
twigTestIsNumeric\
-Check if is numeric
+Check if is of numeric
```
{% if item is twigTestIsNumeric %}
@@ -82,7 +82,7 @@ Check if is numeric
```
twigTestIsNull\
-Check if item is null
+Check if item is of null
```
{% if item is twigTestIsNull %}
@@ -91,7 +91,7 @@ Check if item is null
```
twigTestIsObject\
-Check if item is object
+Check if item is an object
```
{% if item is twigTestIsObject %}
@@ -100,7 +100,7 @@ Check if item is object
```
twigTestIsResource\
-Check if item is resource
+Check if item is a resource
```
{% if item is twigTestIsResource %}
@@ -118,7 +118,7 @@ Check if item is scalar
```
twigTestIsString\
-Check if item is string
+Check if item is a string
```
{% if item is twigTestIsString %}
diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php
index ba9c1ac..d1cbb7e 100644
--- a/src/DependencyInjection/Configuration.php
+++ b/src/DependencyInjection/Configuration.php
@@ -1,4 +1,5 @@
processConfiguration($configuration, $configs);
- $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../../config'));
+ $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');
}
}
diff --git a/src/Nodes/SwitchNode.php b/src/Nodes/SwitchNode.php
index fe99b1d..46772f7 100644
--- a/src/Nodes/SwitchNode.php
+++ b/src/Nodes/SwitchNode.php
@@ -9,7 +9,7 @@
class SwitchNode extends Node
{
- public function compile(Compiler $compiler)
+ final public function compile(Compiler $compiler): void
{
$compiler
->addDebugInfo($this)
diff --git a/src/PimcoreCustomTwigBundle.php b/src/PimcoreCustomTwigBundle.php
index ec373b6..f1fae31 100644
--- a/src/PimcoreCustomTwigBundle.php
+++ b/src/PimcoreCustomTwigBundle.php
@@ -1,5 +1,4 @@
getLine();
$parser = $this->parser;
@@ -44,9 +45,9 @@ public function parse(Token $token): SwitchNode
$expressionParser = $parser->getExpressionParser();
$cases = [];
- $end = false;
+ $end_switch = false;
- while (!$end) {
+ while (!$end_switch) {
$next = $stream->next();
switch ($next->getValue()) {
@@ -76,14 +77,15 @@ public function parse(Token $token): SwitchNode
break;
case 'endswitch':
- $end = true;
+ $end_switch = true;
break;
default:
throw new SyntaxError(sprintf(
'Unexpected end of template.
Twig was looking for the following tags "case", "default", or "endswitch"
to close the "switch" block started at line %d)',
- $lineno), -1);
+ $lineno
+ ), -1);
}
}
@@ -97,7 +99,7 @@ public function parse(Token $token): SwitchNode
* @param Token $token
* @return bool
*/
- public function decideIfFork(Token $token): bool
+ final public function decideIfFork(Token $token): bool
{
return $token->test(['case', 'default', 'endswitch']);
}
@@ -106,7 +108,7 @@ public function decideIfFork(Token $token): bool
* @param Token $token
* @return bool
*/
- public function decideIfEnd(Token $token): bool
+ final public function decideIfEnd(Token $token): bool
{
return $token->test(['endswitch']);
}
diff --git a/src/Twig/Extension/TwigBootstrapSvgIcon.php b/src/Twig/Extension/TwigBootstrapSvgIcon.php
index 2bedfa5..44c6ab4 100644
--- a/src/Twig/Extension/TwigBootstrapSvgIcon.php
+++ b/src/Twig/Extension/TwigBootstrapSvgIcon.php
@@ -9,38 +9,46 @@
class TwigBootstrapSvgIcon extends AbstractExtension
{
+ private Filesystem $filesystem;
+ private string $basePath;
- public function getFunctions(): array
+ public function __construct(string $basePath = 'bundles/pimcorecustomtwig/svg/')
+ {
+ $this->filesystem = new Filesystem();
+ $this->basePath = $basePath;
+ }
+
+ final public function getFunctions(): array
{
return [
new TwigFunction(
'twigExtensionBootstrapSvgIcon',
- [$this, 'getBootstrapSvgIcon'], ['is_safe' => ['html']])
+ [$this, 'getBootstrapSvgIcon'],
+ ['is_safe' => ['html']]
+ )
];
}
- public function getBootstrapSvgIcon(
+ final public function getBootstrapSvgIcon(
string $biName = 'bootstrap',
string $fill = 'currentColor',
float $biSize = 1.6
- ): string
- {
+ ): string {
+ $iconFile = $this->basePath . 'bootstrap-icons.svg';
- $filesystem = new Filesystem();
- $iconFile = 'build/svg/bootstrap-icons.svg';
-
- if (!$filesystem->exists($iconFile)) {
- $iconFile = 'bundles/pimcorecustomtwig/svg/bootstrap-icons.svg';
+ if (!$this->filesystem->exists($iconFile)) {
+ // Optionally handle the error or fallback
+ return ''; // Early return on file not found
}
- $svgFile = [];
- $svgFile[] .= '';
-
- return join($svgFile);
+ return <<
+
+
+ HTML;
}
-
}
diff --git a/src/Twig/Extension/TwigEditmodeLinkToObject.php b/src/Twig/Extension/TwigEditmodeLinkToObject.php
index 784af75..68d7697 100644
--- a/src/Twig/Extension/TwigEditmodeLinkToObject.php
+++ b/src/Twig/Extension/TwigEditmodeLinkToObject.php
@@ -5,33 +5,49 @@
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
+use Pimcore\Http\Request\Resolver\EditmodeResolver;
+use Symfony\Component\HttpFoundation\RequestStack;
class TwigEditmodeLinkToObject extends AbstractExtension
{
+ private string $iconPath = '/bundles/pimcoreadmin/img/material-icons/outline-edit-24px.svg';
+ private EditmodeResolver $editmodeResolver;
+ private RequestStack $requestStack;
- /**
- * @inheritDoc
- */
- public function getFunctions(): array
+ public function __construct(EditmodeResolver $editmodeResolver, RequestStack $requestStack)
+ {
+ $this->editmodeResolver = $editmodeResolver;
+ $this->requestStack = $requestStack;
+ }
+
+ final public function getFunctions(): array
{
return [
- new TwigFunction('twigExtensionEditmodeLinkToObject',
- [$this, 'getLinkToObject'], ['is_safe' => ['html']])
+ new TwigFunction(
+ 'twigExtensionEditmodeLinkToObject',
+ [$this, 'getLinkToObject'],
+ ['is_safe' => ['html']]
+ )
];
}
- public function getLinkToObject(int $objectId): string
+ final public function getLinkToObject(int $objectId): string
{
+ $request = $this->requestStack->getCurrentRequest();
+ if (!$request || !$this->editmodeResolver->isEditmode($request)) {
+ // Return an empty string if not in edit mode or if request is null
+ return '';
+ }
- $linkToObject = [];
- $linkToObject[] .= '