Last active
August 17, 2020 08:14
-
-
Save geldmacher/b6b1ec50ce74881adc5e8f17fdbac105 to your computer and use it in GitHub Desktop.
Custom TYPO3 condition for a configurable date period (with custom Symfony ExpressionLanguage provider)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Vendor\Extension\ExpressionLanguage\FunctionsProvider; | |
use Symfony\Component\ExpressionLanguage\ExpressionFunction; | |
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface; | |
/** | |
* ConditionFunctionsProvider | |
*/ | |
class ConditionFunctionsProvider implements ExpressionFunctionProviderInterface | |
{ | |
/** | |
* getFunctions | |
* | |
* @return ExpressionFunction[] | |
*/ | |
public function getFunctions(): array | |
{ | |
return [ | |
$this->getInDatePeriodFunction(), | |
]; | |
} | |
/** | |
* getInDatePeriodFunction | |
* | |
* tx_plugin.settings.vat = 19 | |
* [inDatePeriod('2020-07-01', '2021-01-01')] | |
* tx_plugin.settings.vat = 16 | |
* [END] | |
* | |
* @return ExpressionFunction | |
*/ | |
protected function getInDatePeriodFunction(): ExpressionFunction | |
{ | |
return new ExpressionFunction('inDatePeriod', static function () { | |
// Not implemented, we only use the evaluator | |
}, static function ($existingVariables, $from, $to) { | |
$result = false; | |
if (!empty($from) && !empty($to)) { | |
$now = new \DateTime('now'); | |
$from = new \DateTime($from); | |
$to = new \DateTime($to); | |
if($now > $from->modify('midnight') && $now < $to->modify('midnight')){ | |
$result = true; | |
} | |
} | |
return $result; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This file needs to be in the Configuration folder of your extension | |
return [ | |
'typoscript' => [ | |
Vendor\Extension\ExpressionLanguage\TyposcriptConditionProvider::class, | |
] | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Vendor\Extension\ExpressionLanguage; | |
use Vendor\Extension\ExpressionLanguage\FunctionsProvider\ConditionFunctionsProvider; | |
use TYPO3\CMS\Core\ExpressionLanguage\AbstractProvider; | |
/** | |
* TyposcriptConditionProvider | |
*/ | |
class TyposcriptConditionProvider extends AbstractProvider | |
{ | |
public function __construct() | |
{ | |
$this->expressionLanguageProviders = [ | |
ConditionFunctionsProvider::class, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment