Skip to content

Commit

Permalink
Add concrete examples data handler
Browse files Browse the repository at this point in the history
  • Loading branch information
PululuK committed Mar 4, 2022
1 parent 2165e5d commit 150b3c0
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 35 deletions.
7 changes: 3 additions & 4 deletions democustomfields17.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function symfonyContainerInstance()
return $this->symfonyInstance;
}

private function getProductAdminHookFieldsDefinition(HookFieldsBuilderInterface $hookFieldsBuilder, array $datas)
private function getProductAdminHookFieldsDefinition(HookFieldsBuilderInterface $hookFieldsBuilder, array $data)
{
$formFactory = $this->symfonyContainerInstance()->get('form.factory');
$options = [
Expand All @@ -96,15 +96,14 @@ private function getProductAdminHookFieldsDefinition(HookFieldsBuilderInterface
'module' => $this,
];

return $formFactory->createNamed($this->name, Democustomfields17AdminForm::class, $datas, $options);
return $formFactory->createNamed($this->name, Democustomfields17AdminForm::class, $data, $options);
}

private function displayProductAdminHookFields(HookFieldsBuilderInterface $hookFieldsBuilder, array $params)
{
$productFieldsData = (new ProductFormDataHandler())->getData($params);

$form = $this->getProductAdminHookFieldsDefinition($hookFieldsBuilder, $productFieldsData);

return $this->symfonyContainerInstance()
->get('twig')
->render('@PrestaShop/'.$this->name.'/admin/product/customfields.html.twig', [
Expand Down
10 changes: 10 additions & 0 deletions sql/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'democustomfields17` (
`id_democustomfields17` int(11) NOT NULL AUTO_INCREMENT,
`id_product` int(11) unsigned NOT NULL,
`my_switch_field_example` tinyint(1) unsigned NOT NULL DEFAULT "0",
`my_text_field_example` text DEFAULT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
PRIMARY KEY (`id_democustomfields17`),
KEY `id_product` (`id_product`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'democustomfields17_lang` (
`id_democustomfields17` int(11) unsigned NOT NULL,
`id_shop` INT(11) UNSIGNED NOT NULL DEFAULT "1",
`id_lang` int(11) unsigned NOT NULL ,
`my_translatable_text_field_example` text DEFAULT NULL,
PRIMARY KEY (`id_democustomfields17`, `id_shop`, `id_lang`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

foreach ($sql as $query) {
if (Db::getInstance()->execute($query) == false) {
return false;
Expand Down
1 change: 1 addition & 0 deletions sql/uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
$sql = array();

$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'democustomfields17`';
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'democustomfields17_lang`';

foreach ($sql as $query) {
if (Db::getInstance()->execute($query) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,30 @@
use PrestaShop\Module\Democustomfields17\Form\Product\Hooks\HookFieldsBuilderInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use PrestaShopBundle\Form\Admin\Type\CategoryChoiceTreeType;
use PrestaShopBundle\Form\Admin\Type\ChangePasswordType;
use PrestaShopBundle\Form\Admin\Type\CountryChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use PrestaShopBundle\Form\Admin\Type\TranslatableType;
use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TextWithLengthCounterType;
use Module;

class HookDisplayAdminProductsExtraFieldsBuilder implements HookFieldsBuilderInterface
{
public function addFields(FormBuilderInterface $adminFormBuilder, Module $module) :FormBuilderInterface
{
$adminFormBuilder
->add('my_text_type_field_exemple', TextType::class, [
'label' => $module->l('My simple text type'),
'attr' => array(
'class' => 'my-custom-class',
'data-hex'=> 'true'
)
->add('my_text_field_example', TextType::class, [
'label' => $module->l('My text'),
'attr' => [
'class' => 'my-custom-class',
'data-hex'=> 'true'
]
])
->add('my_switch_type_field_exemple', SwitchType::class, [
'label' => $module->l('My switch type'),
'choices' => [
$module->l('ON') => true,
$module->l('OFF') => false,
],
->add('my_switch_field_example', SwitchType::class, [
'label' => $module->l('My switch')
])
->add('my_translatable_type_field_exemple', TranslatableType::class, [
// we'll have text area that is translatable
'label' => $module->l('My translatable type'),
->add('my_translatable_text_field_example', TranslatableType::class, [
'label' => $module->l('My translatable text'),
'type' => TextareaType::class,
'locales' => $module->getLocales()
])
->add('meta_title', TextWithLengthCounterType::class, [
'label' => $module->l('My text with length counter type'),
'max_length' => 255,
])
->add('category_id', CategoryChoiceTreeType::class, [
'label' => $module->l('My categorytree type'),
'disabled_values' => [4, 5], // Recommantion : Use something look $module->getDisabledCategoriesIds()
]);

return $adminFormBuilder;
Expand Down
13 changes: 12 additions & 1 deletion src/Form/Product/ProductFormDataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function save(array $data): bool{
$idProduct = (int) $data['id_product'];
$productCustomFields = ProductCustomFieldsFactory::create($idProduct);
$productCustomFields->id_product = $idProduct;
$productCustomFields->my_switch_field_example = (bool) $data['my_switch_field_example'];
$productCustomFields->my_translatable_text_field_example = $data['my_translatable_text_field_example'];
$productCustomFields->my_text_field_example = $data['my_text_field_example'];

try {
if($productCustomFields->save()){
Expand All @@ -27,10 +30,18 @@ public function save(array $data): bool{
}

public function getData(array $params): array{
$productCustomFields = ProductCustomFieldsFactory::create((int)$params['id_product']);
$productCustomFields = ProductCustomFieldsFactory::create(
(int)$params['id_product'],
$params['id_lang'] ?? null,
$params['id_shop'] ?? null
);

return [
'id' => $productCustomFields->id,
'id_product' => $productCustomFields->id_product,
'my_switch_field_example' => (bool) $productCustomFields->my_switch_field_example,
'my_text_field_example' => $productCustomFields->my_text_field_example,
'my_translatable_text_field_example' => $productCustomFields->my_translatable_text_field_example,
];
}
}
39 changes: 36 additions & 3 deletions src/Model/ProductCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class ProductCustomFields extends ObjectModel {
/** @var int product ID */
public $id_product;

/** @var string */
public $my_text_field_example;

/** @var string */
public $my_translatable_text_field_example;

/** @var bool */
public $my_switch_field_example;

/** @var string Object creation date */
public $date_add;

Expand All @@ -25,10 +34,34 @@ class ProductCustomFields extends ObjectModel {
public static $definition = [
'table' => 'democustomfields17',
'primary' => 'id_democustomfields17',
'multilang' => true,
'multilang_shop' => true,
'fields' => [
'id_product' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedId'],
'date_add' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'date_upd' => ['type' => self::TYPE_DATE, 'validate' => 'isDate'],
'id_product' => [
'type' => self::TYPE_INT,
'validate' => 'isUnsignedId'
],
'my_text_field_example' => [
'type' => self::TYPE_HTML,
'validate' => 'isCleanHtml'
],
'my_translatable_text_field_example' => [
'type' => self::TYPE_HTML,
'lang' => true,
'shop' => true,
'validate' => 'isCleanHtml'
],
'my_switch_field_example' => [
'type' => self::TYPE_BOOL
],
'date_add' => [
'type' => self::TYPE_DATE,
'validate' => 'isDate'
],
'date_upd' => [
'type' => self::TYPE_DATE,
'validate' => 'isDate'
],
],
];

Expand Down

0 comments on commit 150b3c0

Please sign in to comment.