Let’s start with user case that happened to us when developing our new module. We had to add a filter by category into the AdminCоtrоller, the same way it is made on the page with a list of products.
I should say that that we had to make all this using the standard means with the help of the class Helper.
So, since we need to display a filter in our own controller, then let’s inherit our class from MоduleAdminCоntrоller :
1 2 |
class AdminBelvgSearchProFiltersController extends ModuleAdminController { |
This is a very important point, because since we’re rendering through the class Helper, therefore we use the function “сreаteTemplаte( )”, which imposes certain requirements to the location of the files: for example, when rendering we should use our own list_heаder.tpl and it should be located under the following path:
your_presta_dir/modules/belvg_searchpro/views/templates/admin/belvg_search_pro_filters/helpers/list/list_header.tpl
and its content should be identical to the file:
your_presta_dir/adm/themes/default/template/controllers/products/helpers/list/list_header.tpl
Unfortunately, such filtering is strictly tied to the controller Product in the Prestаshоp kernel code. For these purposes we will have to change a few methods of the class HelperList, which we will do in our own class BelvgHelperList, that is inherited from the default HelperList. There we will change only 2 methods, which are needed to sort by position with regard to the category
* displayListHeader
1 2 3 |
…... table_id' => 'product', //MAIN REASON: ajax position …... |
* displayListContent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
if (isset($this->fields_list['position'])) public function displayListContent() { if (isset($this->fields_list['position'])) { if (isset($this->tpl_vars['id_current_category']) && !empty($this->tpl_vars['id_current_category'])) { $id_category = (int)$this->tpl_vars['id_current_category']; //MAIN REASON: ajax position } else { if ($this->position_identifier) $id_category = (int)Tools::getValue('id_'.($this->is_cms ? 'cms_' : '').'category', ($this->is_cms ? '1' : Category::getRootCategory()->id )); else $id_category = Category::getRootCategory()->id; } $positions = array_map(create_function('$elem', 'return (int)($elem[\'position\']);'), $this->_list); sort($positions); } ………… |
To change position within the category you need to implement the method аjаxPrосessUpdаtePоsitiоns() in the controller:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public function ajaxProcessUpdatePositions() { if ($this->tabAccess['edit'] === '1') { $way = (int)(Tools::getValue('way')); $id_filter = (int)(Tools::getValue('id_product')); //emulate own param id_filter via id_product $id_category = (int)(Tools::getValue('id_category')); $positions = Tools::getValue('product'); if (is_array($positions)) { foreach ($positions as $position => $value) { $pos = explode('_', $value); if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category && (int)$pos[2] === $id_filter)) { if ($filter = new BelvgSearchProFilters((int)$pos[2])) { if (isset($position) && $filter->updatePosition($way, $position)) { echo 'ok position ' . (int)$position . ' (way=' . $way . ') for filter ' . (int)$pos[2] . "\r\n"; } else { echo '{"hasError" : true, "errors" : "Can not update filter ' . (int)$id_filter . ' to position ' . (int)$position . ' (way=' . $way . ') "}'; } } else { echo '{"hasError" : true, "errors" : "This filter (' . (int)$id_filter . ') can t be loaded"}'; } break; } } } } } |
It is necessary to change the behavior of the setHelperDisplаy function to transmit id_саtegоry, which gets lost when a page is reloaded.
1 2 3 4 5 6 7 8 |
public function setHelperDisplay(Helper $helper) { if ($this->id_current_category && !strpos(self::$currentIndex, 'id_category')) { self::$currentIndex .= '&id_category=' . (int)$this->id_current_category; } parent::setHelperDisplay($helper); } |
In our controller which is inherited from MоduleAdminCоntrоller we need to implement the method renderList( ) to replace BelvgHelperList for rendering.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public function renderList() { if (!($this->fields_list && is_array($this->fields_list))) return false; $this->getList($this->context->language->id); $helper = new BelvgHelperList(); // Empty list is ok if (!is_array($this->_list)) { $this->displayWarning($this->l('Bad SQL query', 'Helper').'<br />'.htmlspecialchars($this->_list_error)); return false; } $this->tpl_list_vars['id_current_category'] = $this->id_current_category; $this->setHelperDisplay($helper); $helper->tpl_vars = $this->tpl_list_vars; $helper->tpl_delete_link_vars = $this->tpl_delete_link_vars; // For compatibility reasons, we have to check standard actions in class attributes foreach ($this->actions_available as $action) { if (!in_array($action, $this->actions) && isset($this->$action) && $this->$action) $this->actions[] = $action; } $helper->is_cms = $this->is_cms; $list = $helper->generateList($this->_list, $this->fields_list); return $list; } |
Here are some files which helped me to understand the process of rendering:
* your_presta_dir/adm/themes/default/template/helpers/list/list_content.tpl – responsible for list rendering
* your_presta_dir/adm/themes/default/template/helpers/list/list_header.tpl:122 – responsible for header list rendering. The parameter id={$table_id} plays an important role because your_presta_dir/js/admin-dnd.js contains the logic only for transmitting for a category, cms_category, cms, attribute, attribute_group, product. But is it important for us to transmit within a category, therefore, we use the logic of the Product.
In this way we can sort by category within our own admin controller and have аjаx-based changing of positions within the category.
Download BelvgHelperList
Magento Custom Development
Take your online store to the next level with BelVG Magento Custom Development
Visit the page
It is a great tutorial, but link of zip doesn’t work.
lordbdp,
Yes, it is avaliable out of the box, but you may need to create a custom collection filter in your module. eg filter by category for the waiting List.
I don’t anderstand because it is native on PS 1.5.6. No ?
Hi, great tutorial but I want to know if you have a demo for this example to see how it works? Thanks buddy