mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 13:03:50 +00:00
adding twig helper for routing and more
This commit is contained in:
62
Templating/ChillTwigHelper.php
Normal file
62
Templating/ChillTwigHelper.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/*
|
||||
*
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class ChillTwigHelper extends AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new TwigFilter('chill_print_or_message', [$this, 'printOrMessage'], [
|
||||
'needs_environment' => true,
|
||||
'is_safe' => ['html', 'html_attrs']
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Print `value` inside a template, or, if $value is empty,
|
||||
* print $message.
|
||||
*
|
||||
* The template can be customized. The template is a full path to another
|
||||
* template, or one of the key belows:
|
||||
*
|
||||
* - 'default' ;
|
||||
* - 'blockquote' ;
|
||||
*
|
||||
* @param Environment $twig
|
||||
* @param string $value
|
||||
* @param string $message
|
||||
* @param string $template
|
||||
* @return string
|
||||
*/
|
||||
public function printOrMessage(
|
||||
Environment $twig,
|
||||
$value,
|
||||
$message = 'No value',
|
||||
$template = 'default'
|
||||
) {
|
||||
switch ($template) {
|
||||
case 'default':
|
||||
case 'blockquote':
|
||||
$t = '@ChillMain/Extensions/PrintOrMessage/'.$template.'.html.twig';
|
||||
break;
|
||||
default:
|
||||
$t = $template;
|
||||
}
|
||||
|
||||
return $twig->render($t, [
|
||||
'value' => $value,
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
}
|
114
Templating/ChillTwigRoutingHelper.php
Normal file
114
Templating/ChillTwigRoutingHelper.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/*
|
||||
*/
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Bridge\Twig\Extension\RoutingExtension;
|
||||
|
||||
/**
|
||||
* Provides function to build path with returnPath.
|
||||
*
|
||||
* The logic of the function is based on the original routing extension.
|
||||
*
|
||||
*/
|
||||
class ChillTwigRoutingHelper extends AbstractExtension
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var RequestStack
|
||||
*/
|
||||
protected $requestStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var RoutingExtension
|
||||
*/
|
||||
protected $originalExtension;
|
||||
|
||||
public function __construct(
|
||||
RequestStack $requestStack,
|
||||
RoutingExtension $originalExtension
|
||||
) {
|
||||
$this->requestStack = $requestStack;
|
||||
$this->originalExtension = $originalExtension;
|
||||
}
|
||||
|
||||
|
||||
public function getFunctions()
|
||||
{
|
||||
return [
|
||||
new TwigFunction('chill_return_path_or', [$this, 'getReturnPathOr'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
||||
new TwigFunction('chill_path_add_return_path', [$this, 'getPathAddReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] ),
|
||||
new TwigFunction('chill_path_forward_return_path', [$this, 'getPathForwardReturnPath'], ['is_safe_callback' => [$this, 'isUrlGenerationSafe']] )
|
||||
];
|
||||
}
|
||||
|
||||
public function isUrlGenerationSafe(\Twig_Node $argsNode)
|
||||
{
|
||||
return $this->originalExtension->isUrlGenerationSafe($argsNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the return path if it exists, or generate the path if not.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $relative
|
||||
* @return string
|
||||
*/
|
||||
public function getReturnPathOr($name, $parameters = [], $relative = false)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if ($request->query->has('returnPath')) {
|
||||
return $request->query->get('returnPath');
|
||||
}
|
||||
|
||||
return $this->originalExtension->getPath($name, $parameters, $relative);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an url with a returnPath parameter to current page
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $relative
|
||||
* @return string
|
||||
*/
|
||||
public function getPathAddReturnPath($name, $parameters = [], $relative = false)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
$parameters['returnPath'] = $request->getRequestUri();
|
||||
|
||||
return $this->originalExtension->getPath($name, $parameters, $relative);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an url with a returnPath parameter to current page
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $parameters
|
||||
* @param bool $relative
|
||||
* @return string
|
||||
*/
|
||||
public function getPathForwardReturnPath($name, $parameters = [], $relative = false)
|
||||
{
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
|
||||
if ($request->query->has('returnPath')) {
|
||||
$parameters['returnPath'] = $request->query->get('returnPath');
|
||||
}
|
||||
|
||||
return $this->originalExtension
|
||||
->getPath(
|
||||
$name,
|
||||
$parameters,
|
||||
$relative
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user