How to get All Active Categories in Custom Module Admin Form Dropdown in Magento 2?Add category tree structure in custom module formHow to show bundle products on custom module admin grid in magento 2Magento 2: store selector for custom module in admin formmagento 2 : Display Uploaded image in admin form custom moduleMagento 2 : How to Create Product Attribute With All Active Categories DropDownMagento 2 : How to Show All Active Categories in Layered Navigation With Its Sub-categories?Created a custom module (custom admin-form)Magento2 Filters custom module dropdown in adminMagento 2 Get Shipping Rates of All active shipping methodshow to active/deactive a module for each store by admin in magento 2.2

Suggested order for Amazon Prime Doctor Who series

.NET executes a SQL query and Active Monitor shows multiple rows blocking each other

How do I set an alias to a terminal line?

How many people are necessary to maintain modern civilisation?

Parameterize chained calls to a utility program in Bash

Should I prioritize my 401k over my student loans?

Do I have any obligations to my PhD supervisor's requests after I have graduated?

What does "play with your toy’s toys" mean?

Why don't countries like Japan just print more money?

Greeting with "Ho"

When to remove insignificant variables?

What was the Shuttle Carrier Aircraft escape tunnel?

How does a pilot select the correct ILS when the airport has parallel runways?

How do I professionally let my manager know I'll quit over smoking in the office?

Can White Castle?

How dangerous are set-size assumptions?

What did River say when she woke from her proto-comatose state?

Relationship between woodwinds and brass in a marching band?

Is a single radon-daughter atom in air a solid?

Is it illegal to withhold someone's passport and green card in California?

Dates on degrees don’t make sense – will people care?

Old sci-fi story: radiation mutated the animals, a boy loses a limb, but it's okay because "humans used to do great with only two arms"

Impossible darts scores

Is "qch. est à mourir" considered an anglicism calqued from "sth is to die for"? How commonly is it used?



How to get All Active Categories in Custom Module Admin Form Dropdown in Magento 2?


Add category tree structure in custom module formHow to show bundle products on custom module admin grid in magento 2Magento 2: store selector for custom module in admin formmagento 2 : Display Uploaded image in admin form custom moduleMagento 2 : How to Create Product Attribute With All Active Categories DropDownMagento 2 : How to Show All Active Categories in Layered Navigation With Its Sub-categories?Created a custom module (custom admin-form)Magento2 Filters custom module dropdown in adminMagento 2 Get Shipping Rates of All active shipping methodshow to active/deactive a module for each store by admin in magento 2.2






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am implementing a custom module in magento 2.In that module I want to show all active categories in admin form dropdown.How can I show them please help me on this.Thanks in Advance.










share|improve this question






















  • Are you using UI form?

    – Prince Patel
    Jul 7 '17 at 9:35











  • no, normal xml way only.

    – shankar boss
    Jul 7 '17 at 11:42











  • Can you share the code of admin form?

    – Prince Patel
    Jul 7 '17 at 11:49

















0















I am implementing a custom module in magento 2.In that module I want to show all active categories in admin form dropdown.How can I show them please help me on this.Thanks in Advance.










share|improve this question






















  • Are you using UI form?

    – Prince Patel
    Jul 7 '17 at 9:35











  • no, normal xml way only.

    – shankar boss
    Jul 7 '17 at 11:42











  • Can you share the code of admin form?

    – Prince Patel
    Jul 7 '17 at 11:49













0












0








0








I am implementing a custom module in magento 2.In that module I want to show all active categories in admin form dropdown.How can I show them please help me on this.Thanks in Advance.










share|improve this question














I am implementing a custom module in magento 2.In that module I want to show all active categories in admin form dropdown.How can I show them please help me on this.Thanks in Advance.







magento2 adminform drop-downs category-listing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jul 7 '17 at 6:39









shankar bossshankar boss

59431230




59431230












  • Are you using UI form?

    – Prince Patel
    Jul 7 '17 at 9:35











  • no, normal xml way only.

    – shankar boss
    Jul 7 '17 at 11:42











  • Can you share the code of admin form?

    – Prince Patel
    Jul 7 '17 at 11:49

















  • Are you using UI form?

    – Prince Patel
    Jul 7 '17 at 9:35











  • no, normal xml way only.

    – shankar boss
    Jul 7 '17 at 11:42











  • Can you share the code of admin form?

    – Prince Patel
    Jul 7 '17 at 11:49
















Are you using UI form?

– Prince Patel
Jul 7 '17 at 9:35





Are you using UI form?

– Prince Patel
Jul 7 '17 at 9:35













no, normal xml way only.

– shankar boss
Jul 7 '17 at 11:42





