Adding the Visual Editor to PrestaShop’s Category Description

In this video and written quick tip, we will see how to add a visual editor to the category description in Presashop. The proper way, of course, so that changes will not be killed with the next Prestashop Upgrade!

Download Project Files

Watch The Screencast (text below)

Adding the visual editor to category description the proper way

Open up your Prestashop root folder, then navigate to controllers/admin and open AdminCategorisController.php in your favorite code editor. Locate the renderForm method, at about line 321 (Prestashop 1.5.4.0). Inside it, locate the following snippet

<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'textarea',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Description:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'description',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'rows' => 10,<br /><%%KEEPWHITESPACE%%>					'cols' => 100,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Invalid characters:').' <>;=#{}',<br /><%%KEEPWHITESPACE%%>				),<br />

Change so it looks like this:

<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'textarea',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Description:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'description',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'rows' => 10,<br /><%%KEEPWHITESPACE%%>					'cols' => 100,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Invalid characters:').' <>;=#{}',<br /><%%KEEPWHITESPACE%%>					'autoload_rte' => true<br /><%%KEEPWHITESPACE%%>				),<br />

As you can see we have added ‘autoload_rte’ => true. This enables the visual editor. Save and access a category page in the back office to see the visual editor. However, this way your changes will inevitably be overrridden at the next prestashop update you do. We need to use an override.

Using an override for the controller

Back to your prestashop root, go to override/controllers/admin and create a file called AdminCategorisController.php. Paste the following snippet inside Change so it looks like this:

