File: D:/HostingSpaces/SBogers10/wingssprayer.komma.pro/app/Components/ComponentType/Types/Trophies.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\Documents;
use Komma\KMS\Core\Attributes\Models\ImageProperty;
use Komma\KMS\Core\Attributes\TextArea;
use Komma\KMS\Core\Attributes\TextField;
class Trophies extends AbstractComponentType
{
const POSSIBLE_AMOUNT = 4;
public function __construct()
{
//Set an id that corresponds to the ComponentTypes enum
$this->id = ComponentTypes::TROPHIES;
//Set icon and name
$this->name = 'trophies';
parent::__construct();
$this->attributes->push((new TextField(__('kms/attributes/components.title')))
->mapValueFrom(Attribute::ValueFromComponent, 'title')
);
$this->attributes->push((new TextArea())
->enableTinymceEditor()
->mapValueFrom(Attribute::ValueFromComponent, 'description')
);
$this->attributes->push((new TextField(__('kms/attributes/components.button_link')))
->mapValueFrom(Attribute::ValueFromComponent, 'button_link')
);
$this->attributes->push((new TextField(__('kms/attributes/components.button_label')))
->mapValueFrom(Attribute::ValueFromComponent, 'button_label')
);
for($i = 0; $i < self::POSSIBLE_AMOUNT; $i++) {
$this->attributes->push((new Documents())
->setLabelText(__('kms/attributes/components.image'))
->onlyAllowImages()
->setSmallDragAndDropArea()
->setSubFolder('component_uploads')
->setMaxDocuments(1)
->setImageProperties([
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setHeight(200),
])
->mapValueFrom(Attribute::ValueFromComponent, 'image_' . ($i + 1))//Needed to sustain Attribute integrity.
->setDataAttribute('tab', ($i + 1))
);
$this->attributes->push((new TextField('Name'))
->mapValueFrom(Attribute::ValueFromComponent, 'name_' . ($i + 1))//Needed to sustain Attribute integrity.
->setDataAttribute('tab', ($i + 1))
);
}
}
public function prepare(ViewComponent $viewComponent)
{
$trophies = collect();
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$name = $viewComponent->{'name_'.$i};
$image = $viewComponent->{'image_'.$i};
if(!empty($name)) {
$step = (object)[
'id' => $i,
'name' => $name,
'image' => $image ?? null
];
$trophies->push($step);
}
unset($viewComponent->{'name_'.$i});
unset($viewComponent->{'image_'.$i});
}
$viewComponent->trophies = $trophies;
}
}