Skip to content

Commit f18c8b9

Browse files
authored
Include localized events that inherit dates from origin (#197)
1 parent 3a008cc commit f18c8b9

2 files changed

Lines changed: 86 additions & 6 deletions

File tree

‎src/Events.php‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,20 +264,21 @@ private function hasEnded(Entry $event, string|CarbonInterface $from): bool
264264

265265
private function lastPossibleDate(Entry $event): ?CarbonImmutable
266266
{
267-
$recurrence = $event->get('recurrence');
267+
// value() inherits from the origin so localized stubs still resolve dates
268+
$recurrence = $event->value('recurrence');
268269

269-
if ($recurrence === 'multi_day' || $event->get('multi_day')) {
270-
$dates = collect($event->get('days'))->pluck('date')->filter();
270+
if ($recurrence === 'multi_day' || $event->value('multi_day')) {
271+
$dates = collect($event->value('days'))->pluck('date')->filter();
271272

272273
return $dates->isEmpty() ? null : $this->parseDate($dates->max());
273274
}
274275

275276
if (in_array($recurrence, ['daily', 'weekly', 'monthly', 'every'])) {
276277
// no end date means it recurs forever
277-
return $this->parseDate($event->get('end_date'));
278+
return $this->parseDate($event->value('end_date'));
278279
}
279280

280-
return $this->parseDate($event->get('start_date'));
281+
return $this->parseDate($event->value('start_date'));
281282
}
282283

283284
// a date we can't read is one we can't rule out, so let it through to the generator
@@ -306,7 +307,7 @@ private function hasStartDate(Entry $occurrence): bool
306307
}
307308
}
308309

309-
return $occurrence->has('start_date');
310+
return filled($occurrence->value('start_date'));
310311
}
311312

312313
private function paginate(EntryCollection $occurrences): LengthAwarePaginator

‎tests/LocalizedEventsTest.php‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace TransformStudios\Events\Tests;
4+
5+
use Carbon\CarbonImmutable;
6+
use Illuminate\Support\Carbon;
7+
use Statamic\Facades\Entry;
8+
use Statamic\Facades\Site;
9+
use TransformStudios\Events\Events;
10+
11+
beforeEach(function () {
12+
Site::setSites([
13+
'default' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/'],
14+
'ar' => ['name' => 'Arabic', 'locale' => 'ar', 'url' => '/ar/'],
15+
]);
16+
17+
$this->collection->sites(['default', 'ar'])->save();
18+
19+
Carbon::setTestNow('2026-08-15 10:00:00');
20+
});
21+
22+
test('includes localized events that inherit start_date from the origin', function () {
23+
$origin = Entry::make()
24+
->collection('events')
25+
->locale('default')
26+
->slug('art-collective')
27+
->data([
28+
'title' => 'Collective Art Activities',
29+
'start_date' => '2026-08-31',
30+
'start_time' => '14:00',
31+
'end_time' => '16:00',
32+
'timezone' => 'America/Los_Angeles',
33+
]);
34+
$origin->save();
35+
36+
$localized = $origin->makeLocalization('ar');
37+
$localized->set('title', 'أنشطة فنية جماعية');
38+
$localized->save();
39+
40+
expect($localized->has('start_date'))->toBeFalse()
41+
->and($localized->get('start_date'))->toBeNull()
42+
->and($localized->value('start_date'))->toBe('2026-08-31');
43+
44+
Site::setCurrent('ar');
45+
46+
$occurrences = Events::fromCollection('events')
47+
->site('ar')
48+
->between(
49+
CarbonImmutable::parse('2026-08-01')->startOfDay(),
50+
CarbonImmutable::parse('2026-08-31')->endOfDay()
51+
);
52+
53+
expect($occurrences)->toHaveCount(1)
54+
->and($occurrences->first()->title)->toBe('أنشطة فنية جماعية')
55+
->and($occurrences->first()->start->toDateString())->toBe('2026-08-31');
56+
});
57+
58+
test('still drops localized events whose origin start date is in the past', function () {
59+
$origin = Entry::make()
60+
->collection('events')
61+
->locale('default')
62+
->slug('past-event')
63+
->data([
64+
'title' => 'Past Event',
65+
'start_date' => '2026-07-01',
66+
'start_time' => '14:00',
67+
]);
68+
$origin->save();
69+
70+
$localized = $origin->makeLocalization('ar');
71+
$localized->set('title', 'حدث سابق');
72+
$localized->save();
73+
74+
Site::setCurrent('ar');
75+
76+
expect(
77+
Events::fromCollection('events')->site('ar')->upcoming(1)
78+
)->toBeEmpty();
79+
});

0 commit comments

Comments
 (0)