Files
KantSites/src/Content/NotFoundPage.php
T
kantarci 16da844934
CMS quality / php (8.2) (push) Canceled after 0s
CMS quality / php (8.4) (push) Canceled after 0s
CMS quality / browser (push) Canceled after 0s
CMS quality / mysql (push) Canceled after 0s
feat: assign custom 404 pages with the website theme (1.8.0)
2026-09-14 12:18:33 +02:00

72 lines
3.1 KiB
PHP

<?php
namespace App\Content;
use App\Domain\InvalidPage;
use Doctrine\DBAL\Connection;
/** A normal block page supplies the content; an error response always keeps the site shell. */
final class NotFoundPage
{
public function __construct(private SiteSettings $settings, private PageRepository $pages, private Connection $db)
{
}
public function selected(): ?array
{
$id = $this->settings->load()['notFoundPageId'];
return '' === $id ? null : ($this->pages->choices(id: $id)[0] ?? null);
}
public function page(int $status = 404): array
{
$id = $this->settings->load()['notFoundPageId'];
$published = 404 === $status && '' !== $id ? $this->pages->findPublishedById($id) : null;
return [
'id' => 'error-'.$status, 'path' => '', 'document' => self::document($status),
...($published ?? []),
'locale' => 'de', 'type' => 'page', 'robots' => 'noindex', 'seo_title' => '',
'header_area' => null, 'footer_area' => null,
];
}
public static function document(int $status = 404): array
{
$title = 410 === $status ? 'Diese Seite ist nicht mehr verfügbar.' : 'Seite nicht gefunden';
return [
'schemaVersion' => 10, 'title' => $title, 'description' => 'Die gesuchte Seite ist nicht verfügbar. Über die Startseite findest du wieder zum richtigen Inhalt.',
'blocks' => [[
'id' => 'error-section', 'type' => 'section',
'style' => ['desktop' => ['textAlign' => 'center', 'minHeight' => '45vh', 'padding' => ['top' => '80px', 'bottom' => '80px']]],
'children' => [
['id' => 'error-code', 'type' => 'text', 'props' => ['text' => (string) $status]],
['id' => 'error-heading', 'type' => 'heading', 'props' => ['text' => $title, 'level' => 1]],
['id' => 'error-message', 'type' => 'text', 'props' => ['text' => 'Die Adresse ist möglicherweise veraltet oder wurde falsch eingegeben. Über die Startseite findest du wieder zum richtigen Inhalt.']],
['id' => 'error-home', 'type' => 'button', 'props' => ['text' => 'Zur Startseite', 'href' => '/']],
],
]],
];
}
public function create(string $expectedId): string
{
return $this->db->transactional(function () use ($expectedId): string {
if (null !== $this->selected()) {
throw new InvalidPage('Es ist bereits eine 404-Seite zugewiesen. Du kannst sie direkt bearbeiten.');
}
$slug = '404';
for ($suffix = 2; false !== $this->db->fetchOne('SELECT id FROM page WHERE website_id = ? AND locale = ? AND path = ?', ['default', 'de', $slug]); ++$suffix) {
$slug = '404-'.$suffix;
}
$id = $this->pages->createPage($slug, null, 'page', self::document());
$this->db->executeStatement('UPDATE page SET robots = ? WHERE id = ?', ['noindex', $id]);
$this->settings->assignNotFoundPage($id, $expectedId);
return $id;
});
}
}