Jump to content

[SOLVED]Overriding is not taken into account for controllers/admin/AdminSuppliersController.php


David Bucur

Recommended Posts

Hi,

I am trying to override the default AdminSuppliersController.php module adding a few extra field in the form.

 

I copied the file into the override/controller/admin/ folder

 

 

changed the class name and removed the non-modified functions.

 

Deleted the cache/class_index.php

 

But it is not taken into account.

 

And if I take the modified piece and put in the original file it works, so it is not a problem of the extra piece not working.

<?php

class AdminSuppliersController extends AdminSuppliersControllerCore
{
	

	public function renderForm()
	{
		// loads current warehouse
		if (!($obj = $this->loadObject(true)))
			return;

		$this->fields_form = array(
			'legend' => array(
				'title' => $this->l('Suppliers'),
				'image' => '../img/admin/suppliers.gif'
			),
			'input' => array(
				array(
					'type' => 'hidden',
					'name' => 'id_address',
				),
				array(
					'type' => 'text',
					'label' => $this->l('Name'),
					'name' => 'name',
					'size' => 40,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
				),
				array(
					'type' => 'textarea',
					'label' => $this->l('Description:'),
					'name' => 'description',
					'cols' => 60,
					'rows' => 10,
					'lang' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('Will appear in the supplier list'),
					'autoload_rte' => 'rte' //Enable TinyMCE editor for short description
				),
				array(
					'type' => 'text',
					'label' => $this->l('Phone:'),
					'name' => 'phone',
					'size' => 15,
					'maxlength' => 16,
					'desc' => $this->l('Phone number for this supplier')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Email:'),
					'name' => 'email',
					'size' => 15,
					'maxlength' => 255,
					'desc' => $this->l('Email for this supplier')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Address:'),
					'name' => 'address',
					'size' => 100,
					'maxlength' => 128,
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Address:').' (2)',
					'name' => 'address2',
					'size' => 100,
					'maxlength' => 128,
				),
				array(
					'type' => 'text',
					'label' => $this->l('Postal Code/Zip Code:'),
					'name' => 'postcode',
					'size' => 10,
					'maxlength' => 12,
					'required' => true,
				),
				array(
					'type' => 'text',
					'label' => $this->l('City:'),
					'name' => 'city',
					'size' => 20,
					'maxlength' => 32,
					'required' => true,
				),
				array(
					'type' => 'select',
					'label' => $this->l('Country:'),
					'name' => 'id_country',
					'required' => true,
					'default_value' => (int)$this->context->country->id,
					'options' => array(
						'query' => Country::getCountries($this->context->language->id, false),
						'id' => 'id_country',
						'name' => 'name',
					),
				),
				array(
					'type' => 'select',
					'label' => $this->l('State'),
					'name' => 'id_state',
					'options' => array(
						'id' => 'id_state',
						'query' => array(),
						'name' => 'name'
					)
				),
				array(
					'type' => 'file',
					'label' => $this->l('Logo:'),
					'name' => 'logo',
					'display_image' => true,
					'desc' => $this->l('Upload a supplier logo from your computer')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Meta title:'),
					'name' => 'meta_title',
					'lang' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}'
				),
				array(
					'type' => 'text',
					'label' => $this->l('Meta description:'),
					'name' => 'meta_description',
					'lang' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}'
				),
				array(
					'type' => 'tags',
					'label' => $this->l('Meta keywords:'),
					'name' => 'meta_keywords',
					'lang' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('To add "tags" click in the field, write something and then press "Enter"')
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Enable:'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					)
				)
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);

		// loads current address for this supplier - if possible
		$address = null;
		if (isset($obj->id))
		{
			$id_address = Address::getAddressIdBySupplierId($obj->id);

			if ($id_address > 0)
				$address = new Address((int)$id_address);
		}

		// force specific fields values (address)
		if ($address != null)
		{
			$this->fields_value = array(
				'id_address' => $address->id,
				'phone' => $address->phone,
				'address' => $address->address1,
				'address2' => $address->address2,
				'postcode' => $address->postcode,
				'city' => $address->city,
				'id_country' => $address->id_country,
				'id_state' => $address->id_state,
			);
		}
		else
			$this->fields_value = array(
				'id_address' => 0,
				'id_country' => Configuration::get('PS_COUNTRY_DEFAULT')
			);


