I am trying to add Category tree to custom extention, the category tree which is in one of the tab of edit product page
Magento 1.8 Extensions – Add Category Tree in Custom Extension
category-treeextensionsmagento-1.8
Related Solutions
public function getCategoriesArray() {
$categoriesArray = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->load()
->toArray();
$categories = array();
foreach ($categoriesArray as $categoryId => $category) {
if (isset($category['name']) && isset($category['level'])) {
$categories[] = array(
'label' => $category['name'],
'level' =>$category['level'],
'value' => $categoryId
);
}
}
return $categories;
}
to add in form
$fieldset->addField('categories', 'multiselect', array(
'label' => $this->__('Categories'),
'name' => 'categories',
'values' => $this->getCategoriesArray(),
));
You can add Category tree if you use UI form.
<field name="parent">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Magento\Catalog\Ui\Component\Product\Form\Categories\Options</item>
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Parent Category</item>
<item name="componentType" xsi:type="string">field</item>
<item name="formElement" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Catalog/js/components/new-category</item>
<item name="elementTmpl" xsi:type="string">ui/grid/filters/elements/ui-select</item>
<item name="dataScope" xsi:type="string">data.parent</item>
<item name="filterOptions" xsi:type="boolean">true</item>
<item name="showCheckbox" xsi:type="boolean">false</item>
<item name="disableLabel" xsi:type="boolean">true</item>
<item name="multiple" xsi:type="boolean">false</item>
<item name="levelsVisibility" xsi:type="number">1</item>
<item name="sortOrder" xsi:type="number">20</item>
<item name="required" xsi:type="boolean">true</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
<item name="listens" xsi:type="array">
<item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
</item>
</item>
</argument>
</field>
Best Answer
Prepare yourself, this is going to be a long one. Here goes.
You will need the following files:
app/code/local/[Namespace]/[Module]/Block/Adminhtml/[Entity]/Edit/Tab/Categories.php
- the tab that will render the categories.app/design/adminhtml/default/default/[namespace]_[module]/[entity]/tab/edit/categories.phtml
- the template needed to render the categoriesIn your form file where you add the tabs of your custom entity add this also:
In the admin controller of your custom entity these 2 actions that will handled the requests for categories:
and make sure that in the same controller this method exists:
In the admin layout file of your module add this handle for the categories action:
Now let's proceed to the saving of your data.
For this you will need the following in one of your module's install/upgrade scripts. This will create a table where the linked values will be stored
Declare your table. Add this in
config.xml
inside the<[module]_resource><entities>
tagYou will need a model for linking to categories:
app/code/local/[Namespace]/[Module]/Model/[Entity]/Category.php
and a resource model
app/code/local/[Namespace]/[Module]/Model/Resource/[Entity]/Category.php
:and a collection resource model :
app/code/local/[Namespace]/[Module]/Model/Resource/[Entity]/Category/Collection.php
Now in the saveAction of your admin controller add this right before calling
$[entity]->save()
In your entity model add this at the top of your class:
protected $_categoryInstance = null;
and these methods anywhere:That's about it. I hope I didn't miss anything. The code may required some changes because I don't know how exactly is your module built, but the main ideas are there. With some debugging you should get it to work.
Note: The code above was generated using the Ultimate Module Creator v1.9.