* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ namespace PrestaShopBundle\Twig; use Currency; use Exception; use PrestaShop\PrestaShop\Adapter\Configuration; use PrestaShop\PrestaShop\Adapter\Currency\CurrencyDataProvider; use PrestaShop\PrestaShop\Adapter\LegacyContext; use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint; use Twig\Extension\AbstractExtension; use Twig\Extension\GlobalsInterface; use Twig\TwigFilter; use Twig\TwigFunction; /** * This class is used by Twig_Environment and provide layout methods callable from a twig template. */ class LayoutExtension extends AbstractExtension implements GlobalsInterface { /** @var LegacyContext */ private $context; /** @var string */ private $environment; /** @var Configuration */ private $configuration; /** @var CurrencyDataProvider */ private $currencyDataProvider; /** * Constructor. * * Keeps the Context to look inside language settings. * * @param LegacyContext $context * @param string $environment * @param Configuration $configuration * @param CurrencyDataProvider $currencyDataProvider */ public function __construct( LegacyContext $context, $environment, Configuration $configuration, CurrencyDataProvider $currencyDataProvider ) { $this->context = $context; $this->environment = $environment; $this->configuration = $configuration; $this->currencyDataProvider = $currencyDataProvider; } /** * Provides globals for Twig templates. * * @return array the base globals available in twig templates */ public function getGlobals(): array { /* * As this is a twig extension we need to be very resilient and prevent it from crashing * the environment, for example the command debug:twig should not fail because of this extension */ try { $defaultCurrency = $this->context->getEmployeeCurrency() ?: $this->currencyDataProvider->getDefaultCurrency(); } catch (\Exception $e) { $defaultCurrency = null; } try { $rootUrl = $this->context->getRootUrl(); } catch (\Exception $e) { $rootUrl = null; } return [ 'theme' => $this->context->getContext()->shop->theme, 'default_currency' => $defaultCurrency, 'default_currency_symbol' => $defaultCurrency instanceof Currency ? $defaultCurrency->getSymbol() : null, 'root_url' => $rootUrl, 'js_translatable' => [], 'rtl_suffix' => $this->context->getContext()->language->is_rtl ? '_rtl' : '', ]; } /** * Define available filters. * * @return array Twig_SimpleFilter */ public function getFilters() { return [ new TwigFilter('configuration', [$this, 'getConfiguration'], ['deprecated' => true]), ]; } /** * Returns a list of functions to add to the existing list. * * @return array An array of functions */ public function getFunctions() { return [ new TwigFunction('getLegacyLayout', [$this, 'getLegacyLayout']), new TwigFunction('getAdminLink', [$this, 'getAdminLink']), new TwigFunction('youtube_link', [$this, 'getYoutubeLink']), new TwigFunction('configuration', [$this, 'getConfiguration']), ]; } /** * Returns a legacy configuration key. * * @param string $key * @param mixed $default Default value is null * @param ShopConstraint $shopConstraint Default value is null * * @return mixed */ public function getConfiguration($key, $default = null, ShopConstraint $shopConstraint = null) { return $this->configuration->get($key, $default, $shopConstraint); } /** * Get admin legacy layout into old controller context. * * Parameters can be set manually into twig template or sent from controller * For details : check Resources/views/Admin/Layout.html.twig * * @param string $controllerName The legacy controller name * @param string $title The page title to override default one * @param array $headerToolbarBtn The header toolbar to override * @param string $displayType The legacy display type variable * @param bool $showContentHeader Can force header toolbar (buttons and title) to be hidden with false value * @param array|string $headerTabContent Tabs labels * @param bool $enableSidebar Allow to use right sidebar to display docs for instance * @param string $helpLink If specified, will be used instead of legacy one * @param string[] $jsRouterMetadata JS Router needed configuration settings: base_url and security token * @param string $metaTitle * @param bool $useRegularH1Structure allows complex