		if (Shop::isFeatureActive())
		{
			$this->fields_form['input'][] = array(
				'type' => 'shop',
				'label' => $this->l('Shop association:'),
				'name' => 'checkBoxShopAsso',
			);
		}

		// set logo image
		$image = ImageManager::thumbnail(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg', $this->table.'_'.(int)$this->object->id.'.'.$this->imageType, 350, $this->imageType, true);
		$this->fields_value['image'] = $image ? $image : false;
		$this->fields_value['size'] = $image ? filesize(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg') / 1000 : false;

		return parent::renderForm();
	}
}

The only modification is adding a new form item(Email) after Phone

 

Can someone see or explain me whats wrong.

 

I have done 5 or 6 overrides and all of them worked perfectly.

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

Hi,

 

You have to delete the class_index.php (which contains the list of the classes and overrides) file and let PrestaShop regenerate it (just refresh the page).
Then you override will be use by PrestaShop :)

 

Fabien

 

Edit : Sorry, I read too quickly, you erased the class_index.php file :-/

If you write a "die('test');" at the beginning of your override, does it die ?

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

Use $fields_form_override for add a fields

Use 'col' instead 'size' for input width

<?php

class AdminSuppliersController extends AdminSuppliersControllerCore
{
    public function renderForm()
    {
        $this->fields_form_override = array(
            array(
                'type' => 'text',
                'label' => $this->l('Email:'),
                'name' => 'email',
                'col' => 4,
                'maxlength' => 255,
                'desc' => $this->l('Email for this supplier')
            )
        );
        return parent::renderForm();
    }
}
  • Like 1
Link to comment
Share on other sites

 

Use $fields_form_override for add a fields

Use 'col' instead 'size' for input width

<?php

class AdminSuppliersController extends AdminSuppliersControllerCore
{
    public function renderForm()
    {
        $this->fields_form_override = array(
            array(
                'type' => 'text',
                'label' => $this->l('Email:'),
                'name' => 'email',
                'col' => 4,
                'maxlength' => 255,
                'desc' => $this->l('Email for this supplier')
            )
        );
        return parent::renderForm();
    }
}

Hi Gonebdg,

I tried what you suggested but it didn't work.

 

The sad thing is if I modify the core files they work, so I don't understand why the overitten files arent working.

Link to comment
Share on other sites

Hi,

 

I just figured out what your problem is.
At the end of your method, you coded :

return parent::renderForm();

So just after runing your renderForm method, you run the renderForm method of the native controller "AdminSuppliersControllerCore", so your array "fields_form" in your override is overwrite.

 

So at the end of your override, try this instead :

return AdminController::renderForm();

But it's not great. The best solution is the one @gonebdg gave you, but there was a mistake in his code :

<?php

class AdminSuppliersController extends AdminSuppliersControllerCore
{
    public function renderForm()
    {
        $this->fields_form_override = array(
                'type' => 'text',
                'label' => $this->l('Email:'),
                'name' => 'email',
                'col' => 4,
                'maxlength' => 255,
                'desc' => $this->l('Email for this supplier')
        );
        return parent::renderForm();
    }
}

However, I don't understand why but it seems that we can add only one field this way.

Edited by Fabien Serny (see edit history)
  • Like 1
Link to comment
Share on other sites

Hi Gonebdg,

I tried what you suggested but it didn't work.

 

The sad thing is if I modify the core files they work, so I don't understand why the overitten files arent working.

 

Are you sure ...  What is your Prestashop version ?

I've testing it on PS v.1.6.0.6

The drawback of this method is, not easy to rearrange fields positions.

So the new field added will be the last field on the form.

 

The other method that you can used is by overriding the classes file AdminController.php

<?php

