Jump to content

Edit History

joaorodr84

joaorodr84


typo

I am creating a custom module using PrestaShop 1.7 and I want to be be able to upload an image for the background. The image should be displayed if the field `background_image` is defined.

I am able to do it, but the image is outside of the form, as you can see in the image below.

1-wrong.thumb.png.85a74747c43cc9b9d0271b0aeef8bd44.png

The image should be displayed immediately above the background image field, like this (see below).

2-correct.thumb.png.41316b75fcc4e990e930881d59fd6520.png

Here is my .tpl file:

    {if isset($background_image)}
    <div>
        <div class="col-lg-3"></div>
        <div>
            <img src="/modules/cb_sectionaboutus/img/{$background_image}" class="img-thumbnail" width="400" />
        </div>
    </div>
    {/if}

And here is part of the main PHP file of the module:

    /**
     * Load the configuration form
     */
    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool)Tools::isSubmit('submitCb_sectionaboutusModule')) == true) {
            $this->postProcess();
        }

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

        /* Passes the background image to the template */
        $data = $this->getDataFromDB();
        $background_image = $data['background_image'];
        $this->context->smarty->assign('background_image', $background_image);

        // About section & Documentation
        $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
        return $output.$this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    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 = 'submitCb_sectionaboutusModule';
        $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 your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id
        );

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

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'textarea',
                        'label' => $this->l('Title'),
                        'name' => 'title',
                        'desc' => $this->l('Enter the title'),
                        'class' => 'rte',
                        'autoload_rte' => true
                    ),
                    array(
                        'type' => 'file',
                        'label' => $this->l('Background Image'),
                        'name' => 'background_image',
                        'desc' => $this->l('Maximum image size: ') . $this->upload_file_size_limit_in_mb . ' MB.',
                        'display_image' => true
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        $data = $this->getDataFromDB();

        return array(
            'title' => $data['title'],
            'background_image' => $data['background_image']
        );
    }

    /**
     * Get the data from the database
     */
    public function getDataFromDB()
    {
        $sql = 'SELECT * FROM ' . _DB_PREFIX_ . $this->name . ' WHERE id_' . $this->name . ' = ' . 1;
        return Db::getInstance()->ExecuteS($sql)[0];
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        /* Current data */
        $data_from_db = $this->getDataFromDB();

        /* New data */
        $form_values = $this->getConfigFormValues();
        
        /* Sets the background image as the old value, in case there is no new upload */
        $form_values['background_image'] = $data_from_db['background_image'];

        /* Validates the background image file */
        $file_name = $this->validateFile();
        /* Checks whether the background image has been successfully uploaded */
        if ($file_name) {
            /* Sets the new background image */
            $form_values['background_image'] = $file_name;
        }

        // Has rows in table --> UPDATE
        if ($data_from_db) {
            $sql = $sql = "UPDATE " . _DB_PREFIX_ . $this->name . " SET ";
            foreach (array_keys($form_values) as $key) {
                $sql .= $key . " = '" . $form_values[$key] . "', ";
            }
            $sql = trim($sql, " ");    // first trim last space
            $sql = trim($sql, ",");    // then trim trailing and prefixing commas
            $sql .= " WHERE id_" . $this->name . " = " . 1;
        }
        
        // No rows in table --> INSERT
        else {
            $columns = "id_cb_sectionaboutus, " . implode(", ", array_keys($form_values));
            $values = array_map('Tools::getValue', array_keys($form_values));
            $values = "1, " . "'" . implode("', '", array_values($values)) . "'";
            $sql = 'INSERT INTO ' . _DB_PREFIX_ . $this->name . ' (' . $columns . ') VALUES (' . $values . ')';
        }

        Db::getInstance()->ExecuteS($sql);
    }

How can I insert the uploaded image in the middle of the form using HelperForm?

I would prefer a solution with HelperForm, but I don't know if it works, so I will accept any answer tha gives me a good solution.

 

 

