Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twig 'filter' filter is broken after update to 3.1.31 #4437

Closed
bryantwells opened this issue Jun 23, 2019 · 10 comments
Closed

Twig 'filter' filter is broken after update to 3.1.31 #4437

bryantwells opened this issue Jun 23, 2019 · 10 comments

Comments

@bryantwells
Copy link

bryantwells commented Jun 23, 2019

Description

After updating to v3.1.31, twig's 'filter' filter is broken. I'm guessing this might be an issue with twig, but figured I would try here first.

Steps to reproduce

  1. Try:
{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|filter(v => v > 38) }}
  1. Receive TypeError:
Argument 1 passed to iterator_to_array() must implement interface Traversable, array given

Additional info

  • Craft version: 3.1.31
  • PHP version: 7.1.22
@brandonkelly
Copy link
Member

{{ sizes|filter(v => v > 38) }} isn’t valid since you can’t output an array/iterator. Does this work for you?

{% set sizes = [34, 36, 38, 40, 42] %}

<ul>
  {% for size in sizes|filter(v => v > 38) %}
    <li>{{ size }}</li>
  {% endear %}
</ul>

@bryantwells
Copy link
Author

bryantwells commented Jun 24, 2019

Thanks for the response @brandonkelly. Sorry, I misquoted the example from Twig's documentation :

{% set sizes = [34, 36, 38, 40, 42] %}
{{ sizes|filter(v => v > 38)|join(', ') }}

Regardless, both this example and the one you provided still return the same TypeError.

@brandonkelly
Copy link
Member

I just copy/pasted that exact same code into a Craft 3.1.31 template and it output 40, 42 like I’d expect. Maybe try running composer update ?

@bryantwells
Copy link
Author

bryantwells commented Jun 24, 2019

hmm — I'm on the latest version... Tried composer update. I also tried deleting the vendor folder / lockfile + reinstalling, as well as a separate fresh install. Getting the same error 🤔

@brandonkelly
Copy link
Member

Search through storage/logs/web.log for the error message you’re getting, and then post the stack trace that follows it.

@bryantwells
Copy link
Author

bryantwells commented Jun 24, 2019

2019-06-24 05:56:53 [-][-][45ba06919d62e9d9adc67fce6c976a6f][error][TypeError] TypeError: Argument 1 passed to iterator_to_array() must implement interface Traversable, array given in /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/twig/Extension.php:601

Stack trace:
#0 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/twig/Extension.php(601): iterator_to_array(Array)
#1 /Users/bryantwells/Sites/_sketchbook/53_craft-test/storage/runtime/compiled_templates/38/38e568af4f4e7c5a481f6c778ef0421167182d299cc41270f584f309f3d5bfa4.php(238): craft\web\twig\Extension->filterFilter(Array, Object(Closure))
#2 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/twig/twig/src/Template.php(407): __TwigTemplate_a3005cfcbd427cfb95972bcff824a7f1baa331090116d5be13d8c721c264b33b->doDisplay(Array, Array)
#3 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/twig/Template.php(52): Twig\Template->displayWithErrorHandling(Array, Array)
#4 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/twig/twig/src/Template.php(380): craft\web\twig\Template->displayWithErrorHandling(Array, Array)
#5 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/twig/Template.php(34): Twig\Template->display(Array, Array)
#6 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/twig/twig/src/Template.php(392): craft\web\twig\Template->display(Array)
#7 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/twig/twig/src/TemplateWrapper.php(45): Twig\Template->render(Array, Array)
#8 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/twig/twig/src/Environment.php(318): Twig\TemplateWrapper->render(Array)
#9 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/View.php(343): Twig\Environment->render('', Array)
#10 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/View.php(393): craft\web\View->renderTemplate('', Array)
#11 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/Controller.php(161): craft\web\View->renderPageTemplate('', Array)
#12 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/controllers/TemplatesController.php(78): craft\web\Controller->renderTemplate('', Array)
#13 [internal function]: craft\controllers\TemplatesController->actionRender('', Array)
#14 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#15 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#16 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller->runAction('render', Array)
#17 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('render', Array)
#18 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module->runAction('templates/rende...', Array)
#19 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/yiisoft/yii2/web/Application.php(103): craft\web\Application->runAction('templates/rende...', Array)
#20 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/craftcms/cms/src/web/Application.php(286): yii\web\Application->handleRequest(Object(craft\web\Request))
#21 /Users/bryantwells/Sites/_sketchbook/53_craft-test/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#22 /Users/bryantwells/Sites/_sketchbook/53_craft-test/web/index.php(21): yii\base\Application->run()
#23 {main}

brandonkelly added a commit that referenced this issue Jun 24, 2019
@brandonkelly
Copy link
Member

Thanks. Still not able to reproduce but I at least made the code where the error is occurring a little more defensive, so hopefully that will fix this for you.

Try changing your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "dev-develop#46499a1f7be531b8867b6953b2e6a6e0fa25e039 as 3.1.31",
  "...": "..."
}

Then run composer update. Let me know if that works.

@markdrzy
Copy link

I'm hitting the exact same TypeError as @bryantwells. My environment is PHP 7.3.4 if that has any bearing. I can confirm that 46499a1 fixes the issue.

@markdrzy
Copy link

This appears to occur because Twig is now wrapping the filtered result in \Iterator\Iterator:

https://github.com/twigphp/Twig/blob/1107684beef36b9ecd585c2ac9786c49e9bbd5c6/src/Extension/CoreExtension.php#L1523-L1531

And Craft 3.1.31 is wrapping the filtered result in iterator_to_array():

/**
* Filters an array.
*
* @param array|\Traversable $arr
* @param callable|null $arrow
* @return array|\Traversable
*/
public function filterFilter($arr, $arrow = null)
{
if ($arrow === null) {
return array_filter($arr);
}
return iterator_to_array(twig_array_filter($arr, $arrow));
}

@brandonkelly brandonkelly reopened this Jun 24, 2019
@brandonkelly
Copy link
Member

brandonkelly commented Jun 24, 2019

Aha! Looks like twig_array_filter() was rewritten in Twig 2.11.3. My local install was stuck on 2.11.2 so I had missed that change.

We’ll release Craft 3.1.32 tomorrow with the fix. In the meantime anyone who is running into this error can update to 46499a1 manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants