timber / starter-theme

The "_s" for Timber: a dead-simple theme that you can build anything from
MIT License
817 stars 276 forks source link

functions.php $twig->addFilter error when installed Timber via Composer #61

Closed robertguss closed 9 months ago

robertguss commented 6 years ago

I typically install the Timber plugin via the WP admin, so maybe I am missing something, however, when I install Timber plugin via Composer and use this starter theme, I am getting the following error.

Fatal error: Uncaught TypeError: Argument 1 passed to Twig_Environment::addFilter() must be an instance of Twig_Filter, string given, called in /app/wp-content/themes/timber-starter-theme/functions.php on line 58 and defined in /app/wp-content/themes/timber-starter-theme/vendor/twig/twig/lib/Twig/Environment.php on line 748 TypeError: Argument 1 passed to Twig_Environment::addFilter() must be an instance of Twig_Filter, string given, called in /app/wp-content/themes/timber-starter-theme/functions.php on line 58 in /app/wp-content/themes/timber-starter-theme/vendor/twig/twig/lib/Twig/Environment.php on line 748 Call Stack: 0.0010 362376 1. {main}() /app/index.php:0 0.0011 362664 2. require('/app/wp-blog-header.php') /app/index.php:17 0.0505 2933488 3. require_once('/app/wp-includes/template-loader.php') /app/wp-blog-header.php:19 0.0559 2976504 4. include('/app/wp-content/themes/timber-starter-theme/page.php') /app/wp-includes/template-loader.php:74 0.0653 3064960 5. Timber\Timber::render() /app/wp-content/themes/timber-starter-theme/page.php:27 0.0653 3064960 6. Timber\Timber::fetch() /app/wp-content/themes/timber-starter-theme/vendor/timber/timber/lib/Timber.php:389 0.0653 3064960 7. Timber\Timber::compile() /app/wp-content/themes/timber-starter-theme/vendor/timber/timber/lib/Timber.php:362 0.0709 3068768 8. Timber\Loader->render() /app/wp-content/themes/timber-starter-theme/vendor/timber/timber/lib/Timber.php:318 0.0709 3068768 9. Timber\Loader->get_twig() /app/wp-content/themes/timber-starter-theme/vendor/timber/timber/lib/Loader.php:66 0.0801 3154392 10. apply_filters() /app/wp-content/themes/timber-starter-theme/vendor/timber/timber/lib/Loader.php:173 0.0801 3154792 11. WP_Hook->apply_filters() /app/wp-includes/plugin.php:203 0.0801 3156296 12. Timber\Twig->add_timber_filters() /app/wp-includes/class-wp-hook.php:286 0.0833 3252016 13. apply_filters() /app/wp-content/themes/timber-starter-theme/vendor/timber/timber/lib/Twig.php:254 0.0833 3252416 14. WP_Hook->apply_filters() /app/wp-includes/plugin.php:203 0.0833 3253920 15. StarterSite->add_to_twig() /app/wp-includes/class-wp-hook.php:286 0.0838 3255472 16. Twig_Environment->addFilter() /app/wp-content/themes/timber-starter-theme/functions.php:58

This error is being thrown by the following line inside of starter theme's functions.php file on line # 58

       function add_to_twig( $twig ) {
        /* this is where you can add your own functions to twig */
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter('myfoo', new Twig_SimpleFilter('myfoo', array($this, 'myfoo')));
        return $twig;
    }

If I change the function to the following it goes away and works correctly:

      function add_to_twig( $twig ) {
        /* this is where you can add your own functions to twig */
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter( new Twig_SimpleFilter('myfoo', array($this, 'myfoo')) );
        return $twig;
    }

To be honest I am not sure if I am doing something wrong, or if this is a bug. If it is I will be more than happy to fix it and submit a PR.

stevie-c91 commented 6 years ago

Hi, any updates with this? Same issue when Timber is installed with Composer and activating the starter theme. Thanks.

ramusdev commented 6 years ago

Also installed Timber through the composer and got this error, but plugin Timber work correctly

ramusdev commented 6 years ago

https://timber.github.io/docs/guides/functions/

marciojc commented 5 years ago

addFilter has been updated so you have to follow the new approach, see the code in functions.php