joaorodr84

joaorodr84


formatting

I am creating a custom module using PrestaShop 1.7 and I want to be be able to upload an image for the background. The image should be displayed if the field `background_image` is defined.

I am able to do it, but the image is outside of the form, as you can see in the image below.

1-wrong.thumb.png.85a74747c43cc9b9d0271b0aeef8bd44.png

The image should be displayed immediately above the background image field, like this (see below).

2-correct.thumb.png.41316b75fcc4e990e930881d59fd6520.png

Here is my .tpl file:

    {if isset($background_image)}
    <div>
        <div class="col-lg-3"></div>
        <div>
            <img src="/modules/cb_sectionaboutus/img/{$background_image}" class="img-thumbnail" width="400" />
        </div>
    </div>
    {/if}

And here is part of the main PHP file of the module:

    /**
     * Load the configuration form
     */
    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool)Tools::isSubmit('submitCb_sectionaboutusModule')) == true) {
            $this->postProcess();
        }

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

        /* Passes the background image to the template */
        $data = $this->getDataFromDB();
        $background_image = $data['background_image'];
        $this->context->smarty->assign('background_image', $background_image);

        // About section & Documentation
        $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
        return $output.$this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    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 = 'submitCb_sectionaboutusModule';
        $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 your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id
        );

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

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'textarea',
                        'label' => $this->l('Title'),
                        'name' => 'title',
                        'desc' => $this->l('Enter the title'),
                        'class' => 'rte',
                        'autoload_rte' => true
                    ),
                    array(
                        'type' => 'file',
                        'label' => $this->l('Background Image'),
                        'name' => 'background_image',
                        'desc' => $this->l('Maximum image size: ') . $this->upload_file_size_limit_in_mb . ' MB.',
                        'display_image' => true
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        $data = $this->getDataFromDB();

        return array(
            'title' => $data['title'],
            'background_image' => $data['background_image']
        );
    }

    /**
     * Get the data from the database
     */
    public function getDataFromDB()
    {
        $sql = 'SELECT * FROM ' . _DB_PREFIX_ . $this->name . ' WHERE id_' . $this->name . ' = ' . 1;
        return Db::getInstance()->ExecuteS($sql)[0];
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        /* Current data */
        $data_from_db = $this->getDataFromDB();

        /* New data */
        $form_values = $this->getConfigFormValues();
        
        /* Sets the background image as the old value, in case there is no new upload */
        $form_values['background_image'] = $data_from_db['background_image'];

        /* Validates the background image file */
        $file_name = $this->validateFile();
        /* Checks whether the background image has been successfully uploaded */
        if ($file_name) {
            /* Sets the new background image */
            $form_values['background_image'] = $file_name;
        }

        // Has rows in table --> UPDATE
        if ($data_from_db) {
            $sql = $sql = "UPDATE " . _DB_PREFIX_ . $this->name . " SET ";
            foreach (array_keys($form_values) as $key) {
                $sql .= $key . " = '" . $form_values[$key] . "', ";
            }
            $sql = trim($sql, " ");    // first trim last space
            $sql = trim($sql, ",");    // then trim trailing and prefixing commas
            $sql .= " WHERE id_" . $this->name . " = " . 1;
        }
        
        // No rows in table --> INSERT
        else {
            $columns = "id_cb_sectionaboutus, " . implode(", ", array_keys($form_values));
            $values = array_map('Tools::getValue', array_keys($form_values));
            $values = "1, " . "'" . implode("', '", array_values($values)) . "'";
            $sql = 'INSERT INTO ' . _DB_PREFIX_ . $this->name . ' (' . $columns . ') VALUES (' . $values . ')';
        }

        Db::getInstance()->ExecuteS($sql);
    }

How can I insert the uploaded image in the middle of the form using HelperForm?

I would prefer a solution with HelperForm, but I don't know if it works, so I will accept any answer tha gives me a good solution.

 

 

