Add function for getting events for a talk

GH-160
This commit is contained in:
Oliver Davies 2019-12-27 21:24:14 +00:00
parent 105ae345a9
commit 5499f555c1
4 changed files with 86 additions and 5 deletions

View file

@ -33,6 +33,7 @@ class TalksExtension extends AbstractExtension
new TwigFunction('get_upcoming_talks', [$this, 'getUpcomingTalks']),
new TwigFunction('get_past_talks', [$this, 'getPastTalks']),
new TwigFunction('get_past_talk_count', [$this, 'getPastTalkCount']),
new TwigFunction('get_events_for_talk', [$this, 'getEventsForTalk']),
];
}
@ -89,6 +90,18 @@ class TalksExtension extends AbstractExtension
return $this->getPastEvents($talks)->count();
}
public function getEventsForTalk(array $talk, array $eventData): Collection
{
return (new Collection($talk['events']))
->map(function (array $event) use ($eventData): Collection {
return (new Collection($event))->merge($eventData[$event['event']]);
})
->filter(function (Collection $event): bool {
return !empty($event->get('date'));
})
->sortBy('date');
}
private function getLastDate($talk): string
{
return $this->eventsFromTalks(new Collection([$talk]))

View file

@ -83,4 +83,71 @@ class RetrievingEventsTest extends TestCase
$this->assertCount(1, $pastEvents);
$this->assertSame('php_south_wales', $pastEvents[0]['event']);
}
/** @test */
public function get_all_of_the_events_for_a_talk()
{
$talk = [
'title' => 'TDD - Test Driven Drupal',
'events' => [
[
'event' => 'drupal_developer_days_2018',
'date' => (new DateTime('-1 day'))->getTimestamp(),
],
[
'event' => 'drupalcamp_london_2019',
'date' => (new DateTime('+1 day'))->getTimestamp(),
],
],
];
$eventData = [
'drupal_developer_days_2018' => [
'name' => 'Drupal Developer Days, Lisbon 2018',
],
'drupalcamp_london_2019' => [
'name' => 'DrupalCamp London 2019',
],
];
$events = $this->extension->getEventsForTalk($talk, $eventData);
$this->assertCount(2, $events);
$this->assertSame(
['drupal_developer_days_2018', 'drupalcamp_london_2019'],
$events->pluck('event')->toArray()
);
}
/** @test */
public function events_with_no_date_are_not_returned_for_an_event()
{
$talk = [
'title' => 'TDD - Test Driven Drupal',
'events' => [
[
'event' => 'drupal_developer_days_2018',
'date' => (new DateTime('-2 days'))->getTimestamp(),
],
[
'event' => 'drupalcamp_london_2019',
'date' => '',
],
],
];
$eventData = [
'drupal_developer_days_2018' => [
'name' => 'Drupal Developer Days, Lisbon 2018',
],
'drupalcamp_london_2019' => [
'name' => 'DrupalCamp London 2019',
],
];
$events = $this->extension->getEventsForTalk($talk, $eventData);
$this->assertCount(1, $events);
$this->assertSame('drupal_developer_days_2018', $events->pluck('event')->first());
}
}

View file

@ -17,6 +17,6 @@
{% include 'talk/slides' %}
{% include 'talk/video' %}
{% include 'talk/tweets' %}
{% include 'talk/events' %}
{% include 'talk/events' with { events: get_events_for_talk(page, site.events) } only %}
</div>
{% endblock %}

View file

@ -1,13 +1,14 @@
{% import 'helpers' as helpers %}
{% if page.events %}
{% if events %}
<div>
<h2 class="mb-2">Presented at</h2>
<h2>Presented at</h2>
<ul class="markup list-disc ml-5">
{% for event in page.events if not event.date is empty %}
<ul class="markup mt-2 list-disc ml-5">
{% for event in events %}
<li>
{{ helpers.talkEventName(event) }}
{{ helpers.talkEventLocation(event) }}
<span class="text-gray-800">- {{ event.date|date('jS F Y') }}</span>
</li>
{% endfor %}