Jump to content

Allow multiple category trees in backoffice form


shaktishakya

Recommended Posts

I need to add more than one category trees to a module form. While trying to do so, I see all the trees appearing in the output, however the other trees do not function properly. A deeper look revealed that the other rendered trees are taking the same id specified for the first one.

 

Below is the piece of the input array

'input' => array(
    array(
       'type' => 'text',
       'label' => $this->l('Block Name'),
       'name' => 'block_name'
    ),
    array(
       'type'  => 'categories',
       'label' => $this->l('Root category'),
       'name'  => 'id_root_category',
       'tree'  => array(
                    'id' => 'id_root_category',
                  )
    ),
    array(
       'type'  => 'categories',
       'label' => $this->l('Exclude categories'),
       'name'  => 'exclude_cats',
       'tree'  => array(
                     'id' => 'exclude_cats',
                     'use_checkbox'        => true
                  ),
       ),

    ...
    ...
 )

The helperForm class does not seem to allow more than one such trees by its code. The use of $categories variable is controlling the number of tree per form. You can see that below.

case 'categories':
if ($categories)
{
$tree = new HelperTreeCategories($params['tree']['id'], isset($params['tree']['title']) ? $params['tree']['title'] : null);


if (isset($params['name']))
$tree->setInputName($params['name']);


if (isset($params['tree']['selected_categories']))
$tree->setSelectedCategories($params['tree']['selected_categories']);


if (isset($params['tree']['disabled_categories']))
$tree->setDisabledCategories($params['tree']['disabled_categories']);


if (isset($params['tree']['root_category']))
$tree->setRootCategory($params['tree']['root_category']);


if (isset($params['tree']['use_search']))
$tree->setUseSearch($params['tree']['use_search']);


if (isset($params['tree']['use_checkbox']))
$tree->setUseCheckBox($params['tree']['use_checkbox']);


$this->context->smarty->assign('categories_tree', $tree->render());
$categories = false;
}
break;

Does anyone have any solution?

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

  • 3 months later...

you can make multiple category things appear in the backoffice, but it's a pain.

 

In this example I'll show you how to make a second category selection on the category back office page.

 

This requires overriding 3 files, AdminCategoriesController.php, admin/themes/default/template/helpers/form.tpl (carefult with the path there, there are many form.tpls), and HelperForm.php

 

In AdminCategoriesController

 

in the function renderForm line 454ish, add 

array(
'type'  => 'associated_categories',
'label' => $this->l('Child category'),
'name'  => 'id_child',
'tree'  => array(
'id'                  => 'associated-categories-tree',
'use_checkbox'  => true,
'use_search'  => true,
'selected_categories' => $selected_categories,
'disabled_categories' => !Tools::isSubmit('add'.$this->table) ? array($this->_category->id) : null
)
),
 
And on line 220 add:
$this->tpl_list_vars['associated_categories_tree'] = $categories_tree;
 
 
 
in the file form.tpl line 709ish
add
{elseif $input.type == 'associated_categories'}
{$associated_categories_tree}
 
 
in the file Helperform, up where theres categories=true, put
$associated_categories = true;
 
then line 122ish add
case 'associated_categories':
if ($associated_categories)
{
$tree = new HelperTreeCategories($params['tree']['id'], isset($params['tree']['title']) ? $params['tree']['title'] : null);
 
if (isset($params['name']))
$tree->setInputName($params['name']);
 
if (isset($params['tree']['selected_categories']))
$tree->setSelectedCategories($params['tree']['selected_categories']);
 
if (isset($params['tree']['disabled_categories']))
$tree->setDisabledCategories($params['tree']['disabled_categories']);
 
if (isset($params['tree']['root_category']))
$tree->setRootCategory($params['tree']['root_category']);
 
if (isset($params['tree']['use_search']))
$tree->setUseSearch($params['tree']['use_search']);
 
if (isset($params['tree']['use_checkbox']))
$tree->setUseCheckBox($params['tree']['use_checkbox']);
 
 
$this->context->smarty->assign('associated_categories_tree', $tree->render());
$associated_categories = false;
}
break;
 
 
this won't save the data, I'll leave you to sort that out, but it will make multiple category things.
 
P.S. if anyone knows how to override admin/themes/default/template/helpers/form.tpl using the override folder I'd appreciate you letting me know! I didn't figure that out along the way to this solution and just overwrote the actual file.
Edited by gavinm (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hi.

How to save the chosen categories in string with ID separated by comma?

I guess you could? My recommendation would be to create a table much like the table "product_category" in this case "category_category" being a table with 2 fields, id_category and id_this_category

 

you can then use the synatx 

'associations' => array(
'categories' => array('type' => self::HAS_MANY, 'field' => 'id_this_category', 'object' => 'Category', 'association' => 'category_category'),
),
in the category model.
 
You will have to write your own save function for the association and attach it to the category updater.  If you need help with that I can write it out, but just go to the product model and check the function updateCategories.  the category version you'll have to write is very very similar (you'll have to create the category version of the sub functions it uses too).
Link to comment
Share on other sites

  • 6 months 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...