Jump to content

Add image field in category (back-office)


Recommended Posts

Hi,

I created a module to add custom fields to category, I managed to add a textarea field and a simple text field, both can be translatable. I would like now to add an image, I think I found how to add the file field but I reckon it's missing something.

Module is installed correctly, I can access back-office category edit page and add information, it save something in database but when I tried to edit the category again I've got a 500 error.

Here code for DB creation:

protected function installDb()
{
  return Db::getInstance()->execute('
  CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'categoryfields` (
  `id_category` INT(10) UNSIGNED NOT NULL,
  `id_shop` INT(11) UNSIGNED NOT NULL DEFAULT \'1\',
  `id_lang` INT(10) UNSIGNED NOT NULL,
  `bottom_description` TEXT,
  `promo_img_1` VARCHAR(255),
  `promo_link_1` VARCHAR(255),
  PRIMARY KEY (`id_category`,`id_shop`, `id_lang`)
  ) ENGINE=' . _MYSQL_ENGINE_ . ' default CHARSET=utf8');
}

Here is the form builder modifier:

public function hookActionCategoryFormBuilderModifier(array $params)
{
	$shopId = $this->context->shop->id;
    $categoryId = $params['id'];
    $formBuilder = $params['form_builder'];
    $locales = $this->get('prestashop.adapter.legacy.context')->getLanguages();

	$formBuilder->add('bottom_description', TranslateType::class, [
      'type' => FormattedTextareaType::class,
      'label' => $this->getTranslator()->trans('Bottom description', [], 'Modules.categoryfields.Admin'),
      'locales' => $locales,
      'hideTabs' => false,
      'required' => false
    ]);

    $formBuilder->add('promo_img_1', TranslatableType::class, [
      'type' => FileType::class,
      'label' => $this->getTranslator()->trans('Promo image 1', [], 'Modules.categoryfields.Admin'),
      'locales' => $locales,
      'required' => false
    ]);

    $formBuilder->add('promo_link_1', TranslatableType::class, [
      'type' => TextType::class,
      'label' => $this->getTranslator()->trans('Promo link 1', [], 'Modules.categoryfields.Admin'),
      'locales' => $locales,
      'required' => false
    ]);

    foreach ($locales as $locale) {
      $langId = $locale['id_lang'];
      $row = $this->getCategoryFields($categoryId, $shopId, $langId);
      $params['data']['bottom_description'][$langId] = $row['bottom_description'];
      $params['data']['promo_img_1'][$langId] = $row['promo_img_1'];
      $params['data']['promo_link_1'][$langId] = $row['promo_link_1'];
    }

    $formBuilder->setData($params['data']);
}

The back-office form:

public function hookActionAdminCategoriesFormModifier($params)
    {
        $fieldsForm = &$params['fields'];
        $fieldsForm[0]['form']['input'][] = array(
            'type' => 'textarea',
            'label' => $this->l('Bottom description'),
            'name' => 'bottom_description',
            'autoload_rte' => true,
            'lang' => true
        );
        $fieldsForm[0]['form']['input'][] = array(
            'type' => 'file',
            'label' => $this->l('Promo image 1'),
            'name' => 'promo_img_1',
            'lang' => true
        );
        $fieldsForm[0]['form']['input'][] = array(
            'type' => 'text',
            'label' => $this->l('Promo link 1'),
            'name' => 'promo_link_1',
            'autoload_rte' => true,
            'lang' => true
        );
        $fieldsValue = &$params['fields_value'];
        $fieldsValue = $this->getFieldsValues();
    }

If you need to see more methods to help me, please ask. But I reckon those are the one where I miss something. Thanks in advance for any help!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...