File: D:/HostingSpaces/SBogers10/ste.komma.pro/app/Components/ComponentType/Types/TextUsp.php
<?php
namespace App\Components\ComponentType\Types;
use App\Components\Component\ViewComponent;
use App\Components\ComponentType\ComponentTypes;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\OnOff;
use Komma\KMS\Core\Attributes\TextArea;
use Komma\KMS\Core\Attributes\TextField;
class TextUsp extends AbstractComponentType
{
//Set an id that corresponds to the ComponentTypes enum
protected $id = ComponentTypes::TEXT_USP;
//Set icon and name
protected $name = 'text-usp';
// Number of tabs that can be filled
const POSSIBLE_AMOUNT = 5;
public function __construct()
{
parent::__construct();
$this->attributes->push((new TextArea())
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromComponent, 'text')
);
// Make the tabs
for($i = 0; $i < self::POSSIBLE_AMOUNT; $i++) {
$this->attributes->push(
(new TextField('USP ' . ($i + 1)))
->mapValueFrom(Attribute::ValueFromComponent,'label_' . ($i + 1))
->setDataAttribute('tab', ($i + 1)),
);
}
$this->attributes->push((new OnOff())
->setLabelText('Omwisselen tekst / USP')
->mapValueFrom(Attribute::ValueFromComponent, 'reversed')
);
}
public function prepare(ViewComponent $viewComponent)
{
$tabs = collect();
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$label = $viewComponent->{'label_'.$i};
if(!empty($label)) {
$tab = (object)[
'id' => $i,
'label' => $label,
];
$tabs->push($tab);
}
unset($viewComponent->{'label_'.$i});
}
$viewComponent->usps = $tabs;
}
}