HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/rentman2019.komma.pro/app/Komma/Dynamic/ComponentType/Types/Faq.php
<?php

namespace App\Komma\Dynamic\ComponentType\Types;

use App\Komma\Dynamic\Component\ViewComponent;
use App\Komma\Dynamic\ComponentType\ComponentTypes;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\TextArea;
use App\Komma\Kms\Core\Attributes\TextField;

class Faq extends AbstractComponentType
{
    private $faqItemCount = 10;

    public function __construct()
    {
        parent::__construct();

        //Set an id that corresponds to the ComponentTypes enum
        $this->id = ComponentTypes::FAQ;

        //Set icon and name
        $this->name = 'faq';

        $this->attributes->push((new TextField('label'))
            ->mapValueFrom(Attribute::ValueFromItself, 'label'));

        $this->attributes->push((new TextField('title'))
            ->mapValueFrom(Attribute::ValueFromItself, 'title'));


        for ($i = 1; $i <= $this->faqItemCount; $i++) {
            $this->translatableAttributes[] = 'question_'.$i;
            $this->translatableAttributes[] = 'answer_'.$i;

            $this->attributes->push((new TextField('Question'))
                ->setPlaceholderText('Can I upgrade to a new license?')
                ->mapValueFrom(Attribute::ValueFromItself, 'question_'.$i)//Needed to sustain Attribute integrity.
                ->setDataAttribute('tab', $i));


            $this->attributes->push((new TextArea())
                ->setLabelText('Answer')
                ->setPlaceholderText('Yes, it is possible for all existing users')
                ->mapValueFrom(Attribute::ValueFromItself, 'answer_'.$i)//Needed to sustain Attribute integrity.
                ->setDataAttribute('tab', $i));
        }
    }

    public function prepare(ViewComponent $component): ViewComponent
    {
        $faqItems = [];

        // Loop to populate the arrays
        // Assuming the number of questions and answers match
        for ($i = 1; $i <= $this->faqItemCount; $i++) {
            $question = $component->{'question_'.$i};
            $answer = $component->{'answer_'.$i};

            // Check if the question and answer are not empty
            if (! empty($question) && ! empty($answer)) {
                $faqItems[] = [
                    'question' => $question,
                    'answer' => $answer,
                ];
            }
        }

        $component->faqItems = $faqItems;

        return $component;
    }
}