2

I am adding a custom "category" model with children items in my module.

To add children I need to fetch collection from this "mycategories" module. How do I get a collection of categories when adding children items to show in a dropdown in the backend?

I tried using the model collection factory method:

$collection = $this->mymodulemodelFactory->create();

But I think it is for getting collection of the same model.

Note: By category and its children, i dont mean that they are Magento Catalog categories. They can be anything like Palettes and colors, Font family and fonts etc.

1
  • I edited your question and title based on your comment, to make clear that it is not about Magento categories. I'm still not sure how "show in a dropdown" and "add children" is relevant since you are asking for how to fetch a collection. If you need more help, please add more specific information about what you have and what you try to achieve. Commented Sep 7, 2016 at 6:37

2 Answers 2

1

List of all categories with sub-categories as an option array (with opt-group):

If you want to get the category tree, just use the method toOptionArray of the \Magento\Catalog\Ui\Component\Product\Form\Categories\Options class located in vendor/magento/module-catalog/Ui/Component/Product/Form/Categories/Options.php like this:

/**
 * @var \Magento\Catalog\Ui\Component\Product\Form\Categories\Options
 */
protected $categoriesOptions;

/**
 * @param \Magento\Catalog\Ui\Component\Product\Form\Categories\Options $categoriesOptions
 */
public function __construct(
    \Magento\Catalog\Ui\Component\Product\Form\Categories\Options $categoriesOptions
) {
    $this->categoriesOptions = $categoriesOptions;
}

public function getCategories()
{
    $values = $this->categoriesOptions->toOptionArray();

    return $values;
}

Result in this case should look like this (options with opt-groups):

Options with option groups


List of all categories as an option array:

Try this:

/**
 * @var \Magento\Catalog\Model\CategoryFactory
 */
protected $categoryFactory;

/**
 * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
 */
public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory
) {
    $this->categoryFactory = $categoryFactory;
}

public function getCategories()
{
    /** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $collection */
    $collection = $this->categoryFactory->create()->getCollection()->addAttributeToSelect('name');
    $values = $collection->toOptionArray(); // or use method toArray

    return $values;
}

Result should look like this:

result array


List of all categories with data as an array:

To get all the category data just replace toOptionArray() to toArray():

$values = $collection->toArray();

toArray result

2
  • @HungryDB Does my answer helped you? Commented Aug 16, 2016 at 10:32
  • i am not asking about Magento categories, i just want to show dropdown items as collection of another module.
    – HungryDB
    Commented Sep 7, 2016 at 5:57
0

You have to implement this parent-child relationship and a method like getChildCollection() yourself.

Usually you will have a database column parent_id and to load the children of a given parent, you filter by the parent id.

Example method for a resource model:

public function getChildCollection($parent)
{
    $collection = $this->collectionFactory->create();
    $collection->addFieldToFilter('parent_id', $parent->getId());
    return $collection;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.