Jump to content

SOLVED Add a calendar to the configuration page of a module.


Recommended Posts

Hello, I am developing a prestashop module in which I need a calendar in which to select a date. It would be like this for a text field but with a calendar:

array(
                        'col' => 3,
                        'type' => 'text',
                        'prefix' => '<i class="icon icon-envelope"></i>',
                        'desc' => $this->l('Introduce el nombre del usuario'),
                        'name' => 'MIMODULOMISMADB_ACCOUNT_USUARIO',
                        'label' => $this->l('Usuario'),
                    )

Is it possible to add a calendar to the configuration page of the module?

Edited by dostoyevski (see edit history)
Link to comment
Share on other sites

This would work on a form using Symfony (e.g. category editing). When it comes to a typical form in a module, you have two ways. The first is to use JS to replace the text input with the date input (https://stackoverflow.com/questions/46754728/jquery-ui-date-picker-using-input-type-text, https://www.cssscript.com/ basic-date-picker-input-field/). The second way is to add your own .tpl file to the form, with hard-coded date input. Then in the postProcess() function you check if the input has a filled value and do what you want with it

Link to comment
Share on other sites

Hi,

sample form in the module:

    public function renderMyForm()
    {
        $values = array();
        $this->fields_form = array();

        $this->context->controller->addJqueryUI('ui.datepicker');
        
        $defaultDate = date('Y-m-d');
        if (!Configuration::get($this->name.'_my_date')){
        	$values['my_date'] = Tools::getValue('my_date', $defaultdate);
        } else {
        	$values['my_date'] = Tools::getValue('my_date', Configuration::get($this->name.'_my_date'));
        }

        $fields_form[0] = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'date',
                        'label' => $this->l('My date field'),
                        'name' => 'my_date',
                        'required' => true,
                    )
                ),

                'submit' => array(
                    'title' => $this->l('Save settings'),
                    'class' => 'button btn btn-default pull-right',
                )
            ),
        );
        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->table =  $this->table;
        $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
        $helper->default_form_language = $lang->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
        $helper->identifier = $this->identifier;
        $helper->submit_action = 'Submit'.$this->name;
        $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' => $values,
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );
        return $helper->generateForm(array($fields_form[0]));
    }

 

And save date:

    public function postProcess()
	{
        if (Tools::isSubmit('Submit'.$this->name)) {
            if (Tools::getValue('my_date')){
                Configuration::updateValue($this->name.'_my_date', Tools::getValue('my_date'));
                Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&module_name='.$this->name);
            }
        }
    }

 

  • Like 1
Link to comment
Share on other sites

  • dostoyevski changed the title to SOLVED Add a calendar to the configuration page of a module.

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...