From 834b629f299b367076164e2ac4433a511806a2c0 Mon Sep 17 00:00:00 2001 From: Erik van der Bas Date: Thu, 26 Dec 2024 15:40:21 +0100 Subject: [PATCH] feat: enhance StarterSite class with improved twig function/filter integration and navigation menu support --- src/StarterSite.php | 75 +++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/src/StarterSite.php b/src/StarterSite.php index 13e544f4b..834eaad1f 100644 --- a/src/StarterSite.php +++ b/src/StarterSite.php @@ -1,4 +1,5 @@ _x( 'Main menu', 'Backend - menu name', 'timber-starter' ), + ] + ); + // Add default posts and comments RSS feed links to head. add_theme_support( 'automatic-feed-links' ); @@ -116,30 +127,54 @@ public function theme_supports() { /** * This would return 'foo bar!'. * - * @param string $text being 'foo', then returned 'foo bar!'. + * @param string $text being 'foo', then returned 'foo bar!' */ public function myfoo( $text ) { $text .= ' bar!'; + return $text; } /** * This is where you can add your own functions to twig. * - * @param \Twig\Environment $twig get extension. + * @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/filters + * @param array $filters an array of Twig filters. */ - public function add_to_twig( $twig ) { - $twig->addFilter( new \Twig\TwigFilter( 'myfoo', [ $this, 'myfoo' ] ) ); + public function add_filters_to_twig( $filters ) { + + $additional_filters = [ + 'myfoo' => [ + 'callable' => [ $this, 'myfoo' ], + ], + ]; + + return array_merge( $filters, $additional_filters ); + } + - return $twig; + /** + * This is where you can add your own functions to twig. + * + * @link https://timber.github.io/docs/v2/hooks/filters/#timber/twig/functions + * @param array $functions an array of existing Twig functions. + */ + public function add_functions_to_twig( $functions ) { + $additional_functions = [ + 'get_theme_mod' => [ + 'callable' => 'get_theme_mod', + ], + ]; + + return array_merge( $functions, $additional_functions ); } /** * Updates Twig environment options. * - * @link https://twig.symfony.com/doc/2.x/api.html#environment-options + * @see https://twig.symfony.com/doc/2.x/api.html#environment-options * - * @param array $options An array of environment options. + * @param array $options an array of environment options * * @return array */