<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Anuradha Fernando</title>
    <description>The latest articles on DEV Community by Anuradha Fernando (@anuradha).</description>
    <link>https://dev.to/anuradha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F480037%2Fdab89350-eb63-49d2-9d1f-508789179224.jpeg</url>
      <title>DEV Community: Anuradha Fernando</title>
      <link>https://dev.to/anuradha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anuradha"/>
    <language>en</language>
    <item>
      <title>Preventing Category URL from the product page in Magento2</title>
      <dc:creator>Anuradha Fernando</dc:creator>
      <pubDate>Thu, 17 Dec 2020 15:17:25 +0000</pubDate>
      <link>https://dev.to/anuradha/preventing-category-url-from-the-product-page-in-magento2-4d58</link>
      <guid>https://dev.to/anuradha/preventing-category-url-from-the-product-page-in-magento2-4d58</guid>
      <description>&lt;p&gt;Magento is providing the best tool for managing the product URLs which is the URL Rewrite mechanism. It enables to convert the product URLs to more readable when visitors access the Target URLs.&lt;/p&gt;

&lt;p&gt;eg: &lt;br&gt;
Target URL&lt;br&gt;
/catalog/product/view/id/21&lt;/p&gt;

&lt;p&gt;Request URL&lt;br&gt;
/mens/this-is-a-nic-url.html&lt;/p&gt;

&lt;p&gt;So using both types of URLs will result the same product page in the frontend. Sometimes you might need to prevent accessing one URL type,&lt;/p&gt;

&lt;p&gt;eg: if you need to prevent access to the Target URL but allows the Request URL, we can handle that scenario as follows.&lt;/p&gt;

&lt;p&gt;I've created a sample module for this.&lt;/p&gt;

&lt;p&gt;Directory Structure&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vendor&lt;br&gt;
-Module Name&lt;br&gt;
--etc&lt;br&gt;
--Observer&lt;br&gt;
--registration.php&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First Create the module.xml and registration.php&lt;/p&gt;

&lt;p&gt;Inside the &lt;strong&gt;etc&lt;/strong&gt;, create a directory &lt;strong&gt;frontend&lt;/strong&gt; and create a file &lt;strong&gt;events.xml&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;events.xml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;!--
/**
 * Copyright (c)
 */
--&amp;gt;
&amp;lt;config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"&amp;gt;
    &amp;lt;event name="controller_action_predispatch_catalog_product_view"&amp;gt;
        &amp;lt;observer name="vendor_predispatch" instance="Vendor\Module\Observer\PreDispatch" /&amp;gt;
    &amp;lt;/event&amp;gt;
&amp;lt;/config&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a directory &lt;strong&gt;Observer&lt;/strong&gt; in the module root. Create a file inside the &lt;strong&gt;Observer&lt;/strong&gt; directory &lt;strong&gt;PreDispatch.php&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PreDispatch.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * Copyright (c)
 */

namespace Vendor\Module\Observer;

use Magento\Framework\App\Request\Http;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Context;

class PreDispatch implements ObserverInterface
{
    /**
     * Uri value to be checked with
     */
    const NEEDLE = '/catalog/';

    /**
     * @var ResponseInterface
     */
    private $response;

    /**
     * @var Context
     */
    private $context;

    /**
     * @var Http
     */
    private $http;

    /**
     * @var UrlInterface
     */
    private $url;

