Create the ability to create blog pages and blog-posts.

This commit is contained in:
2026-04-20 09:31:28 +10:00
parent 87999e63f8
commit c34cd57610
6 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
title: Blog Post
num: "{{ page.created.toDate('YmdHi') }}"
columns:
- width: 2/3
sections:
content:
type: fields
fields:
text:
label: Content
type: textarea
autofocus: true
size: large
customExcerpt:
label: Custom Excerpt
type: textarea
size: medium
- width: 1/3
sections:
pinned: fields/pinned
showOnHome: fields/show-on-home
dates: fields/dates

30
blueprints/pages/blog.yml Normal file
View File

@@ -0,0 +1,30 @@
title: Blog
columns:
- width: 2/3
sections:
drafts:
type: pages
status: draft
templates: blog-post
info: "{{ page.created.toDate('Y-m-d @ g:ia') }}"
published:
type: pages
status: published
info: "{{ page.created.toDate('Y-m-d @ g:ia') }}"
- width: 1/3
sections:
options:
type: fields
fields:
postsPerPage:
label: Posts Per Page
type: number
default: 10
line:
type: fields
fields:
line:
type: line
linkLocation: fields/link-location

28
controllers/blog.php Normal file
View File

@@ -0,0 +1,28 @@
<?php
/**
* Copyright 2026, Dreytac <dreytac@hobbyhome.net>
*
* This file is part of Kirby Blog.
*
* Kirby Blog is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
*
* Kirby Blog is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License along with Kirby Blog. If not, see <https://www.gnu.org/licenses/>.
*/
return function ($page) {
$posts = new Pages();
if ($user = kirby()->user() and $user->role()->isAdmin()) {
$posts = $page->childrenAndDrafts();
} else {
$posts = $page->children();
}
$posts = $posts->sortBy("pinned", SORT_DESC, "updated", SORT_DESC);
$posts = $posts->paginate($page->postsPerPage()->toInt());
return compact("posts");
};

View File

@@ -14,6 +14,17 @@
Kirby::plugin( Kirby::plugin(
name: "hobbyhome/blog", name: "hobbyhome/blog",
extends: [ extends: [
"blueprints" => [
"pages/blog" => __DIR__ . "/blueprints/pages/blog.yml",
"pages/blog-post" => __DIR__ . "/blueprints/pages/blog-post.yml",
],
"templates" => [
"blog" => __DIR__ . "/templates/blog.php",
"blog-post" => __DIR__ . "/templates/blog-post.php",
],
"controllers" => [
"blog" => require __DIR__ . "/controllers/blog.php",
],
], ],
info: [ info: [
"authors" => [[ "authors" => [[

17
templates/blog-post.php Normal file
View File

@@ -0,0 +1,17 @@
<?= snippet("header") ?>
<?= snippet("page/title", ["showDate" => true]) ?>
<?= $page->text()->kirbytext() ?>
<hr class="mb-3">
<?php if (($user = $kirby->user()) and $user->role()->isAdmin()) : ?>
<div class="clearfix mb-3">
<div class="float-end">
<a href="<?= $page->panel()->url() ?>" target="_blank"><?= kirbytag("fa", "edit") ?></a>
</div>
</div>
<?php endif ?>
<?= snippet("footer") ?>

21
templates/blog.php Normal file
View File

@@ -0,0 +1,21 @@
<?= snippet("header") ?>
<?= snippet("page/title") ?>
<?php if (count($posts)) : ?>
<div class="row">
<div class="col-12">
<?php foreach ($posts as $post) : ?>
<?= snippet("collection/post", ["post" => $post]) ?>
<?php endforeach ?>
</div>
</div>
<?php else : ?>
<div class="alert alert-info mb-3">
No posts have been created yet!
</div>
<?php endif ?>
<?= snippet("page/pagination", ["pagination" => $posts->pagination()]) ?>
<?= snippet("footer") ?>