From ac10e95b5127423dc2701c13edaeeef0a74fc93a Mon Sep 17 00:00:00 2001 From: Dreytac Date: Mon, 20 Apr 2026 09:31:28 +1000 Subject: [PATCH] Add the ability to create `blog` pages and `blog-posts`. --- blueprints/pages/blog-post.yml | 24 ++++++++++++++++++++++++ blueprints/pages/blog.yml | 30 ++++++++++++++++++++++++++++++ controllers/blog.php | 28 ++++++++++++++++++++++++++++ index.php | 11 +++++++++++ templates/blog-post.php | 17 +++++++++++++++++ templates/blog.php | 21 +++++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 blueprints/pages/blog-post.yml create mode 100644 blueprints/pages/blog.yml create mode 100644 controllers/blog.php create mode 100644 templates/blog-post.php create mode 100644 templates/blog.php diff --git a/blueprints/pages/blog-post.yml b/blueprints/pages/blog-post.yml new file mode 100644 index 0000000..abeb6c6 --- /dev/null +++ b/blueprints/pages/blog-post.yml @@ -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 diff --git a/blueprints/pages/blog.yml b/blueprints/pages/blog.yml new file mode 100644 index 0000000..3487877 --- /dev/null +++ b/blueprints/pages/blog.yml @@ -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 diff --git a/controllers/blog.php b/controllers/blog.php new file mode 100644 index 0000000..46cbff9 --- /dev/null +++ b/controllers/blog.php @@ -0,0 +1,28 @@ + + * + * 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 . + */ + +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"); +}; diff --git a/index.php b/index.php index 3eafb72..8ec52c9 100644 --- a/index.php +++ b/index.php @@ -14,6 +14,17 @@ Kirby::plugin( name: "hobbyhome/blog", 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: [ "authors" => [[ diff --git a/templates/blog-post.php b/templates/blog-post.php new file mode 100644 index 0000000..ef2a801 --- /dev/null +++ b/templates/blog-post.php @@ -0,0 +1,17 @@ + + + true]) ?> + +text()->kirbytext() ?> + +
+ +user()) and $user->role()->isAdmin()) : ?> +
+
+ +
+
+ + + diff --git a/templates/blog.php b/templates/blog.php new file mode 100644 index 0000000..a95bbe8 --- /dev/null +++ b/templates/blog.php @@ -0,0 +1,21 @@ + + + + + +
+
+ + $post]) ?> + +
+
+ +
+ No posts have been created yet! +
+ + + $posts->pagination()]) ?> + +