<br /><!--?php<br /><br />Class AdminCategoriesController extends AdminCategoriesControllerCore<br />{<br /><br />?--><br />

Note: We are extending a prestashop class (controller in this case). If you want to know more about this, head over here. In the original AdminCategoriesController, copy the whole renderForm function we just modified, and paste it inside our new override. Okay, at this point you might notice that the method is calling parent::renderForm(); at the end, right before the closing bracket. Therefore, change the following line

<br />return parent::renderForm();<br />

to

<br />return AdminController::renderForm();<br />

Tip: if we didn’t do this, the original one would have overridden this one no matter what, and no changes would have taken place Lastly, head over to the root folder again then cache/ and delete the class_index.php file, so that our changes take place. At this point, you can also restore the original adminCategoriesController to its defaults, since we are extending it in a maintainable way. Here is the complete override (you can download it as a project file) …And! if you would like more freedom with the visual editor, or simply the possibility to add html / javascript / css content everywhere in Prestashop/s front office, have a look at my module HTML Block!

<br /><br /><?php<br /><br />Class AdminCategoriesController extends AdminCategoriesControllerCore<br />{<br /><%%KEEPWHITESPACE%%>	public function renderForm()<br /><%%KEEPWHITESPACE%%>	{<br /><%%KEEPWHITESPACE%%>		$this->initToolbar();<br /><%%KEEPWHITESPACE%%>		$obj = $this->loadObject(true);<br /><%%KEEPWHITESPACE%%>		$id_shop = Context::getContext()->shop->id;<br /><%%KEEPWHITESPACE%%>		$selected_cat = array((isset($obj->id_parent) && $obj->isParentCategoryAvailable($id_shop))? (int)$obj->id_parent : (int)Tools::getValue('id_parent', Category::getRootCategory()->id));<br /><%%KEEPWHITESPACE%%>		$unidentified = new Group(Configuration::get('PS_UNIDENTIFIED_GROUP'));<br /><%%KEEPWHITESPACE%%>		$guest = new Group(Configuration::get('PS_GUEST_GROUP'));<br /><%%KEEPWHITESPACE%%>		$default = new Group(Configuration::get('PS_CUSTOMER_GROUP'));<br /><br /><%%KEEPWHITESPACE%%>		$unidentified_group_information = sprintf($this->l('%s - All people without a valid customer account.'), '<b>'.$unidentified->name[$this->context->language->id].'</b>');<br /><%%KEEPWHITESPACE%%>		$guest_group_information = sprintf($this->l('%s - Customer who placed an order with the guest checkout.'), '<b>'.$guest->name[$this->context->language->id].'</b>');<br /><%%KEEPWHITESPACE%%>		$default_group_information = sprintf($this->l('%s - All people who have created an account on this site.'), '<b>'.$default->name[$this->context->language->id].'</b>');<br /><%%KEEPWHITESPACE%%>		$root_category = Category::getRootCategory();<br /><%%KEEPWHITESPACE%%>		$root_category = array('id_category' => $root_category->id, 'name' => $root_category->name);<br /><%%KEEPWHITESPACE%%>		$this->fields_form = array(<br /><%%KEEPWHITESPACE%%>			'tinymce' => true,<br /><%%KEEPWHITESPACE%%>			'legend' => array(<br /><%%KEEPWHITESPACE%%>				'title' => $this->l('Category'),<br /><%%KEEPWHITESPACE%%>				'image' => '../img/admin/tab-categories.gif'<br /><%%KEEPWHITESPACE%%>			),<br /><%%KEEPWHITESPACE%%>			'input' => array(<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'text',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Name:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'name',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'size' => 48,<br /><%%KEEPWHITESPACE%%>					'required' => true,<br /><%%KEEPWHITESPACE%%>					'class' => 'copy2friendlyUrl',<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Invalid characters:').' <>;=#{}',<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'radio',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Displayed:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'active',<br /><%%KEEPWHITESPACE%%>					'required' => false,<br /><%%KEEPWHITESPACE%%>					'class' => 't',<br /><%%KEEPWHITESPACE%%>					'is_bool' => true,<br /><%%KEEPWHITESPACE%%>					'values' => array(<br /><%%KEEPWHITESPACE%%>						array(<br /><%%KEEPWHITESPACE%%>							'id' => 'active_on',<br /><%%KEEPWHITESPACE%%>							'value' => 1,<br /><%%KEEPWHITESPACE%%>							'label' => $this->l('Enabled')<br /><%%KEEPWHITESPACE%%>						),<br /><%%KEEPWHITESPACE%%>						array(<br /><%%KEEPWHITESPACE%%>							'id' => 'active_off',<br /><%%KEEPWHITESPACE%%>							'value' => 0,<br /><%%KEEPWHITESPACE%%>							'label' => $this->l('Disabled')<br /><%%KEEPWHITESPACE%%>						)<br /><%%KEEPWHITESPACE%%>					)<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'categories',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Parent category:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'id_parent',<br /><%%KEEPWHITESPACE%%>					'values' => array(<br /><%%KEEPWHITESPACE%%>						'trads' => array(<br /><%%KEEPWHITESPACE%%>							 'Root' => $root_category,<br /><%%KEEPWHITESPACE%%>							 'selected' => $this->l('Selected'),<br /><%%KEEPWHITESPACE%%>							 'Collapse All' => $this->l('Collapse All'),<br /><%%KEEPWHITESPACE%%>							 'Expand All' => $this->l('Expand All')<br /><%%KEEPWHITESPACE%%>						),<br /><%%KEEPWHITESPACE%%>						'selected_cat' => $selected_cat,<br /><%%KEEPWHITESPACE%%>						'input_name' => 'id_parent',<br /><%%KEEPWHITESPACE%%>						'use_radio' => true,<br /><%%KEEPWHITESPACE%%>						'use_search' => false,<br /><%%KEEPWHITESPACE%%>						'disabled_categories' => array(4),<br /><%%KEEPWHITESPACE%%>						'top_category' => Category::getTopCategory(),<br /><%%KEEPWHITESPACE%%>						'use_context' => true,<br /><%%KEEPWHITESPACE%%>					)<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'radio',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Root Category:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'is_root_category',<br /><%%KEEPWHITESPACE%%>					'required' => false,<br /><%%KEEPWHITESPACE%%>					'is_bool' => true,<br /><%%KEEPWHITESPACE%%>					'class' => 't',<br /><%%KEEPWHITESPACE%%>					'values' => array(<br /><%%KEEPWHITESPACE%%>						array(<br /><%%KEEPWHITESPACE%%>							'id' => 'is_root_on',<br /><%%KEEPWHITESPACE%%>							'value' => 1,<br /><%%KEEPWHITESPACE%%>							'label' => $this->l('Yes')<br /><%%KEEPWHITESPACE%%>						),<br /><%%KEEPWHITESPACE%%>						array(<br /><%%KEEPWHITESPACE%%>							'id' => 'is_root_off',<br /><%%KEEPWHITESPACE%%>							'value' => 0,<br /><%%KEEPWHITESPACE%%>							'label' => $this->l('No')<br /><%%KEEPWHITESPACE%%>						)<br /><%%KEEPWHITESPACE%%>					)<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'textarea',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Description:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'description',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'rows' => 10,<br /><%%KEEPWHITESPACE%%>					'cols' => 100,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Invalid characters:').' <>;=#{}',<br /><%%KEEPWHITESPACE%%>					'autoload_rte' => true<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'file',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Image:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'image',<br /><%%KEEPWHITESPACE%%>					'display_image' => true,<br /><%%KEEPWHITESPACE%%>					'desc' => $this->l('Upload a category logo from your computer.')<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'text',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Meta title:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'meta_title',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Forbidden characters:').' <>;=#{}'<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'text',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Meta description:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'meta_description',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Forbidden characters:').' <>;=#{}'<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'tags',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Meta keywords:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'meta_keywords',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Forbidden characters:').' <>;=#{}',<br /><%%KEEPWHITESPACE%%>					'desc' => $this->l('To add "tags," click in the field, write something, and then press "Enter."')<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'text',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Friendly URL:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'link_rewrite',<br /><%%KEEPWHITESPACE%%>					'lang' => true,<br /><%%KEEPWHITESPACE%%>					'required' => true,<br /><%%KEEPWHITESPACE%%>					'hint' => $this->l('Only letters and the minus (-) character are allowed.')<br /><%%KEEPWHITESPACE%%>				),<br /><%%KEEPWHITESPACE%%>				array(<br /><%%KEEPWHITESPACE%%>					'type' => 'group',<br /><%%KEEPWHITESPACE%%>					'label' => $this->l('Group access:'),<br /><%%KEEPWHITESPACE%%>					'name' => 'groupBox',<br /><%%KEEPWHITESPACE%%>					'values' => Group::getGroups(Context::getContext()->language->id),<br /><%%KEEPWHITESPACE%%>					'info_introduction' => $this->l('You now have three default customer groups.'),<br /><%%KEEPWHITESPACE%%>					'unidentified' => $unidentified_group_information,<br /><%%KEEPWHITESPACE%%>					'guest' => $guest_group_information,<br /><%%KEEPWHITESPACE%%>					'customer' => $default_group_information,<br /><%%KEEPWHITESPACE%%>					'desc' => $this->l('Mark all of the customer groups you;d like to have access to this category.')<br /><%%KEEPWHITESPACE%%>				)<br /><%%KEEPWHITESPACE%%>			),<br /><%%KEEPWHITESPACE%%>			'submit' => array(<br /><%%KEEPWHITESPACE%%>				'title' => $this->l('Save'),<br /><%%KEEPWHITESPACE%%>				'class' => 'button'<br /><%%KEEPWHITESPACE%%>			)<br /><%%KEEPWHITESPACE%%>		);<br /><br /><%%KEEPWHITESPACE%%>		$this->tpl_form_vars['shared_category'] = Validate::isLoadedObject($obj) && $obj->hasMultishopEntries();<br /><%%KEEPWHITESPACE%%>		$this->tpl_form_vars['PS_ALLOW_ACCENTED_CHARS_URL'] = (int)Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL');<br /><%%KEEPWHITESPACE%%>		if (Shop::isFeatureActive())<br /><%%KEEPWHITESPACE%%>			$this->fields_form['input'][] = array(<br /><%%KEEPWHITESPACE%%>				'type' => 'shop',<br /><%%KEEPWHITESPACE%%>				'label' => $this->l('Shop association:'),<br /><%%KEEPWHITESPACE%%>				'name' => 'checkBoxShopAsso',<br /><%%KEEPWHITESPACE%%>			);<br /><%%KEEPWHITESPACE%%>		// remove category tree and radio button "is_root_category" if this category has the root category as parent category to avoid any conflict<br /><%%KEEPWHITESPACE%%>		if ($this->_category->id_parent == Category::getTopCategory()->id && Tools::isSubmit('updatecategory'))<br /><%%KEEPWHITESPACE%%>			foreach ($this->fields_form['input'] as $k => $input)<br /><%%KEEPWHITESPACE%%>				if (in_array($input['name'], array('id_parent', 'is_root_category')))<br /><%%KEEPWHITESPACE%%>					unset($this->fields_form['input'][$k]);<br /><br /><%%KEEPWHITESPACE%%>		if (Tools::isSubmit('add'.$this->table.'root'))<br /><%%KEEPWHITESPACE%%>			unset($this->fields_form['input'][2],$this->fields_form['input'][3]);<br /><br /><%%KEEPWHITESPACE%%>		if (!($obj = $this->loadObject(true)))<br /><%%KEEPWHITESPACE%%>			return;<br /><br /><%%KEEPWHITESPACE%%>		$image = ImageManager::thumbnail(_PS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg', $this->table.'_'.(int)$obj->id.'.'.$this->imageType, 350, $this->imageType, true);<br /><br /><%%KEEPWHITESPACE%%>		$this->fields_value = array(<br /><%%KEEPWHITESPACE%%>			'image' => $image ? $image : false,<br /><%%KEEPWHITESPACE%%>			'size' => $image ? filesize(_PS_CAT_IMG_DIR_.'/'.$obj->id.'.jpg') / 1000 : false<br /><%%KEEPWHITESPACE%%>		);<br /><br /><%%KEEPWHITESPACE%%>		// Added values of object Group<br /><%%KEEPWHITESPACE%%>		$category_groups_ids = $obj->getGroups();<br /><br /><%%KEEPWHITESPACE%%>		$groups = Group::getGroups($this->context->language->id);<br /><%%KEEPWHITESPACE%%>		// if empty $carrier_groups_ids : object creation : we set the default groups<br /><%%KEEPWHITESPACE%%>		if (empty($category_groups_ids))<br /><%%KEEPWHITESPACE%%>		{<br /><%%KEEPWHITESPACE%%>			$preselected = array(Configuration::get('PS_UNIDENTIFIED_GROUP'), Configuration::get('PS_GUEST_GROUP'), Configuration::get('PS_CUSTOMER_GROUP'));<br /><%%KEEPWHITESPACE%%>			$category_groups_ids = array_merge($category_groups_ids, $preselected);<br /><%%KEEPWHITESPACE%%>		}<br /><%%KEEPWHITESPACE%%>		foreach ($groups as $group)<br /><%%KEEPWHITESPACE%%>			$this->fields_value['groupBox_'.$group['id_group']] = Tools::getValue('groupBox_'.$group['id_group'], (in_array($group['id_group'], $category_groups_ids)));<br /><br /><%%KEEPWHITESPACE%%>		return AdminController::renderForm();<br /><%%KEEPWHITESPACE%%>	}<br />}<br />

Looking for quality PrestaShop Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , | Posted under PrestaShop, Video | RSS 2.0

Author Spotlight

Fabio Porta

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *


2 + = 8

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>