I have created custom Drop down attribute for category with attribute_code is "is_homecategory"

Used below methods to create it.


  public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

                'type' => 'int',
                'label' => 'Is Home Category',
                'input' => 'select',
                'sort_order' => 333,
                'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'global' => 1,
                'visible' => true,
                'required' => false,
                'user_defined' => false,
                'default' => '0',
                'group' => 'General Information',
                'backend' => ''

and view/adminhtml/ui_component/category_from.xml

<fieldset name="general">
        <field name="is_homecategory">
            <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Magento\Eav\Model\Entity\Attribute\Source\Boolean</item>
                <item name="config" xsi:type="array">
                    <item name="required" xsi:type="boolean">false</item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="number">333</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="label" translate="true" xsi:type="string">Is Home Category</item>

That is created with Yes/No drop down in admin.

I am getting collection like below in by block file.

class Index extends \Magento\Framework\View\Element\Template {
protected $collectionFactory;
public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Catalog\Model\ResourceModel\Category $collectionFactory,
    array $data = []) {

    parent::__construct($context, $data);
    $this->collectionFactory = $collectionFactory;

public function getCategoryData(){

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
    ->setOrder('updated_at', 'desc')
    return $category;
    /* $categories =   $this->collectionFactory->create()
    return categories; */


Which is not returning any results to me.

All i am looking is I need to get only one category recently set as is_homecategory as 'Yes'.

Can i filter category collection in that way? Please anyone check it and help me. Thanks in Advance.

1 Answer 1


You need to complete addAttributeToFilter() bracket. Use this below code :

$category = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
    ->setOrder('updated_at', 'desc');

Use default argument in config for default set value

<item name="default" xsi:type="string">1</item>
  • hi, @Rohan, Thanks it worked for me, but default value is Showing 'Yes' for me. shall we make default as 'No'? Nov 13, 2018 at 7:38
  • Whenever i create create new category always it is selected with 'Yes' I need to make it "No'. How that can be done please? Nov 13, 2018 at 7:50
  • I will update my answer soon. Nov 13, 2018 at 7:54
  • please check my updated answer Nov 13, 2018 at 8:05
  • 1 is yes is it? I need to set as No Nov 13, 2018 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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