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

[5.x] Add Nav & Collection Tree Saving events #10625

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
4 changes: 4 additions & 0 deletions resources/js/components/navigation/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ export default {
},

treeSaved(response) {
if (! response.data.saved) {
return this.$toast.error(`Couldn't save tree`)
}

this.replaceGeneratedIds(response.data.generatedIds);

this.changed = false;
Expand Down
4 changes: 4 additions & 0 deletions resources/js/components/structures/PageTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ export default {
};

return this.$axios.patch(this.submitUrl, payload).then(response => {
if (! response.data.saved) {
return this.$toast.error(`Couldn't save tree`)
}

this.$emit('saved', response);
this.$toast.success(__('Saved'));
this.initialPages = this.pages;
Expand Down
23 changes: 23 additions & 0 deletions src/Events/CollectionTreeSaving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class CollectionTreeSaving extends Event
{
public $tree;

public function __construct($tree)
{
$this->tree = $tree;
}

/**
* Dispatch the event with the given arguments, and halt on first non-null listener response.
*
* @return mixed
*/
public static function dispatch()
{
return event(new static(...func_get_args()), [], true);
}
}
23 changes: 23 additions & 0 deletions src/Events/NavTreeSaving.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Statamic\Events;

class NavTreeSaving extends Event
{
public $tree;

public function __construct($tree)
{
$this->tree = $tree;
}

/**
* Dispatch the event with the given arguments, and halt on first non-null listener response.
*
* @return mixed
*/
public static function dispatch()
{
return event(new static(...func_get_args()), [], true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public function update(Request $request, $collection)
// if somehow the root would end up having child pages, which isn't allowed.
$contents = $structure->validateTree($contents, $request->site);

$tree->tree($contents)->save();
return [
'saved' => $tree->tree($contents)->save(),
];
}

private function toTree($items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ public function update(Request $request, $nav)

$tree = $this->reorderTree($request->pages);

$nav->in($request->site)->tree($tree)->save();
$saved = $nav->in($request->site)->tree($tree)->save();

return [
'generatedIds' => $this->generatedIds,
'saved' => $saved,
];
}

Expand Down
6 changes: 6 additions & 0 deletions src/Structures/CollectionTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Statamic\Contracts\Structures\CollectionTreeRepository;
use Statamic\Events\CollectionTreeDeleted;
use Statamic\Events\CollectionTreeSaved;
use Statamic\Events\CollectionTreeSaving;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -45,6 +46,11 @@ protected function dispatchSavedEvent()
CollectionTreeSaved::dispatch($this);
}

protected function dispatchSavingEvent()
{
return CollectionTreeSaving::dispatch($this);
}

protected function dispatchDeletedEvent()
{
CollectionTreeDeleted::dispatch($this);
Expand Down
6 changes: 6 additions & 0 deletions src/Structures/NavTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Statamic\Contracts\Structures\NavTreeRepository;
use Statamic\Events\NavTreeDeleted;
use Statamic\Events\NavTreeSaved;
use Statamic\Events\NavTreeSaving;
use Statamic\Facades\Blink;
use Statamic\Facades\Nav;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -43,6 +44,11 @@ protected function dispatchSavedEvent()
NavTreeSaved::dispatch($this);
}

protected function dispatchSavingEvent()
{
return NavTreeSaving::dispatch($this);
}

protected function dispatchDeletedEvent()
{
NavTreeDeleted::dispatch($this);
Expand Down
11 changes: 11 additions & 0 deletions src/Structures/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public function findByEntry($id)

public function save()
{
if ($this->dispatchSavingEvent() === false) {
return false;
}

$this->cachedFlattenedPages = null;

Blink::forget('collection-structure-flattened-pages-collection*');
Expand All @@ -166,6 +170,8 @@ public function save()
$this->dispatchSavedEvent();

$this->syncOriginal();

return true;
}

public function delete()
Expand All @@ -186,6 +192,11 @@ protected function dispatchSavedEvent()
//
}

protected function dispatchSavingEvent()
{
//
}

protected function dispatchDeletedEvent()
{
//
Expand Down
48 changes: 36 additions & 12 deletions tests/Data/Structures/CollectionTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Tests\Data\Structures;

use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Events\CollectionTreeSaving;
use Statamic\Facades\Blink;
use Statamic\Facades\Collection;
use Statamic\Structures\CollectionTree;
Expand All @@ -16,16 +18,6 @@ class CollectionTreeTest extends TestCase
use PreventSavingStacheItemsToDisk;
use UnlinksPaths;

private $directory;

public function setUp(): void
{
parent::setUp();

$stache = $this->app->make('stache');
$stache->store('collection-trees')->directory($this->directory = '/path/to/structures/collections');
}

#[Test]
public function it_can_get_and_set_the_handle()
{
Expand Down Expand Up @@ -61,7 +53,7 @@ public function it_gets_the_path()
$collection = Collection::make('pages')->structureContents(['root' => true]);
Collection::shouldReceive('findByHandle')->with('pages')->andReturn($collection);
$tree = $collection->structure()->makeTree('en');
$this->assertEquals('/path/to/structures/collections/pages.yaml', $tree->path());
$this->assertEquals($this->fakeStacheDirectory.'/content/structures/collections/pages.yaml', $tree->path());
}

#[Test]
Expand All @@ -75,7 +67,7 @@ public function it_gets_the_path_when_using_multisite()
$collection = Collection::make('pages')->structureContents(['root' => true]);
Collection::shouldReceive('findByHandle')->with('pages')->andReturn($collection);
$tree = $collection->structure()->makeTree('en');
$this->assertEquals('/path/to/structures/collections/en/pages.yaml', $tree->path());
$this->assertEquals($this->fakeStacheDirectory.'/content/structures/collections/en/pages.yaml', $tree->path());
}

#[Test]
Expand Down Expand Up @@ -118,4 +110,36 @@ public function it_does_a_diff()
$this->assertEquals(['1.1', '2.2', '2.3'], $diff->moved());
$this->assertEquals(['1.1'], $diff->ancestryChanged());
}

#[Test]
public function it_fires_a_saving_event()
{
Event::fake();

$collection = Collection::make('test')->structureContents(['root' => true]);
Collection::shouldReceive('findByHandle')->with('test')->andReturn($collection);

$tree = $collection->structure()->makeTree('en');
$tree->save();

Event::assertDispatched(CollectionTreeSaving::class);

$this->assertFileExists($tree->path());
}

#[Test]
public function returning_false_in_collection_tree_saving_stops_saving()
{
Event::listen(CollectionTreeSaving::class, function (CollectionTreeSaving $event) {
return false;
});

$collection = Collection::make('test')->structureContents(['root' => true]);
Collection::shouldReceive('findByHandle')->with('test')->andReturn($collection);

$tree = $collection->structure()->makeTree('en');
$tree->save();

$this->assertFileDoesNotExist($tree->path());
}
}
26 changes: 26 additions & 0 deletions tests/Data/Structures/NavTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Tests\Data\Structures;

use Facades\Statamic\Structures\BranchIds;
use Illuminate\Support\Facades\Event;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Events\NavTreeSaving;
use Statamic\Facades\Blink;
use Statamic\Facades\File;
use Statamic\Facades\Nav;
Expand Down Expand Up @@ -113,4 +115,28 @@ public function it_doesnt_save_tree_when_ensuring_ids_if_nothing_changed()
$this->assertEquals($existingTree, $tree->tree());
$this->assertEquals($existingFileContents, File::get($tree->path()));
}

#[Test]
public function it_fires_a_saving_event()
{
Event::fake();

$nav = tap(Nav::make('links'))->save();
tap($nav->makeTree('en', [['id' => 'the-id', 'title' => 'Branch']]))->save();

Event::assertDispatched(NavTreeSaving::class);
}

#[Test]
public function returning_false_in_nav_tree_saving_stops_saving()
{
Event::listen(NavTreeSaving::class, function (NavTreeSaving $event) {
return false;
});

$nav = tap(Nav::make('links'))->save();
$tree = tap($nav->makeTree('en', [['id' => 'the-id', 'title' => 'Branch']]))->save();

$this->assertFileDoesNotExist($tree->path());
}
}
Loading