From dbbd7c63ed9f79f002561d5c394fa1505c29bbd3 Mon Sep 17 00:00:00 2001 From: Mario Linz Date: Thu, 30 Jun 2022 15:00:18 +0200 Subject: [PATCH] Update SLUG field in server side post_create and put_update hooks. --- api/collections/articles.yml | 18 +++++++++--------- api/collections/content.yml | 4 ++-- api/collections/fields/article/_article.yml | 2 +- api/hooks/articles/post_create.js | 11 +++++++++++ api/hooks/articles/put_update.js | 11 +++++++++++ api/hooks/lib/helper.js | 17 +++++++++++++++++ src/components/routes/Content.svelte | 6 ++++-- 7 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 api/hooks/articles/post_create.js create mode 100644 api/hooks/articles/put_update.js create mode 100644 api/hooks/lib/helper.js diff --git a/api/collections/articles.yml b/api/collections/articles.yml index a6de6f7..ac4205f 100644 --- a/api/collections/articles.yml +++ b/api/collections/articles.yml @@ -107,15 +107,15 @@ permissions: put: false delete: false -# hooks: -# post: -# create: -# type: javascript -# file: hooks/article/post_create.js -# put: -# update: -# type: javascript -# file: hooks/article/put_return.js +hooks: + post: + create: + type: javascript + file: hooks/articles/post_create.js + put: + update: + type: javascript + file: hooks/articles/put_update.js fields: - !include fields/article/_article.yml diff --git a/api/collections/content.yml b/api/collections/content.yml index 57c79e6..8282c3d 100644 --- a/api/collections/content.yml +++ b/api/collections/content.yml @@ -176,9 +176,9 @@ fields: choices: endpoint: "articles" params: - sort: "article.publish_date.from" + sort: "article.general.publish_date.from" filter: { "article.assignments.pages": { source: "path" } } - template: { twig: "{{ article.content.title }}" } + template: { twig: "
{{ article.content.title }}
{{ article.general.locale }}
" } - name: tags type: string[] meta: diff --git a/api/collections/fields/article/_article.yml b/api/collections/fields/article/_article.yml index a8f90fd..49cbed8 100644 --- a/api/collections/fields/article/_article.yml +++ b/api/collections/fields/article/_article.yml @@ -5,7 +5,7 @@ meta: label: de: Einstellungen zum Artikel en: Article Setings - activeTab: 0 + activeTab: 1 subFields: - name: general type: object diff --git a/api/hooks/articles/post_create.js b/api/hooks/articles/post_create.js new file mode 100644 index 0000000..22bb847 --- /dev/null +++ b/api/hooks/articles/post_create.js @@ -0,0 +1,11 @@ +// @ts-check +const { generateArticleSlugUrlString: generateSlugUrlStringByArticle } = require("../lib/helper") + +;(function () { + /** @type {import("tibi-types").HookResponse} */ + let hookResponse + + context.data.article.content.slug = generateSlugUrlStringByArticle(context.data.article) + + return hookResponse +})() diff --git a/api/hooks/articles/put_update.js b/api/hooks/articles/put_update.js new file mode 100644 index 0000000..22bb847 --- /dev/null +++ b/api/hooks/articles/put_update.js @@ -0,0 +1,11 @@ +// @ts-check +const { generateArticleSlugUrlString: generateSlugUrlStringByArticle } = require("../lib/helper") + +;(function () { + /** @type {import("tibi-types").HookResponse} */ + let hookResponse + + context.data.article.content.slug = generateSlugUrlStringByArticle(context.data.article) + + return hookResponse +})() diff --git a/api/hooks/lib/helper.js b/api/hooks/lib/helper.js new file mode 100644 index 0000000..67dcc0e --- /dev/null +++ b/api/hooks/lib/helper.js @@ -0,0 +1,17 @@ +const generateArticleSlugUrlString = (article) => { + let slug = article.content.slug + let title = article.content.title + + if (!slug || slug === "") { + slug = title + .replace(/[^a-zA-Z0-9 ]/g, "") + .replace(/\s/g, "-") + .toLowerCase() + } + + return slug +} + +module.exports = { + generateArticleSlugUrlString, +} diff --git a/src/components/routes/Content.svelte b/src/components/routes/Content.svelte index 1ff3f82..3405df2 100644 --- a/src/components/routes/Content.svelte +++ b/src/components/routes/Content.svelte @@ -72,12 +72,13 @@ const loadArticle = (type?: string) => { let pathParts = path.split("/") - let slug = pathParts[pathParts.length - 1] + let slugOrId = pathParts[pathParts.length - 1].toString() // Set default API call filter + let filter = { "article.general.locale": $currentLang, - "article.content.slug": slug, + "article.content.slug": slugOrId, } // Changed filter to find simmilar content for changed language @@ -94,6 +95,7 @@ let apiParams: APIParams = { filter, } + loading = true getArticles("articles", apiParams) .then((respoonse) => {