no, normal xml way only.

– shankar boss
Jul 7 '17 at 11:42













Can you share the code of admin form?

– Prince Patel
Jul 7 '17 at 11:49





Can you share the code of admin form?

– Prince Patel
Jul 7 '17 at 11:49










2 Answers
2






active

oldest

votes


















0














In Block Put the code to get category collection



<?php
namespace SampleExampleBlock;

class Categorydata extends MagentoFrameworkViewElementTemplate

public function getCategories()

$categoryFactory = $_objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')->addFieldToFilter('is_active', 1);
return $categories;


?>


Next You can call Block function in your Template file



<?php 
$collection = $block->getCategories();
foreach ($collection as $categorycollection)
$categorycollection->getName();

?>





share|improve this answer























  • If i use ui component means how do i get category collcetion

    – Jaisa
    Mar 23 '18 at 5:31











  • Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

    – Learing_Coder
    Mar 23 '18 at 5:57


















0














Any custom module get the collection and shown in the select form field



  1. First, add the below code in your block


 protected $_categoryModal;

public function __construct(
MagentoCatalogModelCategory $categoryModal,
)


$this->_categoryModal = $categoryModal;


public function toOptionArray()


$productCategorys = $this->_categoryModal->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active', 1);
$productCategory = [];
foreach ($productCategorys as $category)
$productCategory[] = [ 'value' => $category->getId(), 'label' => $category->getName() ];

return $productCategory;




2.form field add into your _prepareForm method



$fieldset->addField(
'cat_id',
'select',
array(
'label' => $this->__("category"),
'class' => 'required-entry',
'required' => 'true',
'name' => 'cat_id',
'values' => $this->toOptionArray()
)
);



  1. Now please remove the generated and var folder using below command line



    rm -rf generated var/







