marvin255 / bxcontent
Requires
- php: >=5.4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~2.2
- phpunit/phpunit: ~4.8
README
为内容管理器提供创建复杂片段的功能,可以包含复杂的HTML。
目录
安装
使用 Composer
-
将以下内容添加到您的 composer.json 文件的
require部分"require": { "marvin255/bxcontent": "~0.4" }
-
如果您需要通过 composer 自动更新库,请将以下内容添加到
scripts部分"scripts": [ { "post-install-cmd": "\\marvin255\\bxcontent\\installer\\Composer::injectModule", "post-update-cmd": "\\marvin255\\bxcontent\\installer\\Composer::injectModule", } ]
-
在项目根目录的终端中执行以下命令
composer update -
如果未执行第2步,则将
vendors/marvin255/bxcontent/marvin255.bxcontent文件夹复制到您的项目local/modules文件夹中。 -
在1С-Битрикс "网站管理" 的管理部分安装模块。
普通
- 下载包含存储库的存档。
- 将存档中的
marvin255.bxcontent文件夹复制到您的项目local/modules文件夹中。 - 在1С-Битрикс "网站管理" 的管理部分安装模块。
使用
在管理部分,将出现新的属性 HTML构造器。相应地,要使用构造器,需要为所需的信息块元素创建用户属性类型 HTML构造器。之后,在元素编辑页面将出现构造器。
构造器的基本单位是片段。每个片段都必须定义为实现 \marvin255\bxcontent\snippets\SnippetInterface 接口的对象。片段有两个主要字段:片段名称和控制元素数组。
作为控件应设置实现 \marvin255\bxcontent\controls\ControlInterface 接口的对象。对于每个控件,必须指定:类型(传递给js以正确显示)、名称(控件值将创建并传递到数据库中,名称)和名称(将在界面中显示)。此外,可以将每个控件设置为多选,如果指定了参数 multiple,则该控件将返回一个数组作为值,并在构造器中为该控件显示多个字段。
一个片段可以包含任意数量的不同控件。相应地,片段实际上是控件集合。
系统使用 \marvin255\bxcontent\SnippetManager 类来管理片段类型,通过该类可以获取系统中的所有注册片段并设置新的片段类型。\marvin255\bxcontent\SnippetManager 实现了单例模式,可以通过调用 \marvin255\bxcontent\SnippetManager::getInstance() 获取。
在系统中注册片段
为了使片段出现在系统中,它们必须在 \marvin255\bxcontent\SnippetManager 中注册。为此,存在一个名为 collectSnippets 的事件,其中传递给该事件的唯一参数是 \marvin255\bxcontent\SnippetManager 对象的引用。为了注册片段,不必创建单独的类,可以使用与模块一起提供的通用类 \marvin255\bxcontent\snippets\Base。
注册片段的示例
AddEventHandler('marvin255.bxcontent', 'collectSnippets', 'collectSnippetsHandler'); function collectSnippetsHandler($manager) { //сниппет с текстом и выпадающим списком $manager->set('text_select', new \marvin255\bxcontent\snippets\Base([ 'label' => 'Текст и выпадающий список', 'controls' => [ new \marvin255\bxcontent\controls\Editor([ 'name' => 'description', 'label' => 'Текстовый редактор', ]), new \marvin255\bxcontent\controls\Select([ 'name' => 'class', 'label' => 'Список', 'prompt' => '-', 'list' => [ 'item1' => 'Опция 1', 'item2' => 'Опция 2', ], ]), ], ])); //сниппет со слайдером $manager->set('slider', new \marvin255\bxcontent\snippets\Base([ 'label' => 'Слайдер', 'controls' => [ new \marvin255\bxcontent\controls\Input([ 'name' => 'title', 'label' => 'Заголовок слайдера', ]), new \marvin255\bxcontent\controls\Combine([ 'name' => 'slides', 'label' => 'Слайды', 'multiple' => true, 'elements' => [ new \marvin255\bxcontent\controls\File([ 'name' => 'image', 'label' => 'Файл с изображением', ]), new \marvin255\bxcontent\controls\Input([ 'name' => 'sign', 'label' => 'Подпись', ]), ], ]), ], ])); }
可获取的元素类型
由于所有元素都在js端进行处理,因此它们必须在php和js中描述。因此,目前它们的数量是有限的。
-
\marvin255\bxcontent\controls\Input- 输入普通文本的字符串, -
\marvin255\bxcontent\controls\Editor- wysiwyg 编辑器, -
\marvin255\bxcontent\controls\Textarea- 输入多行文本的 textarea 字段, -
\marvin255\bxcontent\controls\File- 允许在 Bitrix 文件系统中选择或上传文件的字段, -
\marvin255\bxcontent\controls\Image- 允许在 Bitrix 文件系统中选择或上传图像的字段, -
\marvin255\bxcontent\controls\Select- 允许选择有限数量选项的字段 (select), -
\marvin255\bxcontent\controls\Combine- 可以通过它来组合其他多个字段的字段。
结果表示
结果可以以开放部分的形式表示,无论是通过在输出字段构造器的每个模板中处理获得的json,还是通过为代码片段指定的表示更集中化。
在创建每个代码片段时,可以指定一个实现接口 \marvin255\bxcontent\views\ViewInterface 的对象,该对象将负责显示该特定代码片段。
AddEventHandler('marvin255.bxcontent', 'collectSnippets', 'collectSnippetsHandler'); function collectSnippetsHandler($manager) { //получаем объект приложения битрикса для отображения компонентов global $APPLICATION; //сниппет с текстом и выпадающим списком $manager->set('text_select', new \marvin255\bxcontent\snippets\Base([ 'view' => new \marvin255\bxcontent\views\Component($APPLICATION, 'custom:slider'), 'label' => 'Текст и выпадающий список', 'controls' => [ new \marvin255\bxcontent\controls\Editor([ 'name' => 'description', 'label' => 'Текстовый редактор', ]), new \marvin255\bxcontent\controls\Select([ 'name' => 'class', 'label' => 'Список', 'prompt' => '-', 'list' => [ 'item1' => 'Опция 1', 'item2' => 'Опция 2', ], ]), ], ])); }
因此,要输出为所有代码片段生成的html,只需调用代码片段管理器的 render 方法。例如,对于组件 bitrix:news.detail,它看起来像这样
//template.php echo \marvin255\bxcontent\SnippetManager::getInstance()->render($arResult['PROPERTIES']['html_constructor_property']['VALUE']);
或者,如果指定了显示属性
//template.php echo $arResult['DISPLAY_PROPERTIES']['html_constructor_property']['DISPLAY_VALUE'];
添加自己的js和样式
代码片段管理器具有添加js和css的功能。
例如,在事件中,不仅可以指定特定的代码片段,还可以指定为其指定的脚本和样式
AddEventHandler('marvin255.bxcontent', 'collectSnippets', 'collectSnippetsHandler'); function collectSnippetsHandler($manager) { $manager->addJs('/bitrix/js/marvin255.bxcontent/controls.js'); $manager->addCss('/bitrix/css/marvin255.bxcontent/plugin.css'); }
代码片段的集合
一些代码片段预先定义并添加到库中。来自集合的代码片段必须继承自 \marvin255\bxcontent\packs\Pack 类。这样的代码片段可以通过函数 \marvin255\bxcontent\packs\Pack::setTo 添加到管理器,该函数的第一个参数是管理器对象,第二个参数是代码片段的配置数组。此外,来自集合的代码片段还具有内置的显示方法。
use marvin255\bxcontent\packs\bootstrap; AddEventHandler('marvin255.bxcontent', 'collectSnippets', 'collectSnippetsHandler'); function collectSnippetsHandler($manager) { bootstrap\Carousel::setTo($manager, ['label' => 'кастомное название сниппета перепишет то, что задано в сниппете']); bootstrap\Collapse::setTo($manager); bootstrap\Media::setTo($manager); bootstrap\Tabs::setTo($manager); }
Bootstrap 代码片段集合
\marvin255\bxcontent\packs\bootstrap\Carousel - Bootstrap 的滑块.
\marvin255\bxcontent\packs\bootstrap\Collapse - Bootstrap 的折叠面板.
\marvin255\bxcontent\packs\bootstrap\Tabs - Bootstrap 的标签页.
\marvin255\bxcontent\packs\bootstrap\Media - Bootstrap 的媒体对象.