Skip to content

Commit

Permalink
Throw a helpful exception if a referenced blueprint does not exist (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Jul 19, 2021
1 parent 646793c commit 080d3e7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/Exceptions/BlueprintNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Statamic\Exceptions;

use Exception;
use Facade\Ignition\Support\StringComparator;
use Facade\IgnitionContracts\BaseSolution;
use Facade\IgnitionContracts\ProvidesSolution;
use Facade\IgnitionContracts\Solution;
use Statamic\Facades\Blueprint;
use Statamic\Statamic;

class BlueprintNotFoundException extends Exception implements ProvidesSolution
{
protected $blueprintHandle;
protected $namespace;

public function __construct($blueprintHandle, $namespace = null)
{
parent::__construct("Blueprint [{$blueprintHandle}] not found");

$this->blueprintHandle = $blueprintHandle;
$this->namespace = $namespace;
}

public function getSolution(): Solution
{
$description = ($suggestedBlueprint = $this->getSuggestedBlueprint())
? "Did you mean `$suggestedBlueprint`?"
: 'Are you sure the blueprint exists?';

return BaseSolution::create("The {$this->blueprintHandle} blueprint was not found.")
->setSolutionDescription($description)
->setDocumentationLinks([
'Read the blueprints guide' => Statamic::docsUrl('/blueprints'),
]);
}

protected function getSuggestedBlueprint()
{
if (! $this->namespace) {
return null;
}

return StringComparator::findClosestMatch(
Blueprint::in($this->namespace)->map->handle()->values()->all(),
$this->blueprintHandle
);
}
}
5 changes: 5 additions & 0 deletions src/Http/Controllers/CP/Collections/EntriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Validation\ValidationException;
use Statamic\Contracts\Entries\Entry as EntryContract;
use Statamic\CP\Breadcrumbs;
use Statamic\Exceptions\BlueprintNotFoundException;
use Statamic\Facades\Asset;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -76,6 +77,10 @@ public function edit(Request $request, $collection, $entry)

$blueprint = $entry->blueprint();

if (! $blueprint) {
throw new BlueprintNotFoundException($entry->value('blueprint'), 'collections/'.$collection->handle());
}

if (User::current()->cant('edit-other-authors-entries', [EntryContract::class, $collection, $blueprint])) {
$blueprint->ensureFieldHasConfig('author', ['read_only' => true]);
}
Expand Down

0 comments on commit 080d3e7

Please sign in to comment.