This is my shortcode:
add_shortcode('text', function($atts, $content = null) {
$atts = shortcode_atts(
['title' => ''],
$atts,
'text'
);
$title = esc_html($atts['title']);
$data = [
'acf_fc_layout' => 'longform-text',
'title' => $title,
'text' => 'xxx',
];
$module = new LongformText($data);
$module->withAttributes([]);
$html = $module->render();
return $html;
});
But even using withAttributes()
method, I’m getting this error on blade file:
Undefined variable: attributes
<section {{ $attributes->merge(['class' => 'component component-'.$class]) }}>
This is the component controller:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class LongformText extends Component
{
protected $acf;
public $class;
public $content;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($acf = null)
{
$this->acf = $acf;
$this->class = $this->acf['acf_fc_layout'];
$this->content = $this->getContent();
}
protected function getContent()
{
$text = wp_kses_post($this->acf['text']);
$data = [
'title' => esc_html($this->acf['title']),
'text' => apply_filters('the_content', $text),
];
return $data;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.longform-text');
}
}