承接 violet88/silverstripe-shortcodable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

violet88/silverstripe-shortcodable

Composer 安装命令:

composer require violet88/silverstripe-shortcodable

包简介

Provides a popup for inserting and editing shortcodes in the SilverStripe CMS.

README 文档

README

Screenshot

Provides a popup for inserting and editing shortcodes in the SilverStripe CMS. Shortcodes are small pieces of code inside square brackets that trigger a larger function. This module allows you to define your own DataObjects and ViewableData as shortcodes, and insert them into the HTMLEditorField in the CMS.

Requirements

  • SilverStripe ^5
  • PHP ^8.1

Note: This module may work with SilverStripe 4 and PHP 7.4, but it is not officially supported. Please use sheadawson/silverstripe-shortcodable for SilverStripe 4 and below.

Installation

Install the module using composer.

composer require violet88/silverstripe-shortcodable

Configuration

Defining Shortcodes

Define your shortcodes in a yml config file. You can define shortcodes for DataObjects and ViewableData classes.

Violet88\Shortcodable\Shortcodable:
    shortcodable_classes:
        - MyDataObject
        - MyViewableData

Shortcodable classes may be namespaced, but at the moment using duplicate class names in different namespaces is not supported and may lead to unexpected results. This is a known issue and will be addressed in a future release.

Parsing Shortcodes

In order to be able to parse the shortcodes, the parse_shortcode method must be available in the class. This method should return the HTML that will replace the shortcode in the frontend.

use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;

class MyDataObject extends DataObject
{
    private static array $db = [
        'Title' => 'Varchar',
        'Content' => 'HTMLText',
    ];

    public static function parse_shortcode($arguments, $content, $parser, $tagName)
    {
        $object = self::get()->byID($arguments['id']);

        return ArrayData::create([
            'Title' => $object->Title,
            'Content' => $object->Content,
        ])->renderWith('MyDataObject');
    }
}

Note: The parse_shortcode method doesn't need to use template rendering. It can return any string.

Note: The parse_shortcode method must be a static one. All shortcodes contain an 'id' argument with which the object can be fetched.

Additional Shortcode Attributes

You can define additional attributes for your shortcodes using the shortcode_fields config array. These attributes will be available in the shortcode popup when inserting or editing a shortcode.

In this array you can also define how the attribute should be rendered in the popup. All standard HTML input types are supported, as well as a select and radiogroup type.

The schema for the shortcode_fields array is as follows:

{
    "attribute_name": {
        "type": "text",
        "label": "Attribute label", // Optional
        "placeholder": "Attribute placeholder", // Optional
        "options": {
            // Only for select and radiogroup types
            "option1": "Option 1",
            "option2": "Option 2"
        }
    }
}

This schema can translate to the following shortcode_fields array in PHP:

use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;

class MyDataObject extends DataObject
{
    private static array $db = [
        'Title' => 'Varchar',
        'Content' => 'HTMLText',
    ];

	private static $shortcode_fields = [
		'Title' => [
			'type' => 'text',
            'label' => 'Title for video',
			'placeholder' => 'Enter a title'
		],
        'Content' => [
            'type' => 'textarea',
            'placeholder' => 'Description for video'
        ],
        'Type' => [
            'type' => 'select',
            'options' => [
                'youtube' => 'YouTube',
                'vimeo' => 'Vimeo'
            ]
        ]
	];

    public static function parse_shortcode($arguments, $content, $parser, $tagName)
    {
        $object = self::get()->byID($arguments['id']);

        return ArrayData::create([
            'Title' => $object->Title,
            'Content' => $object->Content,
        ])->renderWith('MyDataObject');
    }
}

Note: Since the shortcode_fields attribute is static and cannot be changed, we've added the ability to define the options for a select or radiogroup type using a method. This method should return an array of options.

private static $shortcode_fields = [
    'Type' => [
        'type' => 'select',
        'options' => 'getTypes'
    ]
];

