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
44 changes: 44 additions & 0 deletions src/Exceptions/BlueprintDoesNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 BlueprintDoesNotExistException extends Exception implements ProvidesSolution
{
protected $blueprintHandle;

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

$this->blueprintHandle = $blueprintHandle;
}

public function getSolution(): Solution
{
$description = ($suggestedCollection = $this->getSuggestedCollection())
? "Did you mean `$suggestedCollection`?"
: '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 getSuggestedCollection()
{
return StringComparator::findClosestMatch(
Blueprint::in('.')->map->handle()->flatten()->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\BlueprintDoesNotExistException;
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 BlueprintDoesNotExistException($entry->get('blueprint'));
}

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