How to add new widgets in Magento 2

The following steps help you to add new widgets in Magento 2:

Step 1: Create a new category attribute with install schema file.

Step 2: Create an xml file.

 Step 1: Create a new category attribute with install schema file

app/code/Magestore/CategoryAttribute/Setup/InstallData.php
<?php

namespace Magestore\CategoryAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;

use Magento\Framework\Setup\ModuleContextInterface;

use Magento\Framework\Setup\ModuleDataSetupInterface;

use Magento\Eav\Setup\EavSetup;

use Magento\Eav\Setup\EavSetupFactory;

use Magento\Catalog\Model\Category;

use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;

/**

* @codeCoverageIgnore

*/

class InstallData implements InstallDataInterface

{

  /**

 * @var EavSetupFactory

 */

 private $eavSetupFactory;

  /**

*

* @param EavSetupFactory $eavSetupFactory

   */

 public function __construct(EavSetupFactory $eavSetupFactory)

 {

$this->eavSetupFactory = $eavSetupFactory;

 }

 public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)

 {

/** @var EavSetup $eavSetup */

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

 $eavSetup->addAttribute(

Category::ENTITY,

 'custom_attribute',

   [

 'type' => 'varchar',

'label' => 'Custom attribute',

'input' => 'text',

 'required' => false,

 'sort_order' => 100,

 'global' => ScopedAttributeInterface::SCOPE_STORE,

'group' => 'General Information',

]

  );

 }

 

app/code/Magestore/CategoryAttribute/view/adminhtml/ui_component/category_form.xml

Step 2: Create an xml file

<?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.xs

 <fieldset name="custom_content">

 <argument name="data" xsi:type="array">

 <item name="config" xsi:type="array">

 <item name="label" xsi:type="string" translate="true">Custom Content</item>

<item name="collapsible" xsi:type="boolean">true</item>

 <item name="sortOrder" xsi:type="number">100</item>

</item>

</argument>

 <field name="custom_attribute">

 <argument name="data" xsi:type="array">

<item name="config" xsi:type="array">

<item name="sortOrder" xsi:type="number">10</item>

<item name="dataType" xsi:type="string">string</item>

 <item name="formElement" xsi:type="string">input</item>

<item name="label" xsi:type="string" translate="true">Custom Attribute</item>

 <item name="required" xsi:type="boolean">false</item>

 </item>

</argument>

</field>

 </fieldset>

</form>

 

The above mentioned steps which help to add new widgets in Magento 2

Last Update: April 9, 2018  

February 8, 2018   96   Nandini R    Start With Magento 2    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Google+
http://www.webnexs.com/blog/kb/add-new-widgets-magento-2
Twitter
LinkedIn