class AdminController extends AdminControllerCore
{
    public function renderForm()
    {
        // override field form only on AdminSuppliers
        if($this->controller_name == 'AdminSuppliers') {
            $this->fields_form = array(
                'legend' => array(
                    'title' => $this->l('Suppliers'),
                    'icon' => 'icon-truck'
                ),
                'input' => array(
                    array(
                        'type' => 'hidden',
                        'name' => 'id_address',
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Name'),
                        'name' => 'name',
                        'required' => true,
                        'col' => 4,
                        'hint' => $this->l('Invalid characters:').' <>;=#{}',
                    ),
                    array(
                        'type' => 'textarea',
                        'label' => $this->l('Description'),
                        'name' => 'description',
                        'lang' => true,
                        'hint' => array(
                            $this->l('Invalid characters:').' <>;=#{}',
                            $this->l('Will appear in the list of suppliers.')
                        ),
                        'autoload_rte' => 'rte' //Enable TinyMCE editor for short description
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Phone'),
                        'name' => 'phone',
                        'required' => in_array('phone', $required_fields),
                        'maxlength' => 16,
                        'col' => 4,
                        'hint' => $this->l('Phone number for this supplier')
                    ),
                    // email field additions
                    array(
                        'type' => 'text',
                        'label' => $this->l('Email:'),
                        'name' => 'email',
                        'col' => 4,
                        'maxlength' => 255,
                        'desc' => $this->l('Email for this supplier')
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Mobile phone'),
                        'name' => 'phone_mobile',
                        'required' => in_array('phone_mobile', $required_fields),
                        'maxlength' => 16,
                        'col' => 4,
                        'hint' => $this->l('Mobile phone number for this supplier.')
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Address'),
                        'name' => 'address',
                        'maxlength' => 128,
                        'col' => 6,
                        'required' => true
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Address').' (2)',
                        'name' => 'address2',
                        'required' => in_array('address2', $required_fields),
                        'col' => 6,
                        'maxlength' => 128,
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Zip/postal code'),
                        'name' => 'postcode',
                        'required' => in_array('postcode', $required_fields),
                        'maxlength' => 12,
                        'col' => 2,
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('City'),
                        'name' => 'city',
                        'maxlength' => 32,
                        'col' => 4,
                        'required' => true,
                    ),
                    array(
                        'type' => 'select',
                        'label' => $this->l('Country'),
                        'name' => 'id_country',
                        'required' => true,
                        'col' => 4,
                        'default_value' => (int)$this->context->country->id,
                        'options' => array(
                            'query' => Country::getCountries($this->context->language->id, false),
                            'id' => 'id_country',
                            'name' => 'name',
                        ),
                    ),
                    array(
                        'type' => 'select',
                        'label' => $this->l('State'),
                        'name' => 'id_state',
                        'col' => 4,
                        'options' => array(
                            'id' => 'id_state',
                            'query' => array(),
                            'name' => 'name'
                        )
                    ),
                    array(
                        'type' => 'file',
                        'label' => $this->l('Logo'),
                        'name' => 'logo',
                        'display_image' => true,
                        'image' => $image_url ? $image_url : false,
                        'size' => $image_size,
                        'hint' => $this->l('Upload a supplier logo from your computer.')
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Meta title'),
                        'name' => 'meta_title',
                        'lang' => true,
                        'col' => 4,
                        'hint' => $this->l('Forbidden characters:').' <>;=#{}'
                    ),
                    array(
                        'type' => 'text',
                        'label' => $this->l('Meta description'),
                        'name' => 'meta_description',
                        'lang' => true,
                        'col' => 6,
                        'hint' => $this->l('Forbidden characters:').' <>;=#{}'
                    ),
                    array(
                        'type' => 'tags',
                        'label' => $this->l('Meta keywords'),
                        'name' => 'meta_keywords',
                        'lang' => true,
                        'col' => 6,
                        'hint' => array(
                            $this->l('To add "tags" click in the field, write something and then press "Enter".'),
                            $this->l('Forbidden characters:').' <>;=#{}'
                        )
                    ),
                    array(
                        'type' => 'switch',
                        'label' => $this->l('Enable'),
                        'name' => 'active',
                        'required' => false,
                        'class' => 't',
                        'is_bool' => true,
                        'values' => array(
                            array(
                                'id' => 'active_on',
                                'value' => 1,
                                'label' => $this->l('Enabled')
                            ),
                            array(
                                'id' => 'active_off',
                                'value' => 0,
                                'label' => $this->l('Disabled')
                            )
                        )
                    )
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                )
            );
        }
        return parent::renderForm();
    }
}
Link to comment
Share on other sites

