fond-of-spryker/contentful

为 Spryker 提供的 contentful 连接器。

5.1.0 2022-12-19 14:39 UTC

README

license

一个 Spryker-Contentful 连接器。从 contentful 导入内容到存储,并通过 cronjob 更新。

安装

composer require fond-of-spryker/contentful

1. 在 YvesBootstrap.php 的 registerServiceProviders() 中添加 twig 服务提供者。

$this->application->register(new ContentfulTwigServiceProvider());

2. 在 YvesBootstrap.php 的 registerRouters() 中添加 ContentfulRouter。

$this->application->addRouter((new ContentfulRouter())->setSsl(false));

3. 在 ConsoleDependencyProvider.php 的 getConsoleCommands() 中添加控制台命令。

new ContentfulConsole(),

4. 在您的商店配置文件中或 config/Shared/config_default.php 中添加配置。

示例配置

// API-Key
$config[\FondOfSpryker\Shared\Contentful\ContentfulConstants::CONTENTFUL_ACCESS_TOKEN] = 'fu';
// Space id
$config[\FondOfSpryker\Shared\Contentful\ContentfulConstants::CONTENTFUL_SPACE_ID] = 'bar';
// Space default locale
$config[\FondOfSpryker\Shared\Contentful\ContentfulConstants::CONTENTFUL_DEFAULT_LOCALE] = 'en';
// Optional: To deactivate an entry. If Field doesn't exists entry is always shown. Default is "isActive"
$config[\FondOfSpryker\Shared\Contentful\ContentfulConstants::CONTENTFUL_FIELD_NAME_ACTIVE] = 'isActive';
// Optional: If entry is a page, this is the field where the url is stored. Default is "identifier"
$config[\FondOfSpryker\Shared\Contentful\ContentfulConstants::CONTENTFUL_FIELD_NAME_IDENTIFIER] = 'identifier';
// Mapping of contentful locales to available shop locales
$config[\FondOfSpryker\Shared\Contentful\ContentfulConstants::CONTENTFUL_LOCALE_TO_STORE_LOCALE] = [
    'en' => 'en_US',
    'de' => 'de_DE'
];

5. 在 jobs.php 中添加 cronjob。

每 5 分钟检索更新后的 contentful 条目。

$jobs[] = [
    'name' => 'contentful-updater',
    'command' => '$PHP_BIN vendor/bin/console contentful:import -vvv',
    'schedule' => '*/5 * * * *',
    'enable' => true,
    'run_on_non_production' => true,
    'stores' => $allStores,
];

6. 运行

vendor/bin/console transfer:generate
vendor/bin/console contentful:import --all

控制台命令

导入最后更新的条目(最后 5 分钟)

vendor/bin/console contentful:import

导入全部

vendor/bin/console contentful:import --all

按 id 导入条目

vendor/bin/console contentful:import [entryId]

在 twig 模板中使用

模板路径是 Theme/default/contentful/[contentType].twig

 {{ contentfulEntry('contentfulEntryId') }}

在 twig 模板中访问 contentful 属性,例如以下示例

 {{ entry.[fieldname].value }}

Markdown 转换为 html

 {{ entry.[markdownFieldName].value | Markdown }}

图片缩放

{{ contentfulImage(entry.[assetFieldName].value, int width, int height) }}

字段

文本(默认)

{{ entry.[assetFieldName].type }} // 'Text'
{{ entry.[assetFieldName].value }} // Value

布尔值

{{ entry.[assetFieldName].type }} // 'Boolean'
{{ entry.[assetFieldName].value }} // Value

资产

{{ entry.[assetFieldName].type }} // 'Asset'
{{ entry.[assetFieldName].value }} // Url of asset
{{ entry.[assetFieldName].title }}
{{ entry.[assetFieldName].description }}

数组

{{ entry.[assetFieldName].type }} // 'Array'
{{ entry.[assetFieldName].value }} // Array of fields

引用

{{ entry.[assetFieldName].type }} // 'Reference'
{{ entry.[assetFieldName].value }} // ContentfulEntryId

对象(Json)

{{ entry.[assetFieldName].type }} // 'Object'
{{ entry.[assetFieldName].value }} // ContentfulEntryId

# Pages
- If the contentful entry has a "Indentifier" field (URL) it will be imported as page with the given route via IdentifierImporterPlugin.
- Add an additional ResourceCreator to add custom logic to a special contentful entry type.
- More documentation soon