joaorodr84

joaorodr84


formatting

I creating a custom module using PrestaShop 1.7 and I want to be be able to upload an image for the background. The image should be displayed if the field `background_image` is defined.

I am able to do it, but the image is outside of the form, as you can see in the image below.

1-wrong.thumb.png.85a74747c43cc9b9d0271b0aeef8bd44.png

The image should be displayed immediately above the background image field, like this (see below).

2-correct.thumb.png.41316b75fcc4e990e930881d59fd6520.png

Here is my .tpl file:

    {if isset($background_image)}
    <div>
        <div class="col-lg-3"></div>
        <div>
            <img src="/modules/cb_sectionaboutus/img/{$background_image}" class="img-thumbnail" width="400" />
        </div>
    </div>
    {/if}

And here is part of the main PHP file of the module:

    /**
     * Load the configuration form
     */
    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool)Tools::isSubmit('submitCb_sectionaboutusModule')) == true) {
            $this->postProcess();
        }

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

        /* Passes the background image to the template */
        $data = $this->getDataFromDB();
        $background_image = $data['background_image'];
        $this->context->smarty->assign('background_image', $background_image);

        // About section & Documentation
        $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
        return $output.$this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    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 = 'submitCb_sectionaboutusModule';
        $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 your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id
        );

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

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'textarea',
                        'label' => $this->l('Title'),
                        'name' => 'title',
                        'desc' => $this->l('Enter the title'),
                        'class' => 'rte',
                        'autoload_rte' => true
                    ),
                    array(
                        'type' => 'file',
                        'label' => $this->l('Background Image'),
                        'name' => 'background_image',
                        'desc' => $this->l('Maximum image size: ') . $this->upload_file_size_limit_in_mb . ' MB.',
                        'display_image' => true
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        $data = $this->getDataFromDB();

        return array(
            'title' => $data['title'],
            'background_image' => $data['background_image']
        );
    }

    /**
     * Get the data from the database
     */
    public function getDataFromDB()
    {
        $sql = 'SELECT * FROM ' . _DB_PREFIX_ . $this->name . ' WHERE id_' . $this->name . ' = ' . 1;
        return Db::getInstance()->ExecuteS($sql)[0];
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        /* Current data */
        $data_from_db = $this->getDataFromDB();

        /* New data */
        $form_values = $this->getConfigFormValues();
        
        /* Sets the background image as the old value, in case there is no new upload */
        $form_values['background_image'] = $data_from_db['background_image'];

        /* Validates the background image file */
        $file_name = $this->validateFile();
        /* Checks whether the background image has been successfully uploaded */
        if ($file_name) {
            /* Sets the new background image */
            $form_values['background_image'] = $file_name;
        }

        // Has rows in table --> UPDATE
        if ($data_from_db) {
            $sql = $sql = "UPDATE " . _DB_PREFIX_ . $this->name . " SET ";
            foreach (array_keys($form_values) as $key) {
                $sql .= $key . " = '" . $form_values[$key] . "', ";
            }
            $sql = trim($sql, " ");    // first trim last space
            $sql = trim($sql, ",");    // then trim trailing and prefixing commas
            $sql .= " WHERE id_" . $this->name . " = " . 1;
        }
        
        // No rows in table --> INSERT
        else {
            $columns = "id_cb_sectionaboutus, " . implode(", ", array_keys($form_values));
            $values = array_map('Tools::getValue', array_keys($form_values));
            $values = "1, " . "'" . implode("', '", array_values($values)) . "'";
            $sql = 'INSERT INTO ' . _DB_PREFIX_ . $this->name . ' (' . $columns . ') VALUES (' . $values . ')';
        }

        Db::getInstance()->ExecuteS($sql);
    }

How can I insert the uploaded image in the middle of the form using HelperForm?

I would prefer a solution with HelperForm, but I don't know if it works, so I will accept any answer tha gives me a good solution.

 

 

joaorodr84

