File: D:/HostingSpaces/SBogers10/eleo.komma.nl/app/Components/Types/FeaturedPosts.php
<?php
namespace App\Components\Types;
use App\Buttons\Kms\ButtonService;
use App\Buttons\Models\Button;
use App\Components\ComponentTypes;
use App\Posts\PostService;
use Komma\KMS\Components\Component\ViewComponent;
use Komma\KMS\Components\ComponentType\Types\AbstractComponentType;
use Komma\KMS\Core\Attributes\MultiSelect;
use Komma\KMS\Components\ComponentType\Types\Text as KMSText;
use Komma\KMS\Core\Attributes\Numeric;
use Komma\KMS\Core\Attributes\TextField;
class FeaturedPosts extends AbstractComponentType
{
protected int $id = ComponentTypes::FEATURED_POSTS;
protected string $name = 'featured-posts';
public function defineAttributesAndTabs()
{
$buttonsService = \App::make(ButtonService::class);
$selectOptions = $buttonsService->getOptionsForSelect(false, true);
$this->addItems([
(new TextField())
->setLabelText(__('KMS::attributes/components.title'))
->setReference('header'),
(new Numeric())
->setLabelText('Aantal nieuwsberichten')
->setValue(3)
->setWholeMin(1)
->setWholeMax(8)
->setReference('amount_of_posts')
->setExplanation('Laatste berichten (op datum)'),
(new MultiSelect())
->setLabelText('Knop')
->setMaxItemsToSelect(1)
->setItems($selectOptions->toArray())
->canBeLinkedWith(Button::class)
->setReference( 'button'),
]);
}
public function prepare(ViewComponent $viewComponent)
{
$amountOfPosts = (int) $viewComponent->amount_of_posts;
/** @var PostService $postService */
$postService = app(PostService::class);
$viewComponent->posts = $postService->getAmountOfPosts($amountOfPosts);
return $viewComponent;
}
}