share|improve this answer

























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f182506%2fhow-to-get-all-active-categories-in-custom-module-admin-form-dropdown-in-magento%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In Block Put the code to get category collection



    <?php
    namespace SampleExampleBlock;

    class Categorydata extends MagentoFrameworkViewElementTemplate

    public function getCategories()

    $categoryFactory = $_objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
    $categories = $categoryFactory->create()
    ->addAttributeToSelect('*')->addFieldToFilter('is_active', 1);
    return $categories;


    ?>


    Next You can call Block function in your Template file



    <?php 
    $collection = $block->getCategories();
    foreach ($collection as $categorycollection)
    $categorycollection->getName();

    ?>





    share|improve this answer























    • If i use ui component means how do i get category collcetion

      – Jaisa
      Mar 23 '18 at 5:31











    • Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

      – Learing_Coder
      Mar 23 '18 at 5:57















    0














    In Block Put the code to get category collection



    <?php
    namespace SampleExampleBlock;

    class Categorydata extends MagentoFrameworkViewElementTemplate

    public function getCategories()

    $categoryFactory = $_objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
    $categories = $categoryFactory->create()
    ->addAttributeToSelect('*')->addFieldToFilter('is_active', 1);
    return $categories;


    ?>


    Next You can call Block function in your Template file



    <?php 
    $collection = $block->getCategories();
    foreach ($collection as $categorycollection)
    $categorycollection->getName();

    ?>





    share|improve this answer























    • If i use ui component means how do i get category collcetion

      – Jaisa
      Mar 23 '18 at 5:31











    • Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

      – Learing_Coder
      Mar 23 '18 at 5:57













    0












    0








    0







    In Block Put the code to get category collection



    <?php
    namespace SampleExampleBlock;

    class Categorydata extends MagentoFrameworkViewElementTemplate

    public function getCategories()

    $categoryFactory = $_objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
    $categories = $categoryFactory->create()
    ->addAttributeToSelect('*')->addFieldToFilter('is_active', 1);
    return $categories;


    ?>


    Next You can call Block function in your Template file



    <?php 
    $collection = $block->getCategories();
    foreach ($collection as $categorycollection)
    $categorycollection->getName();

    ?>





    share|improve this answer













    In Block Put the code to get category collection



    <?php
    namespace SampleExampleBlock;

    class Categorydata extends MagentoFrameworkViewElementTemplate

    public function getCategories()

    $categoryFactory = $_objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
    $categories = $categoryFactory->create()
    ->addAttributeToSelect('*')->addFieldToFilter('is_active', 1);
    return $categories;


    ?>


    Next You can call Block function in your Template file



    <?php 
    $collection = $block->getCategories();
    foreach ($collection as $categorycollection)
    $categorycollection->getName();

    ?>






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jul 7 '17 at 7:10









    Learing_CoderLearing_Coder

    649317




    649317












    • If i use ui component means how do i get category collcetion

      – Jaisa
      Mar 23 '18 at 5:31











    • Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

      – Learing_Coder
      Mar 23 '18 at 5:57

















    • If i use ui component means how do i get category collcetion

      – Jaisa
      Mar 23 '18 at 5:31











    • Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

      – Learing_Coder
      Mar 23 '18 at 5:57
















    If i use ui component means how do i get category collcetion

    – Jaisa
    Mar 23 '18 at 5:31





    If i use ui component means how do i get category collcetion

    – Jaisa
    Mar 23 '18 at 5:31













    Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

    – Learing_Coder
    Mar 23 '18 at 5:57





    Can you post the file location where you wann display so that i can help out.please post the code,how you have tried. @Sri

    – Learing_Coder
    Mar 23 '18 at 5:57













    0














    Any custom module get the collection and shown in the select form field



    1. First, add the below code in your block


     protected $_categoryModal;

    public function __construct(
    MagentoCatalogModelCategory $categoryModal,
    )


    $this->_categoryModal = $categoryModal;


    public function toOptionArray()


    $productCategorys = $this->_categoryModal->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active', 1);
    $productCategory = [];
    foreach ($productCategorys as $category)
    $productCategory[] = [ 'value' => $category->getId(), 'label' => $category->getName() ];

    return $productCategory;




    2.form field add into your _prepareForm method



    $fieldset->addField(
    'cat_id',
    'select',
    array(
    'label' => $this->__("category"),
    'class' => 'required-entry',
    'required' => 'true',
    'name' => 'cat_id',
    'values' => $this->toOptionArray()
    )
    );



    1. Now please remove the generated and var folder using below command line



      rm -rf generated var/







    share|improve this answer



























      0














      Any custom module get the collection and shown in the select form field



      1. First, add the below code in your block


       protected $_categoryModal;

      public function __construct(
      MagentoCatalogModelCategory $categoryModal,
      )


      $this->_categoryModal = $categoryModal;


      public function toOptionArray()


      $productCategorys = $this->_categoryModal->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active', 1);
      $productCategory = [];
      foreach ($productCategorys as $category)
      $productCategory[] = [ 'value' => $category->getId(), 'label' => $category->getName() ];

      return $productCategory;




      2.form field add into your _prepareForm method



      $fieldset->addField(
      'cat_id',
      'select',
      array(
      'label' => $this->__("category"),
      'class' => 'required-entry',
      'required' => 'true',
      'name' => 'cat_id',
      'values' => $this->toOptionArray()
      )
      );



      1. Now please remove the generated and var folder using below command line



        rm -rf generated var/







      share|improve this answer

























        0












        0








        0







        Any custom module get the collection and shown in the select form field



        1. First, add the below code in your block


         protected $_categoryModal;

        public function __construct(
        MagentoCatalogModelCategory $categoryModal,
        )


        $this->_categoryModal = $categoryModal;


        public function toOptionArray()


        $productCategorys = $this->_categoryModal->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active', 1);
        $productCategory = [];
        foreach ($productCategorys as $category)
        $productCategory[] = [ 'value' => $category->getId(), 'label' => $category->getName() ];

        return $productCategory;




        2.form field add into your _prepareForm method



        $fieldset->addField(
        'cat_id',
        'select',
        array(
        'label' => $this->__("category"),
        'class' => 'required-entry',
        'required' => 'true',
        'name' => 'cat_id',
        'values' => $this->toOptionArray()
        )
        );



        1. Now please remove the generated and var folder using below command line



          rm -rf generated var/







        share|improve this answer













        Any custom module get the collection and shown in the select form field



        1. First, add the below code in your block


         protected $_categoryModal;

        public function __construct(
        MagentoCatalogModelCategory $categoryModal,
        )


        $this->_categoryModal = $categoryModal;


        public function toOptionArray()


        $productCategorys = $this->_categoryModal->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active', 1);
        $productCategory = [];
        foreach ($productCategorys as $category)
        $productCategory[] = [ 'value' => $category->getId(), 'label' => $category->getName() ];

        return $productCategory;




        2.form field add into your _prepareForm method



        $fieldset->addField(
        'cat_id',
        'select',
        array(
        'label' => $this->__("category"),
        'class' => 'required-entry',
        'required' => 'true',
        'name' => 'cat_id',
        'values' => $this->toOptionArray()
        )
        );



        1. Now please remove the generated and var folder using below command line



          rm -rf generated var/








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 13 at 18:54









        Rasik MiyaniRasik Miyani

        1329




        1329



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f182506%2fhow-to-get-all-active-categories-in-custom-module-admin-form-dropdown-in-magento%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form