public function getTypes()
{
    return [
       'youtube' => 'YouTube',
       'vimeo' => 'Vimeo'
    ];
}

If you're looking for something easier to set up, an array of field names can be used instead of the full schema. This will default to a text input type with the field name as the placeholder.

use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;

class MyDataObject extends DataObject
{
    private static array $db = [
        'Title' => 'Varchar',
        'Content' => 'HTMLText',
    ];

    private static $shortcode_fields = [
        'Title',
        'Content',
        'Type'
    ];

    public static function parse_shortcode($arguments, $content, $parser, $tagName)
    {
        $object = self::get()->byID($arguments['id']);

        return ArrayData::create([
            'Title' => $object->Title,
            'Content' => $object->Content,
        ])->renderWith('MyDataObject');
    }
}

CMS Usage

Once installed a new icon will appear in the CMS editor toolbar. It looks like this: icon

This button shows a popup that can be used to create a new shortcode:

popup

Shortcodes can also be edited by clicking on them in the editor, or removed by pressing the backspace key when the cursor is at the beginning of the shortcode:

edit

Validity

Shortcodes are only valid if they are in the format [shortcode] or [shortcode attribute="value"]. If the shortcode is not valid, it will be highlighted in red and removed when the editor is saved:

Valid Shortcode Invalid Shortcode
Valid Shortcode Invalid Shortcode

Upgrading from sheadawson/silverstripe-shortcodable

If you are upgrading from sheadawson/silverstripe-shortcodable the following steps can be used to get you back on track:

  1. Remove the old module using composer:

    composer remove sheadawson/silverstripe-shortcodable
  2. Install the new module using composer:

    composer require violet88/silverstripe-shortcodable
  3. Update your config namespaces from Silverstripe\Shortcodable to Violet88\Shortcodable:

    - Silverstripe\Shortcodable:
    + Violet88\Shortcodable\Shortcodable:
      shortcodable_classes:
        - MyDataObject
        - MyViewableData
  4. Update your class namespaces from Shortcodable to Shortcodable:

    - use SilverStripe\Shortcodable;
    + use Violet88\Shortcodable\Shortcodable;
    
    - use SilverStripe\Shortcodable\Controller\ShortcodableController;
    + use Violet88\Shortcodable\Controllers\ShortcodableController;
  5. Remove references to the ShortcodableHtmlEditorField and ShortcodableShortcodeParserExtension extensions. These no longer exist in the new module.

  6. Update your getShortcodeFields method to use the new shortcode_fields config array. This array can be used to define additional attributes for your shortcodes.

    - public function getShortcodeFields() {
    -     return FieldList::create([
    -         TextField::create('Title', 'Title for video'),
    -         TextareaField::create('Content', 'Description for video'),
    -         DropdownField::create('Type', 'Type', [
    -             'youtube' => 'YouTube',
    -             'vimeo' => 'Vimeo'
    -         ])
    -     ]);
    - }
    + private static $shortcode_fields = [
    +     'Title' => [
    +         'type' => 'text',
    +         'placeholder' => 'Title for video'
    +     ],
    +     'Content' => [
    +         'type' => 'textarea',
    +         'placeholder' => 'Description for video'
    +     ],
    +     'Type' => [
    +         'type' => 'select',
    +         'options' => [
    +             'youtube' => 'YouTube',
    +             'vimeo' => 'Vimeo'
    +         ]
    +     ]
    + ]

violet88/silverstripe-shortcodable 适用场景与选型建议

violet88/silverstripe-shortcodable 是一款 基于 JavaScript 开发的 Composer 扩展包,目前已累计 201 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「silverstripe」 「shortcode」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 violet88/silverstripe-shortcodable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 violet88/silverstripe-shortcodable 我们能提供哪些服务?
定制开发 / 二次开发

基于 violet88/silverstripe-shortcodable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 201
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 37
  • 开发语言: JavaScript

其他信息

  • 授权协议: 0BSD
  • 更新时间: 2024-03-13