PHP code example of think.studio / nova-flexible-content-field-shortcode

1. Go to this page and download the library: Download think.studio/nova-flexible-content-field-shortcode library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

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


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(),

use ThinkOne\NovaFlexibleContentFieldShortcode\ViewPresenter;

class ImageWithCaption extends ViewPresenter
{
    protected string $viewPath = 'my-folder.shortcodes.image-with-caption';
}

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 ) );
    }
}

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
        );
    }
}
blade.php
# 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
balde.php
{!! $article->full_content !!}
{!! $article->full_content_other !!}