Jump to content

Error 500 in Module Development (display custom background in product detail page).


Recommended Posts

Hi.

I'm developing a module to add a custom background in the product detail page.

To do that, i created a module that hooks in: hookDisplayAdminProductsExtra, but when i try to do $helper->generateForm($this->fields_form) i get:

SmartyException  Twig_Error_Runtime
HTTP 500 Internal Server Error
An exception has been thrown during the rendering of a template ("Unable to load template 'file:helpers/uploader\simple.tpl'").

Twig_Error_Runtime
in src\PrestaShopBundle\Resources\views\Admin\Product\ProductPage\product.html.twig (line 161)
             

{% set hooks = renderhooksarray('displayAdminProductsExtra', { 'id_product': id_product }) %}

my methods:


    protected function initForm()
    {
        $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');

        $this->fields_form[0]['form'] = array(
            'legend' => array(
                'title' => $this->trans('New Background block', array(), 'Modules.mkcproducts.Admin'),
            ),
            'input' => array(
                array(
                    'type' => 'file',
                    'label' => $this->trans('Image', array(), 'Admin.Global'),
                    'name' => 'image',
                    'value' => true,
                    'display_image' => true,
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->trans('Text', array(), 'Admin.Global'),
                    'lang' => true,
                    'name' => 'text',
                    'cols' => 40,
                    'rows' => 10
                )
            ),
            'submit' => array(
                'title' => $this->trans('Save', array(), 'Admin.Actions'),
            )
        );

        $helper = new HelperForm();
        $helper->module = $this;
        $helper->name_controller = 'mkcproducts';
        $helper->identifier = $this->identifier;
        $helper->token = Tools::getAdminTokenLite('AdminModules');
        foreach (Language::getLanguages(false) as $lang) {
            $helper->languages[] = array(
                'id_lang' => $lang['id_lang'],
                'iso_code' => $lang['iso_code'],
                'name' => $lang['name'],
                'is_default' => ($default_lang == $lang['id_lang'] ? 1 : 0)
            );
        }

        $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;
        $helper->default_form_language = $default_lang;
        $helper->allow_employee_form_lang = $default_lang;
        $helper->toolbar_scroll = true;
        $helper->title = $this->displayName;
        $helper->submit_action = 'savemkcproduct';
        $helper->toolbar_btn =  array(
            'save' =>
            array(
                'desc' => $this->trans('Save', array(), 'Admin.Actions'),
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
            ),
            'back' =>
            array(
                'href' => AdminController::$currentIndex.'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules'),
                'desc' => $this->trans('Back to list', array(), 'Admin.Actions'),
            )
        );
        return $helper;
    }
    

    public function hookDisplayAdminProductsExtra($params) {




        $html = '';
        $id_cproduct = (int)Tools::getValue('id_product');

        if (Tools::isSubmit('savemkcproduct')) {
            if ($id_cproduct = Tools::getValue('id_cproduct')) {
                $cproduct = new CProduct((int)$id_cproduct);
            } else {
                $cproduct = new CProduct();
            }

            if ($cproduct->validateFields(false) && $cproduct->validateFieldsLang(false)) {
                $cproduct->save();

                if (isset($_FILES['image']) && isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name'])) {
                    if ($error = ImageManager::validateUpload($_FILES['image'])) {
                        return false;
                    } elseif (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['image']['tmp_name'], $tmpName)) {
                        return false;
                    } elseif (!ImageManager::resize($tmpName, dirname(__FILE__).'/img/mkcproduct-'.(int)$cproduct->id_cproduct.'-'.(int)$cproduct->id_shop.'.jpg')) {
                        return false;
                    }

                    unlink($tmpName);
                    $cproduct->file_name = 'mkcproduct-'.(int)$cproduct->id_cproduct.'-'.(int)$cproduct->id_shop.'.jpg';
                    $cproduct->save();
                }
                $this->_clearCache('*');
            } else {
                $html .= '<div class="conf error">'.$this->trans('An error occurred while attempting to save.', array(), 'Admin.Notifications.Error').'</div>';
            }
        }

           $helper = $this->initForm();
            foreach (Language::getLanguages(false) as $lang) {
                if ($id_cproduct) {
                    $cproduct = new CProduct((int)$id_cproduct);
                    $helper->fields_value['text'][(int)$lang['id_lang']] = $cproduct->text[(int)$lang['id_lang']];
                    $image = dirname(__FILE__).DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR.$cproduct->file_name;
                    $this->fields_form[0]['form']['input'][0]['image'] = '<img src="'.$this->getImageURL($cproduct->file_name).'" />';
                } else {
                    $helper->fields_value['text'][(int)$lang['id_lang']] = Tools::getValue('text_'.(int)$lang['id_lang'], '');
                }
            }
            if ($id_cproduct = Tools::getValue('id_cproduct')) {
                $this->fields_form[0]['form']['input'][] = array('type' => 'hidden', 'name' => 'id_cproduct');
                $helper->fields_value['id_cproduct'] = (int)$id_cproduct;
            }

  
            return $html.$helper->generateForm($this->fields_form);

       

          //  $this->context->smarty->assign('listacomplementos', $listacomplementos);
        //return $this->display(__FILE__, 'views/admin/tab.tpl');

    }

 

What do you think that is the problem !?

Thank you all.

Ricardo Ferreira

 

 

 

 

 

error_500.png

Link to comment
Share on other sites

  • 1 year later...

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