Skip to content

Commit d575f0a

Browse files
committed
A big pile of improvements and new features including a new fieldMethod, additional preview controls, and more.
1 parent 3717b2d commit d575f0a

File tree

8 files changed

+316
-138
lines changed

8 files changed

+316
-138
lines changed

README.md

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,22 @@ git submodule add https://github.com/scottboms/kirby-events-field.git site/plugi
3030

3131
In a Page blueprint, add a new field with the type `event.` Standard field attributes such as `label, required, help`, etc. can also be used to override defaults. Use `empty` to change the text displayed when the field is in an empty state.
3232

33-
#### Field Properties
34-
35-
| Name | Type | Default | Description |
36-
|-----------|---------|---------|------------------------------------------------------------------|
37-
| empty | string | `null` | The placeholder text if no information has been added |
38-
| time | boolean | `true` | Include time as part of date fields |
39-
| eventName | boolean | `true` | If `true`, the field is available in the form |
40-
| endDate | boolean | `true` | If `true`, the field is available in the form |
41-
| venue | boolean | `true` | If `true`, the field is available in the form |
42-
| url | boolean | `true` | If `true`, the field is available in the form |
43-
| details | boolean | `true` | If `true`, the field is available in the form |
44-
| preview | array | `[ ]` | Optional array of field names to display in the preview |
33+
### Field Properties
34+
35+
| Name | Type | Default | Description |
36+
|------------|---------|---------|-----------------------------------------------------------------|
37+
| empty | string | `null` | The placeholder text if no information has been added |
38+
| eventName | boolean | `true` | If `true`, the field is available in the form |
39+
| endDate | boolean | `true` | If `true`, the field is available in the form |
40+
| hoursStart | boolean | `true` | If `true`, the field is available in the form |
41+
| hoursEnd | boolean | `true` | If `true`, the field is available in the form |
42+
| city | boolean | `true` | If `true`, the field is available in the form |
43+
| state | boolean | `true` | If `true`, the field is available in the form |
44+
| country | boolean | `true` | If `true`, the field is available in the form |
45+
| venue | boolean | `true` | If `true`, the field is available in the form |
46+
| url | boolean | `true` | If `true`, the field is available in the form |
47+
| details | boolean | `true` | If `true`, the field is available in the form |
48+
| preview | array | `[ ]` | Optional array of field names to display in the preview |
4549

4650

4751
```yml
@@ -55,11 +59,15 @@ In a Page blueprint, add a new field with the type `event.` Standard field attri
5559
type: event
5660
empty: 'Add an event'
5761

58-
# field attributes to include
62+
# optional field properties
5963
eventName: true
60-
endDate: false
61-
time: false
64+
endDate: true
65+
hoursStart: true
66+
hoursEnd: true
6267
venue: true
68+
city: true
69+
state: true
70+
country: true
6371
url: true
6472
details: true
6573

@@ -68,9 +76,14 @@ In a Page blueprint, add a new field with the type `event.` Standard field attri
6876
- eventName
6977
- startDate
7078
- endDate
79+
- hoursStart
80+
- hoursEnd
81+
- eventDates # summary of startDate, endDate
82+
- eventHours # summary of hoursStart, hoursEnd
7183
- city
7284
- state
7385
- country
86+
- location # summary of city, state, country
7487
- venue
7588
- url
7689
- details
@@ -87,7 +100,9 @@ To access an event field in your templates, you can use the toEvent() method.
87100

88101
<div class="dates">
89102
<span class="start"><?= $event->startDate()->toDate('M d, Y') ?></span> –
90-
<span class="end"><?= $event->endDate()->toDate('M d, Y') ?></span>
103+
<span class="end"><?= $event->endDate()->toDate('M d, Y') ?></span>, daily from
104+
<span class="start"><?= $event->hoursStart()->toDate('g:i a') ?></span> –
105+
<span class="end"><?= $event->hoursEnd()->toDate('g:i a') ?></span>
91106
</div>
92107

93108
<div class="location">
@@ -101,6 +116,10 @@ To access an event field in your templates, you can use the toEvent() method.
101116
<?php endif ?>
102117
```
103118

119+
### Field Methods
120+
121+
Special Field Methods have been included to provide additional utility when utilizing the field. These include a `toEvent()` wrapper which behaves similarly to Kirby's native toStructure() method. Additionally, a `daysUntil()` method allows you to provide a simple countdown based on the field's `startDate` value.
122+
104123

