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

Use first blueprint #70

Merged
merged 6 commits into from
Oct 11, 2023
Merged
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
3 changes: 3 additions & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Table Of Contents:
## Configuration

If you'd like to have a different event timezone default than the app default (usually UTC), publish the config file (`php artisan vendor:publish --tag=events-config`), then update it via the CP. This is used on individual events that do not have a timezone set (see Fieldset below).

The default collection for your events is `events`, if you use a different one, publish the config file and then update it via the CP.

## Fieldset

In your collection's blueprint, make sure you have fields like in our sample [fieldset](https://github.com/transformstudios/statamic-events/blob/main/resources/fieldsets/event.yaml).
Expand Down
12 changes: 12 additions & 0 deletions resources/blueprints/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@ timezone:
max_items: 1
mode: typeahead
default: UTC
collection:
max_items: 1
mode: select
type: collections
display: 'Events Collection'
icon: collections
localizable: false
listable: hidden
instructions_position: above
visibility: visible
replicator_preview: true
hide_display: false
2 changes: 1 addition & 1 deletion resources/fieldsets/event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fields:
listable: hidden
instructions_position: above
mode: typeahead
width: 25
width: 50
-
handle: days
field:
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/FieldNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace TransformStudios\Events\Exceptions;

use Exception;

class FieldNotFoundException extends Exception
{
public function __construct(private string $field)
{
parent::__construct("Field [{$field}] not found, please make sure you have a `timezone` field in your blueprint.");

$this->field = $field;
}
}
10 changes: 10 additions & 0 deletions src/Fieldtypes/Timezones.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TransformStudios\Events\Fieldtypes;

use Statamic\Fieldtypes\Relationship;
use Statamic\Support\Arr;

class Timezones extends Relationship
{
Expand All @@ -26,6 +27,15 @@ public function getIndexItems($request)
->map(fn (array $zone) => $this->toItemArray($zone['timezone']));
}

public function preProcess($data)
{
if (is_array($data)) {
return [Arr::get($data, 'timezone')];
}

return Arr::wrap($data);
}

protected function toItemArray($key)
{
if (is_null($key)) {
Expand Down
14 changes: 11 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Edalzell\Forma\Forma;
use Illuminate\Support\Carbon;
use Statamic\Facades\Blueprint;
use Statamic\Facades\Collection;
use Statamic\Facades\Site;
use Statamic\Providers\AddonServiceProvider;
use Statamic\Support\Arr;
use TransformStudios\Events\Exceptions\FieldNotFoundException;
use TransformStudios\Events\Fieldtypes\Timezones;
use TransformStudios\Events\Modifiers\InMonth;
use TransformStudios\Events\Modifiers\IsEndOfWeek;
Expand Down Expand Up @@ -73,8 +74,15 @@ private function bootFields(): self

$timezone = config('events.timezone', config('app.timezone'));

return Blueprint::find('collections/events/event')
->field('timezone')
$collection = Collection::findByHandle(config('events.collection', 'events'));

$blueprint = Arr::first($collection->entryBlueprints());

if (is_null($tzField = $blueprint->field('timezone'))) {
throw new FieldNotFoundException('timezone');
}

return $tzField
->setValue($timezone)
->augment()
->value()
Expand Down
Loading