    /**
     * CatalogCategoryViewPostDispatch constructor.
     *
     * @param ResponseInterface $response
     * @param Context $context
     * @param Http $http
     * @param UrlInterface $url
     */
    public function __construct(
        ResponseInterface $response,
        Context $context,
        Http $http,
        UrlInterface $url
    ) {
        $this-&amp;gt;response = $response;
        $this-&amp;gt;context = $context;
        $this-&amp;gt;http = $http;
        $this-&amp;gt;url = $url;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer)
    {
        if (substr($this-&amp;gt;http-&amp;gt;getRequestUri(), 0, 9) === self::NEEDLE) {
            $customerBeforeAuthUrl = $this-&amp;gt;url-&amp;gt;getUrl('noroute');
            $this-&amp;gt;response-&amp;gt;setRedirect($customerBeforeAuthUrl)
            -&amp;gt;sendResponse();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>magento2</category>
      <category>urlrewite</category>
    </item>
    <item>
      <title>Add a new customized column to the grid...</title>
      <dc:creator>Anuradha Fernando</dc:creator>
      <pubDate>Tue, 08 Dec 2020 12:53:40 +0000</pubDate>
      <link>https://dev.to/anuradha/add-a-new-customized-column-to-the-grid-1c3b</link>
      <guid>https://dev.to/anuradha/add-a-new-customized-column-to-the-grid-1c3b</guid>
      <description>&lt;p&gt;You might need to add a custom column to the admin sales_order_grid. Please follow the below steps.&lt;/p&gt;

&lt;p&gt;I've created a sample module for this. The module will be having the below file structure.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;app/code/Vendor/Module/registration.php&lt;br&gt;
app/code/Vendor/Module/etc/module.xml&lt;br&gt;
app/code/Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml&lt;br&gt;
app/code/Vendor/Module/view/adminhtml/web/js/ui/grid/cells/custom-column.js&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In order to customize the 'custom-column' we should add some files to the app/design/adminhtml.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;app/design/adminhtml/Vendor/backend/Magento_Sales/layout/sales_order_index.xml&lt;br&gt;
app/design/adminhtml/Vendor/backend/Magento_Sales/ui_component/sales_order_grid.xml&lt;br&gt;
app/design/adminhtml/Vendor/backend/Magento_Sales/web/css/custom-column.css&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'm not explaining the module creation as you all know about that part. Therefore let's take a look at the other main files such as &lt;em&gt;Vendor_Module/view/adminhtml/ui_component/sales_order_grid.xml&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Vendor_Module/view/adminhtml/web/js/ui/grid/cells/custom-column.js&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sales_order_grid.xml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;!--
/**
 * Copyright (c)
 * See COPYING.txt for license details.
 */
--&amp;gt;
&amp;lt;listing 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"&amp;gt;
    &amp;lt;columns name="sales_order_columns"&amp;gt;
        &amp;lt;column name="custom_column"&amp;gt;
            &amp;lt;argument name="data" xsi:type="array"&amp;gt;
                &amp;lt;item name="config" xsi:type="array"&amp;gt;
                    &amp;lt;item name="sortable" xsi:type="boolean"&amp;gt;false&amp;lt;/item&amp;gt;
                    &amp;lt;item name="has_preview" xsi:type="string"&amp;gt;1&amp;lt;/item&amp;gt;
                    &amp;lt;item name="label" 
xsi:type="string" translate="true"&amp;gt;Custom Column&amp;lt;/item&amp;gt;
                &amp;lt;/item&amp;gt;
            &amp;lt;/argument&amp;gt;
        &amp;lt;/column&amp;gt;
    &amp;lt;/columns&amp;gt;
&amp;lt;/listing&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;custom-column.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Copyright (c)
 * See COPYING.txt for license details.
 */

define([
    'underscore',
    'knockout',
    'Magento_Ui/js/grid/columns/select'
], function (_, ko, Select) {
    'use strict';
    return Select.extend({
        getFieldClass: function (row) {
            var computed;
            computed = ko.pureComputed(function () {
                //ADD  YOUR CODE SNIPPET HERE
                }          
            }, this);
            return computed;
        }
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's take a practical scenario.&lt;br&gt;
eg: Showing two different colors 'Green' and 'Yellow' in 'Custom Column' based on the below scenarios.&lt;/p&gt;

&lt;p&gt;If the 'Grand Total' is &lt;strong&gt;greater than or equal to $50.00&lt;/strong&gt;, the 'Custom Column' color should be 'green'. Else color should be 'Yellow'.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;How we can implement this?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Edit the file: app/code/Vendor/Module/view/adminhtml/web/js/ui/grid/cells/custom-column.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Copyright (c)
 * See COPYING.txt for license details.
 */

define([
    'underscore',
    'knockout',
    'Magento_Ui/js/grid/columns/select'
], function (_, ko, Select) {
    'use strict';

    return Select.extend({
        getFieldClass: function (row) {
            var computed;
            computed = ko.pureComputed(function () {
                var grandTotal = row['grand_total'].replace(/\$/g,'');
                var customColCss = {
                    'custom-column-green': ko.observable(
grandTotal &amp;gt;= '50.00'?true:false
),
                    'custom-column-yellow': ko.observable(
grandTotal&amp;lt;'50.00'?true:false
)
                }
                return customColCss;

            }, this);
            return computed;
        }
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;app/design/adminhtml/Vendor/backend/Magento_Sales/layout/sales_order_index.xml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;!--
/**
 * Copyright (c)
 * See COPYING.txt for license details.
 */
--&amp;gt;
&amp;lt;page 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"&amp;gt;
    &amp;lt;head&amp;gt;
        &amp;lt;css src="Magento_Sales::css/custom-column.css" /&amp;gt;
    &amp;lt;/head&amp;gt;
&amp;lt;/page&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;app/design/adminhtml/Vendor/backend/Magento_Sales/ui_component/sales_order_grid.xml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;!--
/**
 * Copyright (c)
 * See COPYING.txt for license details.
 */
--&amp;gt;
&amp;lt;listing 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"&amp;gt;
    &amp;lt;columns name="sales_order_columns"&amp;gt;
        &amp;lt;column name="custom_column" component="Vendor_Module/js/ui/grid/cells/custom-column"&amp;gt;
            &amp;lt;argument name="data" xsi:type="array"&amp;gt;
                &amp;lt;item name="config" xsi:type="array"&amp;gt;
                    &amp;lt;item name="visibleByDefault" xsi:type="boolean"&amp;gt;true&amp;lt;/item&amp;gt;
                    &amp;lt;item name="sortOrder" xsi:type="number"&amp;gt;110&amp;lt;/item&amp;gt;
                &amp;lt;/item&amp;gt;
            &amp;lt;/argument&amp;gt;
        &amp;lt;/column&amp;gt;
    &amp;lt;/columns&amp;gt;
&amp;lt;/listing&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;app/design/adminhtml/Vendor/backend/Magento_Sales/web/css/custom-column.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.custom-column-yellow {
    background: #ffff00 !important;
}

.custom-column-green {
    background: #006400 !important;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output as follows:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--55xbW25r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/s4gevbbgg626tk5ayc8q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--55xbW25r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/i/s4gevbbgg626tk5ayc8q.png" alt="Alt Text" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>magento2grid</category>
    </item>
    <item>
      <title>Get nearest time slot in the past (in Magento2)</title>
      <dc:creator>Anuradha Fernando</dc:creator>
      <pubDate>Sun, 22 Nov 2020 03:54:16 +0000</pubDate>
      <link>https://dev.to/anuradha/get-nearest-time-slot-in-the-past-in-magento2-m91</link>
      <guid>https://dev.to/anuradha/get-nearest-time-slot-in-the-past-in-magento2-m91</guid>
      <description>&lt;p&gt;Let's assume that you have a list of Time Slots generated within the 30 minutes intervals. You need to get the closest time slot in the past based on the current server time.&lt;/p&gt;

&lt;p&gt;eg: &lt;br&gt;
Start Time: 14:00&lt;br&gt;
End Time: 18:00&lt;br&gt;
Interval: 30 (minutes)&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;14:00
14:30
15:00
15:30
16:00
16:30
17:00
17:30
18:00
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Current time: 17:26 (you can get the current server time from the Magento DateTimeFactory)&lt;/p&gt;

&lt;p&gt;So the output will be &lt;strong&gt;17:00&lt;/strong&gt;.&lt;br&gt;
Why? because we have 30minutes intervals in our time list. Therefore according to the current time &lt;em&gt;17:26&lt;/em&gt; the previous closest time slot is &lt;em&gt;17:00&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How to get the current Date/Time from Magento core&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use \Magento\Framework\Stdlib\DateTime\DateTimeFactory;

/**
 * @var DateTimeFactory
*/
protected $dateTimeFactory;

public function __construct(
    DateTimeFactory $dateTimeFactory
)
{
    $this-&amp;gt;dateTimeFactory = $dateTimeFactory;
}

public function getClosestTimeSlot()
{
    $dateModel = $this-&amp;gt;dateTimeFactory-&amp;gt;create();
    $currTime = date('H:i', strtotime($dateModel-&amp;gt;gmtDate()));

    $openingTime = '14:00';
    $closingTime = '18:00';
    $interval = 30; //in minutes

    $timeSlots = [];
    //set the intervals based on the $timeSlotLength
    $nextInterval = '+' . $interval . 'minutes';

    $strOpeningTime = strtotime($openingTime);
    $strClosingTime = strtotime($closingTime);

    while ($strOpeningTime &amp;lt;= $strClosingTime) {
       array_push($timeSlots, date('H:i', 
       $strOpeningTime));
       $strOpeningTime = strtotime($nextInterval, $strOpeningTime);
    }  

    //sets the array to be returned with the lowest time slot 
      from time slot array
    $timeArray = [
        'lower '=&amp;gt; min(current($timeSlots), $currTime)
    ];

    //store the closest lower time slot to the $timeArray
    foreach ($timeSlots as $ts) {
        if ($currTime &amp;gt; $ts)
            $timeArray['lower'] = $ts;
    }

    return $timeArray['lower']; //output will be 17:00
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;cheers!!&lt;/p&gt;

</description>
      <category>magento2</category>
    </item>
    <item>
      <title>Adding a custom attribute to a category doesn't show store specific value</title>
      <dc:creator>Anuradha Fernando</dc:creator>
      <pubDate>Sun, 01 Nov 2020 18:52:36 +0000</pubDate>
      <link>https://dev.to/anuradha/adding-a-custom-attribute-to-a-category-doesn-t-show-store-specific-value-4n13</link>
      <guid>https://dev.to/anuradha/adding-a-custom-attribute-to-a-category-doesn-t-show-store-specific-value-4n13</guid>
      <description>&lt;p&gt;When adding a custom attribute to a category in the admin, Store Scope will not show even though we set the Store View in the setup script.&lt;br&gt;
&lt;code&gt;Vendor/Module/Setup/Patch/Data/AddViewModeCategoryAttribute.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$eavSetup-&amp;gt;addAttribute(
            \Magento\Catalog\Model\Category::ENTITY,
            'attribute_code',
            [
                'type' =&amp;gt; 'text',
                'label' =&amp;gt; 'ATTRIBUTE LABLE',
                'input' =&amp;gt; 'select',
                'sort_order' =&amp;gt; 333,
                'source' =&amp;gt; 'Vendor\Module\Model\Category\Attribute\Source\NAME',
                'global' =&amp;gt; \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible' =&amp;gt; true,
                'required' =&amp;gt; false,
                'user_defined' =&amp;gt; false,
                'default' =&amp;gt; 'grid',
                'group' =&amp;gt; 'General Information',
                'backend' =&amp;gt; ''
            ]
        );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Expected result&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Should see [Store View] label under the attribute label.&lt;/li&gt;
&lt;li&gt;Should be able to see different values per scope level (global, website, store)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Actual result&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Can't see the [Store View] label under the attribute label.&lt;/li&gt;
&lt;li&gt;Can't see different values per scope level (global, website, store)&lt;/li&gt;
&lt;li&gt;Can see different values in the database&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is a Magento2 bug and not yet fixed in 2.3&lt;/p&gt;

&lt;p&gt;As a fix, please use the below code.&lt;/p&gt;

&lt;p&gt;Add below code into your module's di.xml&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;preference for="Magento\Catalog\Model\Category\DataProvider" type="Vendor\Module\Model\Category\DataProvider" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to add the 'Use Default Value' checkbox for custom attributes, we need to override DataProvider.php. There is a function called 'getFieldsMap'. So after modifying the 'getFieldsMap' function 'DataProvider' class will be as follows.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vendor/Module/Model/Category/DataProvider.php&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace Vendor\Module\Model\Category;

use Magento\Catalog\Model\Category\DataProvider as CategoryDataProvider;

class DataProvider extends CategoryDataProvider
{
    protected function getFieldsMap() {
        $parentFieldMap = parent::getFieldsMap();       

        array_push($parentFieldMap['general'], 'attribute_code');
        return $parentFieldMap;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;replace the 'attribute_code' with the actual attribute code&lt;br&gt;
'general' =&amp;gt; section in admin panel form, where attribute is displayed&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Run the setup upgrade command and clear the cache. You will be able to set &lt;strong&gt;store&lt;/strong&gt; specific values for custom attributes.&lt;/p&gt;

&lt;p&gt;cheers!!&lt;/p&gt;

</description>
      <category>magento2</category>
    </item>
    <item>
      <title>How to pass a parameter from one controller to another in Magento2 Admin form</title>
      <dc:creator>Anuradha Fernando</dc:creator>
      <pubDate>Sun, 25 Oct 2020 14:29:28 +0000</pubDate>
      <link>https://dev.to/anuradha/how-to-pass-a-parameter-from-one-controller-to-another-in-magento2-admin-form-k1l</link>
      <guid>https://dev.to/anuradha/how-to-pass-a-parameter-from-one-controller-to-another-in-magento2-admin-form-k1l</guid>
      <description>&lt;p&gt;Do you want to pass a parameter when clicking the save button in the admin form? It's not just like a hidden value to append to the form URL. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;http://www.m235p2.com/admin/module/module/new/order_id/1/key/db2ee099a204cb0d706bf711e49830ef693cb0c763baf306&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We are in the edit form and we have "order_id" with us, we are going to pass this order_id to "save controller" when clicking the Save button.&lt;/p&gt;

&lt;p&gt;Add GenericButton.php (if not exists).&lt;br&gt;
Vendor/Module/Block/Adminhtml/Module/Edit/GenericButton.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * Copyright © Nose All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Vendor\Module\Block\Adminhtml\Module\Edit;

use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Vendor\Module\Api\Data\ModuleInterface;

abstract class GenericButton
{
    /**
     * @var Context
     */
    protected $context;

    /**
     * @var BlockRepositoryInterface
     */
    protected $blockRepository;

    /**
     * GenericButton constructor.
     * @param Context $context
     */
    public function __construct(
        Context $context,
        BlockRepositoryInterface $blockRepository
    ) {
        $this-&amp;gt;context = $context;
        $this-&amp;gt;blockRepository = $blockRepository;
    }    

    /**
     * Generate url by route and parameters
     *
     * @param   string $route
     * @param   array $params
     * @return  string
     */
    public function getUrl($route = '', $params = [])
    {
        return $this-&amp;gt;context-&amp;gt;getUrlBuilder()-&amp;gt;getUrl($route, $params);
    }

    /**
     * Get order id from the URL
     *
     * @return mixed
     */
    public function getOrderId()
    {
        try {
            return $this-&amp;gt;context-&amp;gt;getRequest()-&amp;gt;getParam('order_id');
        } catch (NoSuchEntityException $e) {
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add SaveButton.php (if not exists)&lt;br&gt;
Vendor/Module/Block/Adminhtml/Module/Edit/SaveButton.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
/**
 * Copyright © Nose All rights reserved.
 * See LICENSE.txt for license details.
 */
namespace Vendor/Module/Block/Adminhtml/Module/Edit;

use Magento\Backend\Block\Widget\Context;
use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class SaveButton extends GenericButton implements ButtonProviderInterface
{
    /**
     * Url Builder
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $urlBuilder;

    /**
     * CustomButton constructor.
     *
     * @param \Magento\Backend\Block\Widget\Context $context
     */
    public function __construct(Context $context, BlockRepositoryInterface $blockRepository)
    {
        $this-&amp;gt;urlBuilder = $context-&amp;gt;getUrlBuilder();
        parent::__construct($context, $blockRepository);
    }

    /**
     * Set order_id to save button as a parameter
     *  
     * @return array
     */
    public function getButtonData()
    {
        $data = [
            'label' =&amp;gt; __('Save'),
            'class' =&amp;gt; 'save',
            'on_click' =&amp;gt; '',
            'sort_order' =&amp;gt; 20,
            'data_attribute' =&amp;gt; [
                'mage-init' =&amp;gt; [
                    'Magento_Ui/js/form/button-adapter' =&amp;gt; [
                        'actions' =&amp;gt; [
                            [
                                'targetName' =&amp;gt; 'vendor_module_module_form.vendor_module_module_form',
                                'actionName' =&amp;gt; 'save',
                                'params' =&amp;gt; [
                                    true,
                                    ['order_id' =&amp;gt; $this-&amp;gt;getOrderId()],
                                ]
                            ]
                        ]
                    ]
                ],

            ]
        ];

        return $data;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Save.php&lt;br&gt;
Vendor/Module/Controller/Adminhtml/Module/Save.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
* Save action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
    /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
    $resultRedirect = $this-&amp;gt;resultRedirectFactory-&amp;gt;create();
    $data = $this-&amp;gt;getRequest()-&amp;gt;getParams();
    $orderId = $this-&amp;gt;getRequest()-&amp;gt;getParam('order_id');

    //continue with the rest of the code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;cheers!!&lt;/p&gt;

</description>
      <category>magento2</category>
    </item>
  </channel>
</rss>
