maestroerror/statamic-magic-import

Statamic 的无缝、神奇导入体验

2.0 2023-10-25 11:43 UTC

This package is auto-updated.

Last update: 2024-08-26 15:45:25 UTC


README

您轻松数据导入的首选解决方案。凭借用户友好的界面和对 JSON 数据格式的支持,将数据导入 Statamic 变得像挥动魔杖一样简单。体验无需烦恼的数据管理魔法

此插件可以与使用 statamic exporter Wordpress 插件 提取的 JSON 文件一起使用。

JSON 导入

JSON 文件示例

collections.example.json

{
  // Defining collections
  "collections": {
    // Name of new collection (You can add as many collections as you need)
    "post": {
      // Entrie slug
      "/post/top-10-titles": {
        // Date of creation (here you can use "order" keyword as well)
        "date": "2023-10-04",
        // Fields
        "data": {
          "title": "Top 10 Titles for example data",
          "content": "<div>Some HTML content for TinyMCE or Bard fields</div>",
          "author": "admin",
          "featured_image": "https://example.com/images/nice-image.webp", // It will download your image and add to assets
          "categories": ["category_1", "category_2"], // Create taxonomy and import it in "Taxonomy terms" field
          "tags": ["tag_1", "tag_2", "tag_3"] // For "Taxonomy terms" field
        }
      }
    }
  }
}

taxonomies.example.json

{
  // Defining taxonomies
  "taxonomies": {
    // Taxonomy name
    "categories": {
      // Taxonomy terms and titles:
      "development": {
        "title": "Development"
      },
      "outsourcing": {
        "title": "Outsourcing"
      },
      "product-design": {
        "title": "Product design"
      },
      "project-management": {
        "title": "Project management"
      },
      "startups": {
        "title": "Startups"
      },
      "tech-news": {
        "title": "Tech news"
      },
      "uncategorized": {
        "title": "Uncategorized"
      }
    }
  }
}

功能

JSON 导入

  • 创建分类和术语
  • 创建页面
  • 创建集合和条目
    • 包括字段选择功能,用于导入现有集合

以下划线开头的前缀任何元数据键将被忽略。

事件

此插件使用内置方法创建和保存内容。因此,将触发正常事件,您可以根据您的配置进行钩子操作以进行额外工作。这意味着您可以通过以下事件来定制导入

  • Statamic\Events\CollectionCreated
  • Statamic\Events\CollectionSaved
  • Statamic\Events\EntrySaving
  • Statamic\Events\EntryCreated
  • Statamic\Events\EntrySaved
  • Statamic\Events\TaxonomySaved
  • Statamic\Events\TermSaved
  • Statamic\Events\AssetSaved
  • Statamic\Events\AssetUploaded

在您阅读此内容时,可能会有其他事件。请参阅 文档 了解更多信息。

图片

包括图片扩展名 (.png, .jpg, .webp 等) 的所有 URL 都将被下载。特色图片将默认下载到 "assets" 容器中(可在配置中更改),到名为 "{collection_handle}/{entry_slug}" 的文件夹中,并保存到 assets 字段。

安装方法

您可以在 Statamic 控制面板的 工具 > 插件 部分中搜索此插件并单击 安装,或者从您的项目根目录运行以下命令

composer require maestroerror/statamic-magic-import

使用方法

转到 工具 > 魔法导入 部分,上传 JSON 文件。

对于集合,摘要将显示您有两个选项:创建新集合或导入现有集合。

statamic-json-import:creating-new-collection

当导入现有集合时,您可以为每个集合字段选择 JSON 字段。

即使创建新集合,也可能存在具有相同名称的集合。如果您选择仍然导入,内容将被覆盖,但您可以(取消)选择任何您想要的(按标题)并单击“导入”。

statamic-json-import:pages-before-choice

statamic-json-import:collection-after-choice

注意:如果您导入大量数据集和/或许多图片,可能会遇到超时错误。

配置

配置文件的内容如下所示

<?php

return [

    /*
     * Enable downloading images. The default is 'true'.
     */
    'download_images' => true,

    /**
     * The name of the assets container where images should be downloaded.
     */
    'assets_container' => 'assets',

    /*
     * Whether to skip download of an image if it already exist. The default is 'false'.
     */
    'skip_existing_images' => false,

    /*
     * Enable image overwriting. When set to false, a new image are created with a timestamp suffix, if the image already exists. The default is 'false'.
     */
    'overwrite_images' => false,

    /*
     * Set images as Asset object, path or ID
     * Possible values: path, object, id (If none of them is set 'path' will be used)
     */
    'set_images_as' => "path",

    /*
     * Sets data in same manner as in CP controller, processing via blueprint's fields
     * Skips field if any error occured and logs the error
     */
    'set_data_using_fields' => false,

    /*
     * In case if you are updating your images path with some suffix, for example ".webp"
     */
    'image_suffix' => "",

    /*
     * Filter out meta data keys prefixed with '_'. The default is 'true'.
     */
    'exclude_underscore_data' => true,

];

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag=statamic-magic-import-config

您可以使用以下命令发布其他资产

php artisan vendor:publish --tag=statamic-magic-import

待办事项

  • 使用 JSON 字段两次会跳过第一个字段(之前曾使用 "title" 从 json 中使用 "title" 和 "header" 在集合中,并且集合的 "title" 字段为空)