think.studio/nova-flexible-content-field-shortcode 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

think.studio/nova-flexible-content-field-shortcode

Composer 安装命令:

composer require think.studio/nova-flexible-content-field-shortcode

包简介

Designed specifically for the ability to add shortcodes to existing content.

README 文档

README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

A very highly targeted package. Designed specifically for the ability to add shortcodes to existing content. If you are just starting to develop an application then just use think.studio/nova-flexible-content or alternative without using this package. The package is needed if you already have a large amount of content, and the client requires adding new functionality

Nova Package
V1 V1 V2
V4 V3

Installation

You can install the package via composer:

  1. Install "flexible content" package.
composer require think.studio/nova-flexible-content
  1. Install "shortcode" package
composer require think.studio/nova-flexible-content-field-shortcode
# optional publish configs
php artisan vendor:publish --provider="ThinkOne\NovaFlexibleContentFieldShortcode\ServiceProvider" --tag="config"

Usage

Admin

\ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodeText field. You need add it to top level layouts in you flexible content

class ImageLayout extends \NovaFlexibleContent\Layouts\Layout
{
    public function fields(): array
    {
        return [
            \ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodeText::make('Shortcode')
            ->help('All parameters you can find <a href="//some-link">here</a>'),
            NLFMImage::make('Image', 'image'),
            Text::make('Caption', 'caption'),
        ];
    }
}

class ImageLayout extends \NovaFlexibleContent\Layouts\Layout
{
    public function fields(): array
    {
        return [
           \ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodeText::make('Shortcode'),
            NLFMImage::make('Image', 'image'),
            Text::make('Caption', 'caption'),
        ];
    }
}

class ImagesSliderLayout extends \NovaFlexibleContent\Layouts\Layout
{
    public function fields(): array
    {
        return [
            \ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodeText::make('Shortcode'),
            \NovaFlexibleContent\Flexible::make('Slider', 'slider')
                    ->useLayout(ImageSlideLayout::class)
                    ->layoutsMenuButton('Add slide'),
        ];
    }
}

\NovaFlexibleContent\Flexible::make('Shortcodes', 'shortcodes')
    ->useLayout(ImageLayout::class)
    ->useLayout(ImagesSliderLayout::class)
    ->layoutsMenuButton('Add shortcode')
    ->hideWhenCreating(),

Html presenter

You need create "presenters" to display your shortcodes. Any presenter should implement interface ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodePresenter
Example:

use ThinkOne\NovaFlexibleContentFieldShortcode\ViewPresenter;

class ImageWithCaption extends ViewPresenter
{
    protected string $viewPath = 'my-folder.shortcodes.image-with-caption';
}
# my-folder/shortcodes/image-with-caption.blade.php
@if(!empty($shortcodeData) && !empty($shortcodeData['image']))
    <div class="...">
        <div class="...">
            <img
                class="..."
                src="{{$shortcodeData['image']}}" alt=""/>
            @if(!empty($shortcodeData['caption']))
                <div class="...">
                    {{$shortcodeData['caption']}}
                </div>
            @endif
        </div>
    </div>
@endif

Render example with trait

class Article extends Model {

    use \ThinkOne\NovaFlexibleContentFieldShortcode\Eloquent\HasShortcodes;

    public function shortcodesMap(): array {
        return [ 'image' => ImageWithCaption::class, ];
    }

    public function getFullContentAttribute(): string {
        return $this->getDataWithShortcodes( WysiwygHelper::filter( (string) $this->content ) );
    }
    
    public function getFullContentOtherAttribute(): string {
        return $this->getDataWithShortcodes( WysiwygHelper::filter( (string) $this->content_other ) );
    }
}

Then call it:

{!! $article->full_content !!}
{!! $article->full_content_other !!}

Raw render example

class Post extends Model
{
    public function presentersMap(): array
    {
        return [
            'image'  => ImageWithCaption::class,
            'EYsCY62xKnkHrvIo'  => AdHocImageWithCaption::class,
            'slider' => ImagesSlider::class,
        ];
    }
    
    public function shortcodesData(): array
    {
        $data = null;
        if ($this->shortcodes && is_string($this->shortcodes)) {
            $data = json_decode($this->shortcodes, true);
        }

        return is_array($data) ? $data : [];
    }
    
    public function postContent(): ?string
    {
        return \ThinkOne\NovaFlexibleContentFieldShortcode\ShortcodeCompiler::make(
            $this->shortcodesData(), // array represent "nova-flexible-content" field
            $this->presentersMap() // array represent presenters map (key => class), see above
        )->renderShortcodes(
          $this->content // text containing shortcode, maybe you need to filter it to prevent xss
        );
    }
}

Credits

  • Think Studio

think.studio/nova-flexible-content-field-shortcode 适用场景与选型建议

think.studio/nova-flexible-content-field-shortcode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 think.studio/nova-flexible-content-field-shortcode 我们能提供哪些服务?
定制开发 / 二次开发

基于 think.studio/nova-flexible-content-field-shortcode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-12