Update SLUG field in server side post_create and put_update hooks.

This commit is contained in:
Mario Linz 2022-06-30 15:00:18 +02:00
parent 49896d6978
commit dbbd7c63ed
7 changed files with 55 additions and 14 deletions

View File

@ -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

View File

@ -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: "<div>{{ article.content.title }}</div><div>{{ article.general.locale }}</div>" }
- name: tags
type: string[]
meta:

View File

@ -5,7 +5,7 @@ meta:
label:
de: Einstellungen zum Artikel
en: Article Setings
activeTab: 0
activeTab: 1
subFields:
- name: general
type: object

View File

@ -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
})()

View File

@ -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
})()

17
api/hooks/lib/helper.js Normal file
View File

@ -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,
}

View File

@ -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) => {