Create Custom Category Dropdown in Admin – Magento2 Guide

adminhtmlcategorymagento2

How can I create my custom category dropdown in admin side(Create category page) like an image

enter image description here

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:

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="general">
        <field name="featured_category_ids" sortOrder="100" formElement="select" component="Magento_Ui/js/form/element/ui-select">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filterOptions" xsi:type="boolean">true</item>
                    <item name="showCheckbox" xsi:type="boolean">true</item>
                    <item name="disableLabel" xsi:type="boolean">true</item>
                    <item name="multiple" xsi:type="boolean">true</item>
                    <item name="levelsVisibility" xsi:type="number">1</item>
                    <item name="listens" xsi:type="array">
                        <item name="${ $.namespace }.${ $.namespace }:responseData" xsi:type="string">setParsed</item>
                    </item>
                </item>
            </argument>
            <settings>
                <label translate="true">Featured Categories</label>
                <dataType>text</dataType>
                <elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options class="Magento\Catalog\Ui\Component\Product\Form\Categories\Options"/>
                    </settings>
                </select>
            </formElements>
        </field>
    </fieldset>
</form>

This will add a dropdown to the 'General' section of the form:

enter image description here

You can find more info about the UI-select component in Magento DevDocs