contextualcode / ezplatform-content-variables
eZ Platform 套件,提供动态内容变量及其管理UI
Requires
- php: >=7.3
- doctrine/migrations: ^3.0
- doctrine/orm: ^2.6
- ezsystems/ezplatform-admin-ui: ^2.2
- ezsystems/ezplatform-kernel: ^1.2
- ezsystems/ezplatform-richtext: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.17
This package is not auto-updated.
Last update: 2024-09-26 01:30:33 UTC
README
此套件提供了一种管理内容变量的方法。基本上,这些是您可以在内容(任何字段类型)中的任何地方使用的占位符。在页面渲染过程中,它们将被实际值替换。
安装
通过
composer
需要contextualcode/ezplatform-content-variables
composer require contextualcode/ezplatform-content-variables
更新
config/routes.yaml
中的路由content_variables: resource: "@EzPlatformContentVariablesBundle/Resources/config/routing.yaml"
运行迁移
php bin/console doctrine:migrations:migrate --configuration=vendor/contextualcode/ezplatform-content-variables/src/bundle/Resources/config/doctrine_migrations.yaml --no-interaction
用法
所有内容变量都分组成集合。因此,首先您需要定义这些集合。要做到这一点,请打开 eZ Platform 管理界面,转到“内容 > 变量”。并创建一个内容变量集合。
集合创建后,您需要添加一些变量。点击集合名称并添加一些内容变量。您需要为每个变量指定以下参数
- 名称
- 标识符
- 值
内容变量有两种类型:静态和回调。默认回调未提供,但创建新的回调非常简单
创建一个新的
AppBundle\ContentVariables\Random
服务(src/AppBundle/ContentVariables/Random.php
),它扩展了ContextualCode\EzPlatformContentVariables\Variable\Value\Callback
。<?php namespace AppBundle\ContentVariables; use ContextualCode\EzPlatformContentVariables\Variable\Value\Callback; class Random extends Callback { protected $identifier = 'random'; public function getValue(): string { return random_int(0, 100); } }
在
AppBundle\ContentVariables\Random
服务中,您需要- 定义
$identifier
属性,它需要在项目范围内是唯一的 - 实现
getValue
函数,它不接受任何参数,应返回一个字符串值。您可以在该函数中使用任何注入的服务。
- 定义
将
AppBundle\ContentVariables\Random
标记为content_variables.value.callback
AppBundle\ContentVariables\Random: tags: [content_variables.value.callback]
这就完成了,现在当您添加/编辑任何内容变量时,您将能够为它选择 random
回调。
创建了一些内容变量后,您可以在任何内容字段中使用它们。为此,请使用带有 #
符号的变量标识符。例如,如果您有一个标识符为 random_var
的变量,请尝试将 #random_var#
添加到某些页面内容中。并检查前端站点访问中编辑的内容。应显示实际的随机数字,而不是 #random_var#
。