/   /   /  Magento 2 For Developer : Add top action button to product edit page

Note:

For more extensions and themes visit our store

Magento 2 For Developer : Add top action button to product edit page


Many time you want adding action button in admin product view or cms page view or other to accomplish some tasks how you can do that ?

First your module is Ibnab_BottonTopAction ,  add registration.php at path app/code/Ibnab/BottonTopAction/registration.php and push:
 

  1.  
  2. <?php
  3. \Magento\Framework\Component\ComponentRegistrar::register(
  4.     \Magento\Framework\Component\ComponentRegistrar::MODULE,
  5.     'Ibnab_BottonTopAction',
  6.     __DIR__
  7. );
  8.  

Add module.xml at path  app/code/Ibnab/BottonTopActionetc/module.xml and fill with:
 

  1.  
  2. <?xml version="1.0"?>
  3. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
  4.     <module name="Ibnab_BottonTopAction" setup_version="1.0.0" schema_version="1.0.0">
  5.     </module>
  6. </config>
  7.  

Now starting add our top action button by layout catalog_product_edit at path app/code/Ibnab/BottonTopAction/view/adminhtml/layout/catalog_product_edit.xml
 and fill :
 

  1.  
  2. <?xml version="1.0"?>
  3. <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  4.     <update handle="catalog_product_new"/>
  5.     <body>
  6.         <referenceContainer name="page.main.actions">
  7.            <block class="Ibnab\BottonTopAction\Block\Adminhtml\Example" name="ibnab_button_top_action"/>
  8.         </referenceContainer>
  9.     </body>
  10. </page>
  11.  

So What’s means ? Easy you have added new block at container  page.main.actions which responsible of top bar action the name of the block is  ibnab_button_top_action .

You should create the block  Ibnab\BottonTopAction\Block\Adminhtml\Example in the path app/code/Ibnab/BottonTopAction/Block/Adminhtml/Example.php:
 

  1.  
  2. <?php
  3. namespace Ibnab\BottonTopAction\Block\Adminhtml;
  4. class Example extends \Magento\Backend\Block\Widget\Form\Container {
  5.     /**
  6.      * Core registry
  7.      *
  8.      * @var \Magento\Framework\Registry
  9.      */
  10.     protected $_coreRegistry = null;
  11.     /**
  12.      * Url
  13.      *
  14.      * @var \Magento\Framework\Url
  15.      */
  16.     protected $_url;
  17.     /**
  18.      * Block group
  19.      *
  20.      * @var string
  21.      */
  22.     protected $_blockGroup = 'Ibnab_BottonTopAction';
  23.     /**
  24.      * @param \Magento\Backend\Block\Widget\Context $context
  25.      * @param \Magento\Framework\Registry $registry
  26.      * @param \Magento\Framework\Url $url
  27.      * @param array $data
  28.      */
  29.     public function __construct(
  30.     \Magento\Backend\Block\Widget\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Url $url,array $data = []
  31.     ) {
  32.         $this->_url = $url;
  33.         $this->_coreRegistry = $registry;
  34.         parent::__construct($context, $data);
  35.     }
  36.     protected function _construct() {
  37.         $this->_objectId = 'product_id';
  38.         $this->_controller = 'adminhtml';
  39.         $this->_mode = 'example';
  40.             $this->buttonList->add(
  41.                 'example', [
  42.                 'label' => __('Example'),
  43.                 'class' => 'example',
  44.                 'id' => 'catalog-product-edit-example',
  45.                 'sort_order' => '-100',
  46.                 'on_click' => sprintf("window.open('%s');", $this->getPreviewUrl()),
  47.                  ]
  48.             );
  49.         //parent::_construct();
  50.     }
  51.     /**
  52.      * Retrieve order model object
  53.      *
  54.      * @return \Magento\Catalog\Model\Product
  55.      */
  56.     public function getProduct()
  57.     {
  58.         return $this->_coreRegistry->registry('current_product');
  59.     }
  60.     /**
  61.      *
  62.      * @return string
  63.      */
  64.     public function getPreviewUrl() {
  65.         //$product = $this->getProduct()->load($this->getProduct()->getId());
  66.         $productUrl = $this->_url->getUrl('catalog/product/view', ['id' => $this->getProduct()->getId(), '_nosid' => true]);
  67.         /*
  68.          * Or
  69.          * $productUrl = $this->_url->getUrl('catalog/product/view', ['id' => $this->getProduct()->getId(), '_nosid' => true, '_query' => ['___store' => 'default']]);
  70.          */
  71.         return $productUrl;
  72.     }
  73. }
  74.  

The code on top using $this->buttonList to add your action button , and see the param onclick which use javascript function window.open('%s'); with param $this->getPreviewUrl()  , the getPreviewUrl() is a function with return the url of product in the frontend .

Comments

Related Posts

make your store more efficient

Solving problems. With open source technology. Professional results. That’s what makes Ibnab your best choice

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.