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:
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Ibnab_BottonTopAction', __DIR__ );
Add module.xml at path app/code/Ibnab/BottonTopActionetc/module.xml and fill with:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd"> <module name="Ibnab_BottonTopAction" setup_version="1.0.0" schema_version="1.0.0"> </module> </config>
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 :
<?xml version="1.0"?> <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"> <update handle="catalog_product_new"/> <body> <referenceContainer name="page.main.actions"> <block class="Ibnab\BottonTopAction\Block\Adminhtml\Example" name="ibnab_button_top_action"/> </referenceContainer> </body> </page>
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:
<?php namespace Ibnab\BottonTopAction\Block\Adminhtml; class Example extends \Magento\Backend\Block\Widget\Form\Container { /** * Core registry * * @var \Magento\Framework\Registry */ protected $_coreRegistry = null; /** * Url * * @var \Magento\Framework\Url */ protected $_url; /** * Block group * * @var string */ protected $_blockGroup = 'Ibnab_BottonTopAction'; /** * @param \Magento\Backend\Block\Widget\Context $context * @param \Magento\Framework\Registry $registry * @param \Magento\Framework\Url $url * @param array $data */ public function __construct( \Magento\Backend\Block\Widget\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Url $url,array $data = [] ) { $this->_url = $url; $this->_coreRegistry = $registry; parent::__construct($context, $data); } protected function _construct() { $this->_objectId = 'product_id'; $this->_controller = 'adminhtml'; $this->_mode = 'example'; $this->buttonList->add( 'example', [ 'label' => __('Example'), 'class' => 'example', 'id' => 'catalog-product-edit-example', 'sort_order' => '-100', ] ); //parent::_construct(); } /** * Retrieve order model object * * @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->_coreRegistry->registry('current_product'); } /** * * @return string */ public function getPreviewUrl() { //$product = $this->getProduct()->load($this->getProduct()->getId()); $productUrl = $this->_url->getUrl('catalog/product/view', ['id' => $this->getProduct()->getId(), '_nosid' => true]); /* * Or * $productUrl = $this->_url->getUrl('catalog/product/view', ['id' => $this->getProduct()->getId(), '_nosid' => true, '_query' => ['___store' => 'default']]); */ return $productUrl; } }
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