Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 02ec8c5

Browse files
Merge pull request #33 from jonassiewertsen/add-synthesizers-for-statamic-types
Add synthesizers for statamic entries
2 parents 9286d66 + 2e73754 commit 02ec8c5

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ class ShowArticles extends Component
157157
}
158158
```
159159

160+
### EXPERIMENTAL: Statamic Support
161+
As a little experiment, support for an Entry or EntryCollection has been added, so you can make an entry or a entry collection simply a public property and it just works.
162+
163+
Supported types:
164+
- Statamic\Entries\EntryCollection;
165+
- Statamic\Entries\Entry;
166+
167+
```php
168+
namespace App\Livewire;
169+
170+
use Livewire\Component;
171+
use Statamic\Entries\EntryCollection;
172+
use Statamic\Entries\Entry;
173+
174+
class Foo extends Component
175+
{
176+
public EntryCollection $entries;
177+
public Entry $entry;
178+
```
179+
180+
To make it work, you need to enable that feature first.
181+
182+
1. php artisan vendor:publish
183+
2. Select statamic-livewire in the list
184+
3. Enable synthesizers
185+
160186
### Entangle: Sharing State Between Livewire And Alpine
161187
In case you want to share state between Livewire and Alpine, there is a Blade directive called `@entangle`. To be usable with Antlers, we do provide a dedicated tag:
162188
```html

config/config.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| EXPERIMENTAL: Livewire Synthesizers
8+
|--------------------------------------------------------------------------
9+
|
10+
| So called synthesizers allow to add custom types to Livewire, which can
11+
| can be used to make Livewire aware of Statamic classes that you want
12+
| to work with, as it might make things easier.
13+
|
14+
| It's recommended to remove or uncomment those synthesizers that are
15+
| not used in your application, to avoid overhead by loading those.
16+
|
17+
| This features is experimental. It's ment to be tested and to played
18+
| with. As long as it is experimental, it can be changed and removed
19+
| at any point without a warning.
20+
|
21+
*/
22+
23+
'synthesizers' => [
24+
25+
'enabled' => false,
26+
27+
'classes' => [
28+
\Jonassiewertsen\Livewire\Synthesizers\EntryCollectionSynthesizer::class,
29+
\Jonassiewertsen\Livewire\Synthesizers\EntrySynthesizer::class,
30+
]
31+
]
32+
];

src/ServiceProvider.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Jonassiewertsen\Livewire;
44

5+
use Livewire\Livewire;
56
use Statamic\Providers\AddonServiceProvider;
67

78
class ServiceProvider extends AddonServiceProvider
@@ -11,4 +12,32 @@ class ServiceProvider extends AddonServiceProvider
1112
protected $tags = [
1213
'Jonassiewertsen\Livewire\Tags\Livewire',
1314
];
15+
16+
public function boot(): void
17+
{
18+
parent::boot();
19+
20+
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'statamic-livewire');
21+
22+
if ($this->app->runningInConsole()) {
23+
$this->publishes([
24+
__DIR__ . '/../config/config.php' => config_path('statamic-livewire.php'),
25+
], 'statamic-livewire');
26+
}
27+
28+
$this->bootSyntesizers();
29+
}
30+
31+
protected function bootSyntesizers()
32+
{
33+
if (! config('statamic-livewire.synthesizers.enabled', false)) {
34+
return;
35+
}
36+
37+
$synthesizers = config('statamic-livewire.synthesizers.classes', []);
38+
39+
foreach ($synthesizers as $synthesizer) {
40+
Livewire::propertySynthesizer($synthesizer);
41+
}
42+
}
1443
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Jonassiewertsen\Livewire\Synthesizers;
4+
5+
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
6+
use Statamic\Entries\Entry;
7+
use Statamic\Entries\EntryCollection as StatamicEntryCollection;
8+
9+
class EntryCollectionSynthesizer extends Synth
10+
{
11+
public static $key = 'entry-collection';
12+
13+
public static function match($target)
14+
{
15+
return $target instanceof StatamicEntryCollection;
16+
}
17+
18+
public function dehydrate($target)
19+
{
20+
$data = [];
21+
22+
foreach ($target->all() as $entry) {
23+
$data[] = [
24+
'collection' => $entry->collection()->handle() ?? null,
25+
'data' => $entry->data()->all(),
26+
'date' => $entry->collection()->dated() ? $entry->date() : null,
27+
'id' => $entry->id(),
28+
'slug' => $entry->slug() ?? null,
29+
];
30+
}
31+
32+
return [ $data, [] ];
33+
}
34+
35+
public function hydrate($values)
36+
{
37+
$items = [];
38+
39+
foreach ($values as $value) {
40+
$entry = Entry::make()
41+
->id($value['id'])
42+
->slug($value['slug'] ?? null)
43+
->collection($value['collection'] ?? null);
44+
45+
if ($value['date']) {
46+
$entry->date($value['date'] ?? null);
47+
}
48+
49+
$items[] = $entry;
50+
}
51+
52+
return New StatamicEntryCollection($items);
53+
}
54+
}

src/Synthesizers/EntrySynthesizer.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Jonassiewertsen\Livewire\Synthesizers;
4+
5+
use Livewire\Mechanisms\HandleComponents\Synthesizers\Synth;
6+
use Statamic\Entries\Entry as StatamicEntry;
7+
use Statamic\Facades\Entry;
8+
9+
class EntrySynthesizer extends Synth
10+
{
11+
public static $key = 'entry-collection';
12+
13+
public static function match($target)
14+
{
15+
return $target instanceof StatamicEntry;
16+
}
17+
18+
public function dehydrate($entry)
19+
{
20+
return [
21+
[
22+
'collection' => $entry->collection()->handle() ?? null,
23+
'data' => $entry->data()->all(),
24+
'date' => $entry->collection()->dated() ? $entry->date() : null,
25+
'id' => $entry->id(),
26+
'slug' => $entry->slug() ?? null,
27+
], [] ];
28+
}
29+
30+
public function hydrate($value)
31+
{
32+
$entry = Entry::make()
33+
->id($value['id'])
34+
->slug($value['slug'] ?? null)
35+
->collection($value['collection'] ?? null);
36+
37+
if ($value['date']) {
38+
$entry->date($value['date'] ?? null);
39+
}
40+
41+
return $entry;
42+
}
43+
}

0 commit comments

Comments
 (0)