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

Throw a helpful exception if a referenced blueprint does not exist #3977

Merged
merged 9 commits into from
Jul 19, 2021
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