kiberden/content

dev-master 2021-04-12 11:57 UTC

This package is not auto-updated.

Last update: 2024-09-25 02:55:34 UTC


README

Latest Stable Version License Build Status

用于内容管理员的复杂信息块元素的额外字段,可以创建包含复杂HTML的复杂片段。

目录

安装

使用 Composer

  1. 将以下内容添加到您的 composer.json 文件的 require 部分

    "require": {
        "marvin255/bxcontent": "~0.4"
    }
  2. 如果需要通过 composer 自动更新库,请将以下内容添加到 scripts 部分

    "scripts": [
        {
            "post-install-cmd": "\\marvin255\\bxcontent\\installer\\Composer::injectModule",
            "post-update-cmd": "\\marvin255\\bxcontent\\installer\\Composer::injectModule",
        }
    ]
  3. 在您的项目目录的命令行中执行以下操作

    composer update
    
  4. 如果未执行第2步,请将 vendors/marvin255/bxcontent/marvin255.bxcontent 文件夹复制到您的项目目录的 local/modules 文件夹中。

  5. 在1С-Битрикс "网站管理" 的管理部分安装模块。

常规

  1. 下载包含仓库的存档。
  2. 将存档中的 marvin255.bxcontent 文件夹复制到您的项目目录的 local/modules 文件夹中。
  3. 在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' => 'Подпись',
                    ]),
                ],
            ]),
        ],
    ]));
}

可用的控件类型

由于所有控件都在客户端处理,因此它们必须在 php 和 js 中进行描述。因此,目前它们的数量有限

  1. \marvin255\bxcontent\controls\Input - 普通文本输入框

  2. \marvin255\bxcontent\controls\Editor - wysiwyg 编辑器

  3. \marvin255\bxcontent\controls\Textarea - 多行文本输入框(textarea)

  4. \marvin255\bxcontent\controls\File - 允许在 Bitrix 文件系统中选择或上传文件的字段

  5. \marvin255\bxcontent\controls\Image - 允许在Bitrix文件系统中选择或上传图像的字段,

  6. \marvin255\bxcontent\controls\Select - 具有限定选项供选择的字段(选择框),

  7. \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的媒体对象