105124
## Compatibility
106125

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "scottboms/events",
2+
"name": "scottboms/events-field",
33
"description": "Events Field plugin for Kirby",
44
"type": "kirby-plugin",
55
"homepage": "https://github.com/scottboms/kirby-events-field",

index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.php

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@
1717
throw new Exception('Events Field requires Kirby v4 or v5');
1818
}
1919

20-
Kirby::plugin('scottboms/events', [
20+
Kirby::plugin('scottboms/events-field', [
2121
'fields' => [
2222
'event' => [
2323
'props' => [
2424
'value' => function ($value = null) {
2525
$event = Yaml::decode($value);
2626
return [
27-
'eventName' => null,
28-
'startDate' => null,
29-
'endDate' => null,
30-
'city' => null,
31-
'state' => null,
32-
'country' => null,
33-
'venue' => null,
34-
'url' => null,
35-
'details' => null,
27+
'eventName' => null,
28+
'startDate' => null,
29+
'endDate' => null,
30+
'hoursStart' => null,
31+
'hoursEnd' => null,
32+
'city' => null,
33+
'state' => null,
34+
'country' => null,
35+
'venue' => null,
36+
'url' => null,
37+
'details' => null,
3638
...$event
3739
];
3840
},
@@ -41,58 +43,22 @@
4143
},
4244

4345
// edit-drawer toggles (independent of preview)
44-
'eventName' => fn ($value = true) => (bool)$value,
45-
'endDate' => fn ($value = true) => (bool)$value,
46-
'venue' => fn ($value = true) => (bool)$value,
47-
'url' => fn ($value = true) => (bool)$value,
48-
'details' => fn ($value = true) => (bool)$value,
49-
'time' => fn ($value = true) => (bool)$value,
50-
'empty' => fn ($value = false) => (bool)$value,
46+
'eventName' => fn ($value = true) => (bool)$value,
47+
'endDate' => fn ($value = true) => (bool)$value,
48+
'hoursStart' => fn ($value = true) => (bool)$value,
49+
'hoursEnd' => fn ($value = true) => (bool)$value,
50+
'venue' => fn ($value = true) => (bool)$value,
51+
'url' => fn ($value = true) => (bool)$value,
52+
'details' => fn ($value = true) => (bool)$value,
53+
'empty' => fn ($value = false) => (bool)$value,
5154
]
5255
]
5356
],
5457

55-
'fieldMethods' => [
56-
/**
57-
* $page->event()->toEvent()
58-
* returns an obj with field instances: startDate(), endDate(), city(), etc.
59-
*/
60-
'toEvent' => function (Field $field) {
61-
$raw = $field->value();
62-
$data = is_array($raw) ? $raw : (is_string($raw) ? (Yaml::decode($raw) ?? []) : []);
63-
64-
$defaults = [
65-
'eventName' => null,
66-
'startDate' => null,
67-
'endDate' => null,
68-
'city' => null,
69-
'state' => null,
70-
'country' => null,
71-
'venue' => null,
72-
'url' => null,
73-
'details' => null,
74-
];
75-
$data = array_merge($defaults, $data);
76-
77-
foreach ($data as $k => $v) {
78-
if ($v === '' || $v === []) {
79-
$data[$k] = null;
80-
}
81-
}
82-
83-
// wrap everything as field instances so you can call ->toDate(), ->isNotEmpty(), etc.
84-
$parent = $field->parent() ?? site();
85-
$wrapped = [];
86-
foreach ($data as $key => $value) {
87-
$wrapped[$key] = new Field($parent, $key, $value);
88-
}
89-
//return as obj, so $event->startDate() returns a field
90-
return new Obj($wrapped);
91-
}
92-
],
58+
'fieldMethods' => require __DIR__ . '/lib/fieldMethods.php',
9359

9460
'info' => [
95-
'version' => '1.0.0',
61+
'version' => '1.1.0',
9662
'homepage' => 'https://github.com/scottboms/kirby-events-field',
9763
'license' => 'MIT',
9864
'authors' => [[ 'name' => 'Scott Boms', 'url' => 'https://scottboms.com' ]],

0 commit comments

Comments
 (0)