如何在管理产品编辑页面Magento2中创建选项卡和网格

时间:2017-01-05 07:49:54

标签: tabs grid magento2

我在这里使用事件观察者" controller_action_layout_render_before_adminhtml_catalog_product_edit"在产品选项卡中呈现自定义模块网格但不起作用。

这里我创建了Grid.php文件。

请帮忙。

  1. 我创建了以下观察文件。 应用程序/代码/命名空间/模块/观测/ AddStockMovementsTab.php 尝试在此文件中编写代码。

  2. 应用程序/代码/命名空间/模块/块/ Adminhtml /观看/ Grid.php

  3. namespace Namespace\Module\Block\Adminhtml\Watch;
    
    use Magento\Framework\Registry;
    use Magento\Backend\Block\Template\Context;
    use Magento\Store\Model\Store;
    use Magento\CatalogInventory\Model\Stock\Item;
    
    class Grid extends \Magento\Backend\Block\Widget\Grid\Extended {
    
    protected $_registry;
    
    /**
     * @var \Magento\Framework\Module\Manager
     */
    protected $moduleManager;
    
    /**
     * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory]
     */
    protected $_setsFactory;
    
    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $_productFactory;
    
    /**
     * @var \Magento\Catalog\Model\Product\Type
     */
    protected $_type;
    
    /**
     * @var \Magento\Catalog\Model\Product\Attribute\Source\Status
     */
    protected $_status;
    protected $_collectionFactory;
    
    /**
     * @var \Magento\Catalog\Model\Product\Visibility
     */
    protected $_visibility;
    
    /**
     * @var \Magento\Store\Model\WebsiteFactory
     */
    protected $_websiteFactory;
    
    /**
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Magento\Backend\Helper\Data $backendHelper
     * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
     * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $setsFactory
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param \Magento\Catalog\Model\Product\Type $type
     * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $status
     * @param \Magento\Catalog\Model\Product\Visibility $visibility
     * @param \Magento\Framework\Module\Manager $moduleManager
     * @param array $data
     *
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
     */
    public function __construct(
        Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Store\Model\WebsiteFactory $websiteFactory,
        \Namespace\Module\Model\ResourceModel\Watch\Collection $collectionFactory,
        \Magento\Framework\Module\Manager $moduleManager,
        Registry $registry,
        array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
        $this->_websiteFactory = $websiteFactory;
        $this->moduleManager = $moduleManager;
        $this->_registry = $registry;
        parent::__construct($context, $backendHelper, $data);
    }
    
    public function _construct()
    {
        parent::_construct();
    
        $this->setId('StockWatchGrid');
        $this->setSaveParametersInSession(true);
        $this->setFilterVisibility(!$this->getProduct());
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setUseAjax(false);
    }
    
    protected function _prepareCollection()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $collection = $this->_collectionFactory->load();
    
        if ($this->getProduct()) {
            $stockItem = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($this->getProduct()->getId());
            if ($stockItem->getId()) {
                $collection->addFieldToFilter('item_id', $stockItem->getId());
            }
        } else {
            $collection->joinProduct();
        }
    
        $this->setCollection($collection);
    
        return parent::_prepareCollection();
    }
    
    /**
     * @return $this
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    protected function _prepareColumns()
    {
    
        if (!$this->getProduct()) {
            $this->addColumn(
                'sku',
                [
                    'header'           => __('SKU'),
                    'index'            => 'sku',
                    'filter_index'     => 'product.sku',
                    'type'             => 'text',
                    'frame_callback'   => [$this, 'decorateSku'],
                    'header_css_class' => 'col-id',
                    'column_css_class' => 'col-id',
                ]
            );
        }
    
        $this->addColumn(
            'qty',
            [
                'header'        => __('Quantity'),
                'index'         => 'qty',
                'class' => 'sort_order'
            ]
        );
    
        $this->addColumn(
            'movement',
            [
                'header' => __('Movement'),
                'index'  => 'movement',
                'class' => 'sort_order'
            ]
        );
    
        $this->addColumn(
            'is_in_stock',
            [
                'header'  => __('In Stock'),
                'index'   => 'is_in_stock',
                'type'    => 'options',
                'options' => [
                    '1' => __('Yes'),
                    '0' => __('No'),
                ],
                'class' => 'sort_order'
            ]
        );
    
        $this->addColumn(
            'message',
            [
                'header' => __('Message'),
                'index'  => 'message',
                'class' => 'sort_order'
            ]
        );
    
        $this->addColumn(
            'user',
            [
                'header' => __('User'),
                'index'  => 'user',
                'class' => 'sort_order'
            ]
        );
    
        $this->addColumn(
            'created_at',
            [
                'header'       => __('Date'),
                'index'        => 'created_at',
                'type'         => 'datetime',
                'class' => 'sort_order'
            ]
        );
    
        return parent::_prepareColumns();
    }
    
    public function getProduct()
    {
        return $this->_registry->registry('product');
    }
    
    public function decorateSku($value, $row)
    {
        $html = sprintf(
            '<a href="%s" title="%s">%s</a>',
            $this->getUrl('catalog/product/edit', array('id' => $row->getProductId())), __('Edit Product'),
            $value
        );
    
        return $html;
    }}
    

0 个答案:

没有答案