How can I create my custom category dropdown in admin side(Create category page) like an image
Create Custom Category Dropdown in Admin – Magento2 Guide
adminhtmlcategorymagento2
Related Solutions
create a module Yournamespace_CategoryAttribute.
in app/etc/Yournamespace_CategoryAttribute.xml
<config>
<modules>
<Yournamespace_CategoryAttribute>
<active>true</active>
<codePool>local</codePool>
</Yournamespace_CategoryAttribute>
</modules>
</config>
===========================================
now if you want to use form, in the same module:
in config.xml
<?xml version="1.0"?>
<config>
<modules>
<Yournamespace_CategoryAttribute>
<version>0.1.1</version>
</Yournamespace_CategoryAttribute>
</modules>
<global>
<models>
<categoryattribute>
<classYournamespace_CategoryAttribute_Model</class>
</categoryattribute>
<!-- removed eav_entity_setup rewrite -->
</models>
<blocks>
<categoryattribute>
<class>Yournamespace_CategoryAttribute_Block</class>
</categoryattribute>
</blocks>
</global>
<frontend>
<routers>
<categoryattribute>
<use>standard</use>
<args>
<module>Yournamespace_CategoryAttribute</module>
<frontName>categoryattribute</frontName>
</args>
</categoryattribute>
</routers>
<layout>
<updates>
<yournamespace_categoryattribute>
<file>yournamespace_categoryattribute.xml</file>
</yournamespace_categoryattribute>
</updates>
</layout>
</frontend>
</config>
in YourNamespace/CategoryAttribute/Model/Setup.php
<?php
class Yournamespace_CategoryAttribute_Model_Setup{
public function setAdminAttribute($params){
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->startSetup();
$setup->addAttribute('catalog_category', $params['attr_code'], array(
'group' => $params['section'],
'input' => $params['input_type'],
'type' => $params['db_type'],
'label' => $params['attr_label'],
'backend' => '',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));
$setup->endSetup();
}
}
in your controllers/IndexController.php
<?php
class Yournamespace_CategoryAttribute_IndexController extends Mage_Core_Controller_Front_Action{
public function indexAction(){
$this->loadLayout();
$this->renderLayout();
}
public function createAction(){
$formParams = $this->getRequest()->getParams();
Mage::getModel('categoryattribute/setup')->setAdminAttribute($formParams);
}
}
in Yournamspace/CategoryAttribute/Block/CategoryAttribute.php
<?php
class Yournamespace_CategoryAttribute_Block_CategoryAttribute extends Mage_Core_Block_Template
{
}
in app/design/frontend/your_theme/default/layout/yournamespace_categoryattribute.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<categoryattribute_index_index>
<reference name="content">
<block type="categoryattribute/categoryattribute" name="categoryattribute" template="categoryattribute/categoryattribute.phtml" />
</reference>
</categoryattribute_index_index>
</layout>
in app/design/frontend/your_theme/default/template/categoryattribute/categoryattribute.phtml
put your form and the form-action should be $baseurl/categoryattribute/
Note: This can be organised from Admin panel, by adding a admin grid and post these from admin form.
Inside your form container add button (in the _construct
method):
class Edit extends \Magento\Backend\Block\Widget\Form\Container
{
//....
/**
* Initialize form
* Add standard buttons
* Add "Save and Continue" button
*
* @return void
*/
protected function _construct()
{
//.....
// Here we check is that model already exists
if ($this->getRequest()->getParam('id')) {
// creating duplication url
$duplicateUrl = $this->_urlBuilder->getUrl(
static::URL_PATH_DUPLICATE, // path to the duplicate action, string like 'module_route/controller/action'
[
// our model's id, id - is parameter from the request
'id' => $this->getRequest()->getParam('id'),
]
);
$this->buttonList->add(
'duplicate',
[
'class' => 'save',
'label' => __('Duplicate'),
'onclick' => 'setLocation("' . $duplicateUrl . '")'
],
12 // sort order
);
}
Add controller's action:
namespace Vendor\Module\Controller\Adminhtml\Some\Thing;
class Duplicate extends \Magento\Backend\App\Action
{
/**
* Create model duplicate
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
/** @var \Vendor\Module\Model\Name $model */
$model = $this->coreRegistry->registry('your_model_unique_identifier');
try {
$newModel = clone $model; // clone
$newModel->setId(null); // remove id
$newModel->isObjectNew(true); // set object as new
$newModel->setData('is_active', 0); // set another data if needed
$newModel->getResource()->save($newModel); // save new model (clone)
// add success message and redirect to the edit form with the newly created model
$this->messageManager->addSuccessMessage(__('You duplicated the model.'));
$resultRedirect->setPath('*/*/edit', ['_current' => true, 'id' => $newModel->getId()]);
} catch (\Exception $e) {
// if something is going wrong: catch exception & redirect back
$this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
$this->messageManager->addErrorMessage($e->getMessage());
$resultRedirect->setPath('*/*/edit', ['_current' => true]);
}
return $resultRedirect;
}
}
Result should look like this:
Best Answer
If you want to add the Categories dropdown to the Category Edit pages in admin area, you should create file app/code/Vendor/Module/view/adminhtml/ui_component/category_form.xml in your custom module with the following content:
This will add a dropdown to the 'General' section of the form:
You can find more info about the UI-select component in Magento DevDocs