<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yaroslawww / 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
);
}
}