0

Here I need to Add defaul value of my custom category attribute in all existing category while I install the script.

here is my code:

<?php

namespace CommercePundit\HtmlSitemap\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;


class UpgradeData implements UpgradeDataInterface {

private $eavSetupFactory;

public function __construct(EavSetupFactory $eavSetupFactory) {
    $this->eavSetupFactory = $eavSetupFactory;
}

public function upgrade( ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    if ( version_compare( $context->getVersion(), '1.0.1', '<' ) ) {

        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'category_sitemap' , [
            'type' => 'int',
            'label' => 'Include in HTML Sitemap',
            'input' => 'select',
            'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
            'default' => '1',
            'sort_order' => 11,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'used_in_product_listing' => true, 
            'visible_on_front' => true, 
            'is_used_in_grid' => true, 
            'is_visible_in_grid' => true,
        ]);
    }
}
}

And if it is wrong way that what I am doing then please suggest me the way of achieving this.

1 Answer 1

1

You need add category_form.xml in app/code/[Name_space]/[Your_module]/view/adminhtml/ui_component with the following content:

<?xml version="1.0" encoding="UTF-8"?>
    <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="new_attribute">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Config\Model\Config\Source\Yesno</item>
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">60</item>
                    <item name="dataType" xsi:type="string">string</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="default" xsi:type="string">No</item>
                    <item name="label" xsi:type="string" translate="true">New Attribute</item>
                </item>
            </argument>
        </field>    
    </fieldset>
</form>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.