1

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

Used below methods to create it.

Setup/InstallData.php

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

        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'is_homecategory',
            [
                '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>
                    <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>
                </item>
            </argument>
        </field>
    </fieldset>

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');
    $category->addAttributeToSelect('*')
    ->setPageSize(1)
    ->addAttributeToFilter('is_homecategory',['eq'=>1]
    ->setOrder('updated_at', 'desc')
    );
    return $category;
    /* $categories =   $this->collectionFactory->create()
    ->addAttributeToFilter('is_homecategory',1)->load();
    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.

2

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

$category = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\Collection');
    $category->addAttributeToSelect('*')
    ->setPageSize(1)
    ->addAttributeToFilter('is_homecategory',['eq'=>1])
    ->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'? – jafar pinjar Nov 13 '18 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? – jafar pinjar Nov 13 '18 at 7:50
  • I will update my answer soon. – Rohan Hapani Nov 13 '18 at 7:54
  • please check my updated answer – Rohan Hapani Nov 13 '18 at 8:05
  • 1 is yes is it? I need to set as No – jafar pinjar Nov 13 '18 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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