joaorodr84

I creating a custom module using PrestaShop 1.7 and I want to be be able to upload an image for the background. The image should be displayed if the field `background_image` is defined.

I am able to do it, but the image is outside of the form, as you can see in the image below (see image "1-wrong.png").

[![image in wrong position][1]][1]

The image should be displayed immediately above the background image field, like this (see image "2-correct.png").

[![image in correct position][2]][2]

Here is my .tpl file:

    {if isset($background_image)}
    <div>
        <div class="col-lg-3"></div>
        <div>
            <img src="/modules/cb_sectionaboutus/img/{$background_image}" class="img-thumbnail" width="400" />
        </div>
    </div>
    {/if}

And here is part of the main PHP file of the module:

    /**
     * Load the configuration form
     */
    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool)Tools::isSubmit('submitCb_sectionaboutusModule')) == true) {
            $this->postProcess();
        }

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

        /* Passes the background image to the template */
        $data = $this->getDataFromDB();
        $background_image = $data['background_image'];
        $this->context->smarty->assign('background_image', $background_image);

        // About section & Documentation
        $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');
        return $output.$this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    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 = 'submitCb_sectionaboutusModule';
        $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 your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id
        );

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

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l('Settings'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    array(
                        'type' => 'textarea',
                        'label' => $this->l('Title'),
                        'name' => 'title',
                        'desc' => $this->l('Enter the title'),
                        'class' => 'rte',
                        'autoload_rte' => true
                    ),
                    array(
                        'type' => 'file',
                        'label' => $this->l('Background Image'),
                        'name' => 'background_image',
                        'desc' => $this->l('Maximum image size: ') . $this->upload_file_size_limit_in_mb . ' MB.',
                        'display_image' => true
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        $data = $this->getDataFromDB();

        return array(
            'title' => $data['title'],
            'background_image' => $data['background_image']
        );
    }

    /**
     * Get the data from the database
     */
    public function getDataFromDB()
    {
        $sql = 'SELECT * FROM ' . _DB_PREFIX_ . $this->name . ' WHERE id_' . $this->name . ' = ' . 1;
        return Db::getInstance()->ExecuteS($sql)[0];
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        /* Current data */
        $data_from_db = $this->getDataFromDB();

        /* New data */
        $form_values = $this->getConfigFormValues();
        
        /* Sets the background image as the old value, in case there is no new upload */
        $form_values['background_image'] = $data_from_db['background_image'];

        /* Validates the background image file */
        $file_name = $this->validateFile();
        /* Checks whether the background image has been successfully uploaded */
        if ($file_name) {
            /* Sets the new background image */
            $form_values['background_image'] = $file_name;
        }

        // Has rows in table --> UPDATE
        if ($data_from_db) {
            $sql = $sql = "UPDATE " . _DB_PREFIX_ . $this->name . " SET ";
            foreach (array_keys($form_values) as $key) {
                $sql .= $key . " = '" . $form_values[$key] . "', ";
            }
            $sql = trim($sql, " ");    // first trim last space
            $sql = trim($sql, ",");    // then trim trailing and prefixing commas
            $sql .= " WHERE id_" . $this->name . " = " . 1;
        }
        
        // No rows in table --> INSERT
        else {
            $columns = "id_cb_sectionaboutus, " . implode(", ", array_keys($form_values));
            $values = array_map('Tools::getValue', array_keys($form_values));
            $values = "1, " . "'" . implode("', '", array_values($values)) . "'";
            $sql = 'INSERT INTO ' . _DB_PREFIX_ . $this->name . ' (' . $columns . ') VALUES (' . $values . ')';
        }

        Db::getInstance()->ExecuteS($sql);
    }

How can I insert the uploaded image in the middle of the form using HelperForm?

I would prefer a solution with HelperForm, but I don't know if it works, so I will accept any answer tha gives me a good solution.

2-correct.png

1-wrong.png

×
×
  • Create New...