syvainjule/categories

在Kirby中创建和管理多语言分类

安装: 270

依赖: 0

建议者: 0

安全: 0

星标: 20

关注者: 2

分支: 1

开放问题: 3

语言:Vue

类型:kirby-plugin

1.0.2 2021-12-07 10:48 UTC

This package is auto-updated.

Last update: 2024-09-07 16:38:03 UTC


README

此插件有助于处理翻译分类,提供跨语言同步加唯一ID的字段以及一些字段方法。

screenshot


概述

此插件完全免费,并按MIT许可证发布。但是,如果您在商业项目中使用它并想帮助我维持维护,请考虑进行任意捐赠或通过我的联盟链接购买您的许可证。


1. 安装

下载并将此存储库复制到/site/plugins/categories

或者,您可以使用composer安装它:composer require sylvainjule/categories


2. 面板设置

categories字段的目的在于允许编辑员轻松创建和管理多语言分类,同时保持字段内容在语言之间的同步,以保持ID唯一。 它仅在多语言设置中工作。

它需要两个步骤

首先,在您的蓝图中的任何地方添加字段

fields:
  categories:
    label: Categories
    type: categories

其次,您需要通过在配置文件中添加此选项来告诉插件要监视和同步跨语言的template => fieldname

// site/config/config.php
return [
    'sylvainjule.categories.watch' => [
        'template' => 'fieldname',
        'template' => ['fieldname1', 'fieldname2'],
    ]
];

例如,如果您有一个带有categories字段的blog模板和一个带有clients + techniques字段的projects模板,您需要设置

// site/config/config.php
return [
    'sylvainjule.categories.watch' => [
        'blog'     => 'categories',
        'projects' => ['clients', 'techniques'],
    ]
];

然后您可以将字段创建的分类设置为selectmultiselectcheckboxes等动态选项

category:
  label: Category
  type: select
  options: query
  query:
    fetch: page.parent.categories.toCategories
    text: "{{ structureItem.text }}"
    value: "{{ structureItem.id }}"

3. 选项

3.1. 前缀

插件为每个列表项存储一个ID:{{prefix}}-{{index}}。索引在添加新分类时自动递增,但您可以分别为每个字段选择前缀(默认为category-

fields:
  countries:
    label: Countries
    type: categories
    prefix: country- # the field will store country-1, country-2, etc.

3.2. defaultFirst

默认情况下,语言按字母顺序显示。如果您想将默认语言显示在首位,请将此选项设置为true。默认为false

fields:
  countries:
    label: Countries
    type: categories
    defaultFirst: true

4. 前端使用

有几个可用方法可以轻松处理分类。要获取整个分类列表

// returns a Structure
$categories = $page->categories()->toCategories();

foreach($categories as $category) {
    echo $category->text();
    // ... see below the list of all properties
}

从那里您可以访问具有以下属性的结构对象

# content of a category
id: category-1       # $category->id()
text: 'My category'  # $category->text()
translations:        # $category->translations(), Array
  en: 'My category'  # $category->translations()['en']
  fr: 'Ma catégorie' # $category->translations()['fr']

如果您已从分类字段设置selectmultiselectcheckboxes等选项,则字段将存储分类的ID。要从那里获取文本

// toCategory($list, $lang = false)
$list = $page->parent()->categories();
$text = $page->category()->toCategory($list); // returns the category's text in the current language
$text = $page->category()->toCategory($list, 'fr'); // returns the category's text in French

// turns
'category-1' into 'My category' // returns a string
// or
'category-1,category-2' into ['My category', 'My category 2'] // returns an array

5. 替代方案

此插件旨在解决一个非常具体的用例:管理单文本分类。如果您需要将更多数据与每个分类相关联,则这不是一个。

在这种情况下,我建议使用页面+ autoid,每个分类一个页面,您可以将尽可能多的元数据与之关联。然后您需要将它们从索引/可搜索页面/等中删除。

请告诉我您是否有其他方便的方法来处理复杂的多语言分类,我会在这里添加它们。


6. 许可证

MIT