Skip to content

Instantly share code, notes, and snippets.

@geldmacher
Last active February 16, 2023 16:37
Show Gist options
  • Save geldmacher/e3ffdd7cb938b2da036763e002482780 to your computer and use it in GitHub Desktop.
Save geldmacher/e3ffdd7cb938b2da036763e002482780 to your computer and use it in GitHub Desktop.
Custom TYPO3 condition for checking if a specific extension is loaded (with custom Symfony ExpressionLanguage provider)
<?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->getIsExtensionLoadedFunction(),
];
}
/**
* getIsExtensionLoadedFunction
*
* [isExtensionLoaded('extensionKey')]
* lib.foo = TEXT
* lib.foo.value = bar
* [END]
*
* @return ExpressionFunction
*/
protected function getIsExtensionLoadedFunction(): ExpressionFunction
{
return new ExpressionFunction('isExtensionLoaded', static function () {
// Not implemented, we only use the evaluator
}, static function ($existingVariables, $extensionKey) {
return !empty($extensionKey) && ExtensionManagementUtility::isLoaded($extensionKey);
});
}
}
<?php
// This file needs to be in the Configuration folder of your extension
return [
'typoscript' => [
Vendor\Extension\ExpressionLanguage\TyposcriptConditionProvider::class,
]
];
<?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