'Featured categories PrestaShop 1.7

I would like to be able to display a few categories on front (name and a custom field I added). I created a custom module where I get the categories list to display it on back-office so I'll be able to select some to use them on front.

public function getContent()
{
    if (((bool) Tools::isSubmit('submit_feaduredcategoriesModule')) == true) {
        $this->postProcess();
    }

    $this->context->smarty->assign('module_dir', $this->_path);

    $output = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl');

    return $output . $this->renderForm();
}

protected function renderForm()
{
    $helper = new HelperForm();

    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $helper->module = $this;
    $helper->default_form_language = $this->context->language->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submit_featuredcategoriesModule';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
        . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');

    $helper->tpl_vars = array(
        'fields_value' => $this->getConfigFormValues(), /* Add values for inputs */
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id,
    );

    return $helper->generateForm(array($this->getConfigForm()));
}

protected function getConfigForm()
{
    return array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Settings'),
                'icon' => 'icon-cogs',
            ),
            'input' => array(
                array(
                    'type'  => 'categories',
                    'label' => $this->l('Featured categories'),
                    'name'  => 'FEATURED_CATEGORIES',
                    'tree'  => array(
                        'id' => 'category',
                        'selected_categories' => array((int)Configuration::get('category')),
                        'use_checkbox' => true
                    )
                ),
            ),
            'submit' => array(
                'title' => $this->l('Save'),
            ),
        ),
    );
}

But from here I'm not sure what to put in postProcess or getConfigFormValues neither am I of the 'selected_categories' in getConfigForm :(

Any help, hint, advice will be really appreciated! Thanks in advance



Solution 1:[1]

The two methods postProcess and getConfigFormValues are used as a standard de-facto, but isn't mandatories.

By the way, let's start with getConfigFormValues:
this method should return an array of the values of the inputs of your form, with index as the name of the input and the value that the input is expecting, in your case should be something like that:

protected function getConfigFormValues()
{
    return [
         'FEATURED_CATEGORIES' => Category::getCategories() // Check the right method to use in the Category class
    ];
}

The postProcess method is used to do all the operations when the form (or forms) is (are) submitted. In your case should be something like this:

protected function postProcess()
{
    if(Tools::isSumbit('submit_featuredcategoriesModule')) // Check if the form is submitted by checking the input name that you specify in $helper->submit_action
    {
        // Do your stuff
    }
}

Tips:

  • Use the official devdocs, is really full of infos: https://devdocs.prestashop.com/
  • The Configuration class is used to store any kind of infos in the database, it use the ps_configuration table (if ps_ is used as tables prefix). The mostly used methods are get(KEY) to retrieve infos, and updateValue(KEY) to store values in the DB
  • The Tools class have a full set of methods to interact with requests and with PS ecosystem, i.e. getValue(KEY) to get values from POST or GET request (also upload FILE, etc) or isSubmit(KEY) to check if that value is submitted (also from GET or POST)

The class files are in root/classes, check them to discover all the methods ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1