diff --git a/source/_includes/talk/events.html.twig b/source/_includes/talk/events.html.twig index ffd2e5b2..f5ad5d28 100644 --- a/source/_includes/talk/events.html.twig +++ b/source/_includes/talk/events.html.twig @@ -2,7 +2,7 @@
Here are a list of my previous conference and user group talks:
-{% include "talks-table" with { - talks: { talks: data.talks|merge(page.talks), event_data: site.events }|past_talks -} %} +{% include "talks-table" with { talks: getPastTalks(data.talks, site.events) } %}Upcoming talks can be found on the talks page.
diff --git a/source/talks.html.twig b/source/talks.html.twig index 01fc26ae..6b431bb6 100644 --- a/source/talks.html.twig +++ b/source/talks.html.twig @@ -10,7 +10,7 @@ use: [talks]All previous talks can be found in the talks archive.
{% endblock %} diff --git a/src/FormatTalksBundle/Twig/FormatTalksExtension.php b/src/FormatTalksBundle/Twig/FormatTalksExtension.php index effcab78..86b6d407 100644 --- a/src/FormatTalksBundle/Twig/FormatTalksExtension.php +++ b/src/FormatTalksBundle/Twig/FormatTalksExtension.php @@ -3,8 +3,8 @@ namespace FormatTalksBundle\Twig; use Illuminate\Support\Collection; +use Twig\TwigFunction; use Twig_Extension; -use Twig_SimpleFilter; class FormatTalksExtension extends Twig_Extension { @@ -21,12 +21,12 @@ class FormatTalksExtension extends Twig_Extension /** * {@inheritdoc} */ - public function getFilters() + public function getFunctions() { return [ - new Twig_SimpleFilter('all_talks', [$this, 'getAll']), - new Twig_SimpleFilter('upcoming_talks', [$this, 'getUpcoming']), - new Twig_SimpleFilter('past_talks', [$this, 'getPast']), + new TwigFunction('getAllTalks', [$this, 'getAll']), + new TwigFunction('getUpcomingTalks', [$this, 'getUpcoming']), + new TwigFunction('getPastTalks', [$this, 'getPast']), ]; } @@ -39,9 +39,9 @@ class FormatTalksExtension extends Twig_Extension * * @return array */ - public function getAll(array $data) + public function getAll($talks, array $eventData = []) { - return $this->format($data)->sortBy('event.date'); + return $this->format($talks, $eventData)->sortBy('event.date'); } /** @@ -49,15 +49,15 @@ class FormatTalksExtension extends Twig_Extension * * Used on the main talks page. * - * @param array $data The talk and event data. - * * @return array */ - public function getUpcoming(array $data) + public function getUpcoming($talks, array $eventData = []) { - return $this->format($data)->filter(function ($talk) { - return $talk['event']['date'] >= $this->today; - })->sortBy('event.date'); + return $this->format($talks, $eventData) + ->filter(function ($talk) { + return $talk['event']['date'] >= $this->today; + }) + ->sortBy('event.date'); } /** @@ -69,11 +69,13 @@ class FormatTalksExtension extends Twig_Extension * * @return array */ - public function getPast(array $data) + public function getPast($talks, array $eventData = []) { - return $this->format($data)->filter(function ($talk) { - return $talk['event']['date'] < $this->today; - })->sortByDesc('event.date'); + return $this->format($talks, $eventData) + ->filter(function ($talk) { + return $talk['event']['date'] < $this->today; + }) + ->sortByDesc('event.date'); } /** @@ -83,11 +85,11 @@ class FormatTalksExtension extends Twig_Extension * * @return Collection The event and talk data. */ - public function format(array $data) + public function format($talks, array $event_data) { - $event_data = collect($data['event_data']); + $event_data = collect($event_data); - return collect($data['talks'])->flatMap(function ($talk) use ($event_data) { + return collect($talks)->flatMap(function ($talk) use ($event_data) { // Build an associative array with the talk, as well as the // specified event data (e.g. date and time) as well as the shared // event data (e.g. event name and website). diff --git a/tests/FormatTalksBundle/Twig/FormatTalksTest.php b/tests/FormatTalksBundle/Twig/FormatTalksTest.php index 0d91e929..2619cb0d 100644 --- a/tests/FormatTalksBundle/Twig/FormatTalksTest.php +++ b/tests/FormatTalksBundle/Twig/FormatTalksTest.php @@ -27,8 +27,7 @@ class FormatTalksTest extends TestCase */ public function testFormat() { - $data = [ - 'event_data' => [ + $event_data = [ 'event-a' => [ 'name' => 'Event A', 'location' => 'Somewhere', @@ -39,8 +38,9 @@ class FormatTalksTest extends TestCase 'location' => 'Somewhere else', 'website' => 'http://event-b.com', ], - ], - 'talks' => [ + ]; + + $talks = [ [ 'title' => 'Talk A', 'events' => [ @@ -54,10 +54,9 @@ class FormatTalksTest extends TestCase ['event' => 'event-b', 'date' => '2018-01-31', 'time' => '17:00'], ], ], - ], ]; - $results = $this->extension->format($data)->all(); + $results = $this->extension->format($talks, $event_data)->all(); $this->assertCount(3, $results); @@ -107,15 +106,12 @@ class FormatTalksTest extends TestCase $eventB = ['date' => (new DateTime('-2 weeks'))->format('Y-m-d')]; $eventC = ['date' => (new DateTime('today'))->format('Y-m-d')]; - $data = [ - 'event_data' => [], - 'talks' => [ - ['events' => [$eventA, $eventB]], - ['events' => [$eventC]], - ], + $talks = [ + ['events' => [$eventA, $eventB]], + ['events' => [$eventC]], ]; - $results = $this->extension->getAll($data); + $results = $this->extension->getAll($talks); $this->assertCount(3, $results); @@ -137,15 +133,12 @@ class FormatTalksTest extends TestCase $eventD = ['date' => (new DateTime('+1 day'))->format('Y-m-d')]; $eventE = ['date' => (new DateTime('+2 weeks'))->format('Y-m-d')]; - $data = [ - 'event_data' => [], - 'talks' => [ - ['events' => [$eventA, $eventC]], - ['events' => [$eventB, $eventE]], - ], + $talks = [ + ['events' => [$eventA, $eventC]], + ['events' => [$eventB, $eventE]], ]; - $results = $this->extension->getUpcoming($data); + $results = $this->extension->getUpcoming($talks); $this->assertCount(3, $results); @@ -168,16 +161,13 @@ class FormatTalksTest extends TestCase $eventE = ['date' => (new DateTime('-2 days'))->format('Y-m-d')]; $eventF = ['date' => (new DateTime('-2 months'))->format('Y-m-d')]; - $data = [ - 'event_data' => [], - 'talks' => [ - ['events' => [$eventD]], - ['events' => [$eventA, $eventB, $eventC]], - ['events' => [$eventF]], - ], + $talks = [ + ['events' => [$eventD]], + ['events' => [$eventA, $eventB, $eventC]], + ['events' => [$eventF]], ]; - $results = $this->extension->getPast($data); + $results = $this->extension->getPast($talks); $this->assertCount(2, $results);