Skip to content

Commit

Permalink
Merge pull request #6 from jonassiewertsen/add_documentation
Browse files Browse the repository at this point in the history
Clean up and bug fixes
  • Loading branch information
jonassiewertsen authored May 5, 2020
2 parents 04431cf + 111b54a commit 7a52901
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
19 changes: 12 additions & 7 deletions src/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Jonassiewertsen\Statamic\HowTo\Commands;

use Illuminate\Console\Command;
use Jonassiewertsen\Statamic\HowTo\Helper\Documentation;
use Jonassiewertsen\Statamic\HowTo\Helper\Video;
use Statamic\Facades\Collection;
use Statamic\Fields\Blueprint;
use Statamic\Structures\CollectionStructure;

class Setup extends Command
{
Expand Down Expand Up @@ -46,21 +49,22 @@ public function handle()
}

private function createCollections() {
Collection::make(config('howToAddon.collection.videos', 'how_to_addon_videos'))
->entryBlueprints(config('howToAddon.blueprint.videos', 'how_to_addon_videos'))
Collection::make(Video::collectionName())
->entryBlueprints(Video::collectionName())
->title('How to videos')
->save();

Collection::make(config('howToAddon.collection.documentation', 'how_to_addon_documentation'))
->entryBlueprints(config('howToAddon.blueprint.documentation', 'how_to_addon_documentation'))
Collection::make(Documentation::collectionName())
->entryBlueprints(Documentation::collectionName())
->title('How to documentation')
->structure((new CollectionStructure)->maxDepth(2))
->save();
}

protected function createBlueprints()
{
(new Blueprint)
->setHandle(config('howToAddon.blueprint.videos', 'how_to_addon_videos'))
->setHandle(Video::collectionName())
->setContents([
'title' => 'How to Video',
'sections' => [
Expand Down Expand Up @@ -94,7 +98,7 @@ protected function createBlueprints()
])->save();

(new Blueprint)
->setHandle(config('howToAddon.blueprint.documentation', 'how_to_addon_documentation'))
->setHandle(Documentation::collectionName())
->setContents([
'title' => 'How to documentation',
'sections' => [
Expand All @@ -110,6 +114,7 @@ protected function createBlueprints()
'localizable' => false,
'listable' => 'hidden',
'display' => 'Content',
'validate' => 'required',
]]
]
],
Expand All @@ -128,4 +133,4 @@ protected function createBlueprints()
])->save();

}
}
}
8 changes: 8 additions & 0 deletions src/Helper/Documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public static function entryChildren($tree, $nav): LaravelCollection
});
}

/**
* Fetch the belonging children for the navigation
*/
public static function exists(): bool
{
return Collection::handleExists(self::collectionName());
}

/**
* Return the entry title
*/
Expand Down
12 changes: 7 additions & 5 deletions src/Http/Controllers/VideosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace Jonassiewertsen\Statamic\HowTo\Http\Controllers;

use Statamic\Facades\Asset;
use Statamic\Facades\Entry;

class VideosController {
public function index() {
public function index()
{
$videos = Entry::whereCollection('how_to_addon_videos');

$videos = $this->sortByTitle($videos);

return view('howToAddon::videos.index', compact('videos'));
}

public function show($slug) {
public function show($slug)
{
$video = Entry::findBySlug($slug, config('howToAddon.blueprint.videos', 'how_to_addon_videos'));

// TODO: Should be replaced by Asset::findByPath later.
Expand All @@ -24,7 +25,8 @@ public function show($slug) {
return view('howToAddon::videos.show', compact('video', 'url'));
}

private function sortByTitle($videos) {
private function sortByTitle($videos)
{
return collect($videos)->sortBy('title');
}
}
}
16 changes: 9 additions & 7 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ private function createNavigation(): void

Nav::extend(function ($nav) {

Documentation::tree()->map(function ($tree) use ($nav) {
return $nav->create(Documentation::entryTitle($tree['entry']))
->route('howToAddon.documentation.show', Documentation::entrySlug($tree['entry']))
->icon('drawer-file')
->section('Documentation')
->children(Documentation::entryChildren($tree, $nav));
});
if (Documentation::exists()) {
Documentation::tree()->map(function ($tree) use ($nav) {
return $nav->create(Documentation::entryTitle($tree['entry']))
->route('howToAddon.documentation.show', Documentation::entrySlug($tree['entry']))
->icon('drawer-file')
->section('Documentation')
->children(Documentation::entryChildren($tree, $nav));
});
}

// Only show the Manage button, if the permissions have been set
if (Gate::allows('edit', Collection::findByHandle(Documentation::collectionName()))) {
Expand Down

0 comments on commit 7a52901

Please sign in to comment.