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

[5.x] Enable foreach to work with Values #10958

Draft
wants to merge 3 commits into
base: 5.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Tags/Iterate.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ protected function iterate($items)
{
[$keyKey, $valueKey] = $this->getKeyNames();

$items = $items instanceof Value ? $items->value() : $items;

$items = collect($items)
->map(function ($value, $key) use ($keyKey, $valueKey) {
return [$keyKey => $key, $valueKey => $value];
Expand Down
49 changes: 49 additions & 0 deletions tests/Tags/IterateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Tests\Tags;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Parse;
use Tests\TestCase;

class IterateTest extends TestCase
{
#[Test]
public function arrays_work()
{
$template = '{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }}';

$this->assertSame(
'first - One second - Two ',
$this->tag($template, [
'group_field' => [
'first' => 'One',
'second' => 'Two',
],
])
);
}

#[Test]
public function values_work()
{
$this->markTestSkipped('needs implementation');

$template = '{{ foreach:group_field }}{{ key }} - {{ value }} {{ /foreach:group_field }}';

$this->assertSame(
'first - One second - Two ',
$this->tag($template, [
'group_field' => [
'first' => 'One',
'second' => 'Two',
],
])
);
}

private function tag($tag, $context = [])
{
return (string) Parse::template($tag, $context);
}
}
Loading