Hi gonebdg,

 

 

Are you sure ...  What is your Prestashop version ?

I've testing it on PS v.1.6.0.6
The drawback of this method is, not easy to rearrange fields positions.

So the new field added will be the last field on the form.

 

 

As I mentionned in my previous post, you code seems wrong. You put two arrays instead of one.
Your code was not working on my installation either, I removed one array and it's working now. I only tried on PS 1.5.6 but it seems to be the same code on PS 1.6 executed in the "renderForm" method.

            // For add a fields via an override of $fields_form, use $fields_form_override
            if (is_array($this->fields_form_override) && !empty($this->fields_form_override))
                $this->fields_form[0]['form']['input'][] = $this->fields_form_override;

As you can see, it needs an array describing a new field, not an array of an array.

class AdminSuppliersController extends AdminSuppliersControllerCore
{
    public function renderForm()
    {
        $this->fields_form_override = array(
                'type' => 'text',
                'label' => $this->l('Email:'),
                'name' => 'email',
                'col' => 4,
                'maxlength' => 255,
                'desc' => $this->l('Email for this supplier')
        );
        return parent::renderForm();
    }
}

About overriding "AdminController" class, it's way dirtier than using the solution I gave :

return AdminController::renderForm();
Link to comment
Share on other sites

That is the problem ... many people ask or create a new thread without informing his Prestashop version :rolleyes:

Sorry about that

Yes, I have PS 1.5.6

 

And I need to be able to add 5 new items to the form and rearrange them as I like so I need to override the default form completely.

Link to comment
Share on other sites

Hi,

 

I just figured out what your problem is.

At the end of your method, you coded :

return parent::renderForm();

So just after runing your renderForm method, you run the renderForm method of the native controller "AdminSuppliersControllerCore", so your array "fields_form" in your override is overwrite.

 

So at the end of your override, try this instead :

return AdminController::renderForm();

But it's not great. The best solution is the one @gonebdg gave you, but there was a mistake in his code :

<?php

class AdminSuppliersController extends AdminSuppliersControllerCore
{
    public function renderForm()
    {
        $this->fields_form_override = array(
                'type' => 'text',
                'label' => $this->l('Email:'),
                'name' => 'email',
                'col' => 4,
                'maxlength' => 255,
                'desc' => $this->l('Email for this supplier')
        );
        return parent::renderForm();
    }
}

However, I don't understand why but it seems that we can add only one field this way.

Thanks Fabien,

your solution solved the problem,it was as easy as to change the parent:: to AdminController::

 

 

 

This is the complete solution if someone needs it.

<?php

class AdminSuppliersController extends AdminSuppliersControllerCore
{
	

