feat(main): Add FAQ accordion
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Websmid\CustomHtml\Form\DataHandler;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataHandler\FormDataHandlerInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
@@ -13,6 +14,7 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
$block = new CustomHtmlBlock();
|
||||
$block->id_ws_customhtml_group = (int) $formData['id_ws_customhtml_group'];
|
||||
$block->title = (string) $formData['title'];
|
||||
$block->type = (string) $formData['type'];
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->active = (int) $formData['active'];
|
||||
@@ -22,6 +24,8 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->saveFaqs((int) $block->id, $formData['faqs'] ?? []);
|
||||
|
||||
return $block->id;
|
||||
}
|
||||
|
||||
@@ -35,10 +39,35 @@ final class CustomHtmlBlockDataHandler implements FormDataHandlerInterface
|
||||
|
||||
$block->id_ws_customhtml_group = (int) $formData['id_ws_customhtml_group'];
|
||||
$block->title = (string) $formData['title'];
|
||||
$block->type = (string) $formData['type'];
|
||||
$block->show_title = (int) $formData['show_title'];
|
||||
$block->content = (string) $formData['content'];
|
||||
$block->active = (int) $formData['active'];
|
||||
|
||||
return (bool) $block->update();
|
||||
if (!$block->update()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->saveFaqs((int) $id, $formData['faqs'] ?? []);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function saveFaqs(int $idBlock, array $faqs): void
|
||||
{
|
||||
CustomHtmlFaq::deleteByBlockId($idBlock);
|
||||
|
||||
foreach (array_values($faqs) as $position => $faqData) {
|
||||
if (empty($faqData['question'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$faq = new CustomHtmlFaq();
|
||||
$faq->id_ws_customhtml = $idBlock;
|
||||
$faq->question = (string) $faqData['question'];
|
||||
$faq->answer = (string) ($faqData['answer'] ?? '');
|
||||
$faq->ordering = $position;
|
||||
$faq->add();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Websmid\CustomHtml\Form\DataProvider;
|
||||
|
||||
use PrestaShop\PrestaShop\Core\Form\IdentifiableObject\DataProvider\FormDataProviderInterface;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
||||
use Validate;
|
||||
|
||||
final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
|
||||
@@ -19,8 +20,10 @@ final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
|
||||
return [
|
||||
'id_ws_customhtml_group' => (int) $block->id_ws_customhtml_group,
|
||||
'title' => $block->title,
|
||||
'type' => $block->type,
|
||||
'show_title' => (bool) $block->show_title,
|
||||
'content' => $block->content,
|
||||
'faqs' => $this->loadFaqs((int) $id),
|
||||
'active' => (bool) $block->active,
|
||||
];
|
||||
}
|
||||
@@ -30,9 +33,21 @@ final class CustomHtmlBlockDataProvider implements FormDataProviderInterface
|
||||
return [
|
||||
'id_ws_customhtml_group' => 0,
|
||||
'title' => '',
|
||||
'type' => 'html',
|
||||
'show_title' => false,
|
||||
'content' => '',
|
||||
'faqs' => [],
|
||||
'active' => true,
|
||||
];
|
||||
}
|
||||
|
||||
private function loadFaqs(int $idBlock): array
|
||||
{
|
||||
return array_map(static function (array $faq) {
|
||||
return [
|
||||
'question' => $faq['question'],
|
||||
'answer' => $faq['answer'],
|
||||
];
|
||||
}, CustomHtmlFaq::getByBlockId($idBlock));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ namespace Websmid\CustomHtml\Form\Type;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use PrestaShop\PrestaShop\Core\Form\ConfigurableFormChoiceProviderInterface;
|
||||
use PrestaShopBundle\Form\Admin\Type\SwitchType;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Websmid\CustomHtml\Config\Config;
|
||||
|
||||
class CustomHtmlBlockType extends AbstractType
|
||||
{
|
||||
@@ -37,6 +39,11 @@ class CustomHtmlBlockType extends AbstractType
|
||||
'label' => $this->translator->trans('Title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => true,
|
||||
])
|
||||
->add('type', ChoiceType::class, [
|
||||
'label' => $this->translator->trans('Block type', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'choices' => array_flip(Config::BLOCK_TYPES),
|
||||
'required' => true,
|
||||
])
|
||||
->add('show_title', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Show title', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
@@ -45,6 +52,15 @@ class CustomHtmlBlockType extends AbstractType
|
||||
'label' => $this->translator->trans('Content', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
'attr' => ['class' => 'autoload_rte'],
|
||||
'row_attr' => ['class' => 'customhtmlblock-type-field customhtmlblock-type-html'],
|
||||
])
|
||||
->add('faqs', CollectionType::class, [
|
||||
'entry_type' => CustomHtmlFaqType::class,
|
||||
'allow_add' => true,
|
||||
'allow_delete' => true,
|
||||
'by_reference' => false,
|
||||
'prototype' => true,
|
||||
'required' => false,
|
||||
])
|
||||
->add('active', SwitchType::class, [
|
||||
'label' => $this->translator->trans('Active', [], 'Modules.Wscustomhtml.Admin'),
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Websmid\CustomHtml\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class CustomHtmlFaqType extends AbstractType
|
||||
{
|
||||
private $translator;
|
||||
|
||||
public function __construct(TranslatorInterface $translator)
|
||||
{
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $formBuilder, array $options): void
|
||||
{
|
||||
$formBuilder
|
||||
->add('question', TextType::class, [
|
||||
'label' => $this->translator->trans('Question', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
])
|
||||
->add('answer', TextareaType::class, [
|
||||
'label' => $this->translator->trans('Answer', [], 'Modules.Wscustomhtml.Admin'),
|
||||
'required' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user