	public function renderForm()
	{
		// loads current warehouse
		if (!($obj = $this->loadObject(true)))
			return;

		$this->fields_form = array(
			'legend' => array(
				'title' => $this->l('Suppliers'),
				'image' => '../img/admin/suppliers.gif'
			),
			'input' => array(
				array(
					'type' => 'hidden',
					'name' => 'id_address',
				),
				array(
					'type' => 'text',
					'label' => $this->l('Name'),
					'name' => 'name',
					'size' => 40,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
				),
				array(
					'type' => 'textarea',
					'label' => $this->l('Description:'),
					'name' => 'description',
					'size' => 60,
					'rows' => 10,
					'lang' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('Will appear in the supplier list'),
					'autoload_rte' => 'rte' //Enable TinyMCE editor for short description
				),
				array(
					'type' => 'text',
					'label' => $this->l('Phone:'),
					'name' => 'phone',
					'size' => 40,
					'maxlength' => 16,
					'desc' => $this->l('Phone number for this supplier')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Email:'),
					'name' => 'email',
					'size' => 40,
					'maxlength' => 255,
					'desc' => $this->l('Email for this supplier')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Address:'),
					'name' => 'address',
					'size' => 100,
					'maxlength' => 128,
					'required' => true
				),
				array(
					'type' => 'text',
					'label' => $this->l('Address:').' (2)',
					'name' => 'address2',
					'size' => 100,
					'maxlength' => 128,
				),
				array(
					'type' => 'text',
					'label' => $this->l('Postal Code/Zip Code:'),
					'name' => 'postcode',
					'size' => 10,
					'maxlength' => 12,
					'required' => true,
				),
				array(
					'type' => 'text',
					'label' => $this->l('City:'),
					'name' => 'city',
					'size' => 20,
					'maxlength' => 32,
					'required' => true,
				),
				array(
					'type' => 'select',
					'label' => $this->l('Country:'),
					'name' => 'id_country',
					'required' => true,
					'default_value' => (int)$this->context->country->id,
					'options' => array(
						'query' => Country::getCountries($this->context->language->id, false),
						'id' => 'id_country',
						'name' => 'name',
					),
				),
				array(
					'type' => 'select',
					'label' => $this->l('State'),
					'name' => 'id_state',
					'options' => array(
						'id' => 'id_state',
						'query' => array(),
						'name' => 'name'
					)
				),
                array(
					'type' => 'text',
					'label' => $this->l('Facebook:'),
					'name' => 'facebook',
					'size' => 60,
					'maxlength' => 255,
					'desc' => $this->l('Facebook for this supplier')
				),
                array(
					'type' => 'text',
					'label' => $this->l('Twitter:'),
					'name' => 'twitter',
					'size' => 60,
					'maxlength' => 255,
					'desc' => $this->l('Twitter for this supplier')
				),
                array(
					'type' => 'text',
					'label' => $this->l('Pinterest:'),
					'name' => 'pinterest',
					'size' => 60,
					'maxlength' => 255,
					'desc' => $this->l('Pinterest for this supplier')
				),
                array(
					'type' => 'text',
					'label' => $this->l('Google+:'),
					'name' => 'google',
					'size' => 60,
					'maxlength' => 255,
					'desc' => $this->l('Google+ for this supplier')
				),
				array(
					'type' => 'file',
					'label' => $this->l('Logo:'),
					'name' => 'logo',
					'display_image' => true,
					'desc' => $this->l('Upload a supplier logo from your computer')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Meta title:'),
					'name' => 'meta_title',
					'lang' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}'
				),
				array(
					'type' => 'text',
					'label' => $this->l('Meta description:'),
					'name' => 'meta_description',
					'lang' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}'
				),
				array(
					'type' => 'tags',
					'label' => $this->l('Meta keywords:'),
					'name' => 'meta_keywords',
					'lang' => true,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('To add "tags" click in the field, write something and then press "Enter"')
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Enable:'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					)
				)
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);

		// loads current address for this supplier - if possible
		$address = null;
		if (isset($obj->id))
		{
			$id_address = Address::getAddressIdBySupplierId($obj->id);

			if ($id_address > 0)
				$address = new Address((int)$id_address);
		}

		// force specific fields values (address)
		if ($address != null)
		{
			$this->fields_value = array(
				'id_address' => $address->id,
				'phone' => $address->phone,
				'address' => $address->address1,
				'address2' => $address->address2,
				'postcode' => $address->postcode,
				'city' => $address->city,
				'id_country' => $address->id_country,
				'id_state' => $address->id_state,
			);
		}
		else
			$this->fields_value = array(
				'id_address' => 0,
				'id_country' => Configuration::get('PS_COUNTRY_DEFAULT')
			);


		if (Shop::isFeatureActive())
		{
			$this->fields_form['input'][] = array(
				'type' => 'shop',
				'label' => $this->l('Shop association:'),
				'name' => 'checkBoxShopAsso',
			);
		}

		// set logo image
		$image = ImageManager::thumbnail(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg', $this->table.'_'.(int)$this->object->id.'.'.$this->imageType, 350, $this->imageType, true);
		$this->fields_value['image'] = $image ? $image : false;
		$this->fields_value['size'] = $image ? filesize(_PS_SUPP_IMG_DIR_.'/'.$this->object->id.'.jpg') / 1000 : false;

		return  AdminController::renderForm();
	}
}


Edited by David Bucur (see edit history)
  • Like 3
Link to comment
Share on other sites

  • 5 years 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...