0

I am creating a module in which I am trying to call different Module's Model collectioin to create dynamic collection in my Grid.php Magento 2

Grid.php

namespace THR\WarehouseCombination\Block\Adminhtml\Combination;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended { /** * @var \Magento\Framework\Module\Manager */ protected $moduleManager;

/**
 * @var \THR\WarehouseCombination\Model\combinationFactory
 */
protected $_combinationFactory;

/**
 * @var \THR\WarehouseCombination\Model\Status
 */
protected $_status;

protected $_combinationHelper;

/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Backend\Helper\Data $backendHelper
 * @param \THR\WarehouseCombination\Model\combinationFactory $combinationFactory
 * @param \THR\WarehouseCombination\Model\Status $status
 * @param \Magento\Framework\Module\Manager $moduleManager
 * @param \Magento\Framework\ObjectManagerInterface $objectmanager
 * @param array $data
 *
 * @SuppressWarnings(PHPMD.ExcessiveParameterList)
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,
    \THR\WarehouseCombination\Model\CombinationFactory $CombinationFactory,
    \THR\WarehouseCombination\Model\Status $status,
    \Magento\Framework\Module\Manager $moduleManager,
    \THR\WarehouseCombination\Helper\Data $combinationHelper
    array $data = []
) {
    $this->_combinationFactory = $CombinationFactory;
    $this->_status = $status;
    $this->moduleManager = $moduleManager;
    $this->_combinationHelper = $combinationHelper;

    parent::__construct($context, $backendHelper, $data);
}

/**
 * @return void
 */
protected function _construct() {
    parent::_construct();
    $this->setId('postGrid');
    $this->setDefaultSort('combination_id');
    $this->setDefaultDir('DESC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(false);
    $this->setVarNameFilter('post_filter');
}

/**
 * @return $this
 */
protected function _prepareCollection() {

    $collection = $this->_combinationFactory->create()->getCollection();
    $this->setCollection($collection);

    parent::_prepareCollection();

    return $this;
}

/**
 * @return $this
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
protected function _prepareColumns() {
    $this->addColumn(
        'combination_id',
        [
            'header' => __('ID'),
            'type' => 'number',
            'index' => 'combination_id',
            'header_css_class' => 'col-id',
            'column_css_class' => 'col-id'
        ]
    );




              $this->addColumn(
                 'warehouse_id',
                 [
                    'header' => __('warehouse_id'),
                    'index' => 'warehouse_id',
                    'type' => 'options',
                    'options' => \THR\WarehouseCombination\Block\Adminhtml\Combination\Grid::getOptionArray0()
                 ]
              );


        $this->addColumn(
           'warehouse_priority',
           [
              'header' => __('Warehouse Priority '),
              'index' => 'warehouse_priority',
           ]
        );




    //$this->addColumn(
        //'edit',
        //[
            //'header' => __('Edit'),
            //'type' => 'action',
            //'getter' => 'getId',
            //'actions' => [
                //[
                    //'caption' => __('Edit'),
                    //'url' => [
                        //'base' => '*/*/edit'
                    //],
                    //'field' => 'combination_id'
                //]
            //],
            //'filter' => false,
            //'sortable' => false,
            //'index' => 'stores',
            //'header_css_class' => 'col-action',
            //'column_css_class' => 'col-action'
        //]
    //);



     $this->addExportType($this->getUrl('warehousecombination/*/exportCsv', ['_current' => true]),__('CSV'));
     $this->addExportType($this->getUrl('warehousecombination/*/exportExcel', ['_current' => true]),__('Excel XML'));

    $block = $this->getLayout()->getBlock('grid.bottom.links');
    if ($block) {
        $this->setChild('grid.bottom.links', $block);
    }

    return parent::_prepareColumns();
}


/**
 * @return $this
 */
protected function _prepareMassaction() {

    $this->setMassactionIdField('combination_id');
    //$this->getMassactionBlock()->setTemplate('THR_WarehouseCombination::combination/grid/massaction_extended.phtml');
    $this->getMassactionBlock()->setFormFieldName('combination');

    $this->getMassactionBlock()->addItem(
        'delete',
        [
            'label' => __('Delete'),
            'url' => $this->getUrl('warehousecombination/*/massDelete'),
            'confirm' => __('Are you sure?')
        ]
    );

    $statuses = $this->_status->getOptionArray();

    $this->getMassactionBlock()->addItem(
        'status',
        [
            'label' => __('Change status'),
            'url' => $this->getUrl('warehousecombination/*/massStatus', ['_current' => true]),
            'additional' => [
                'visibility' => [
                    'name' => 'status',
                    'type' => 'select',
                    'class' => 'required-entry',
                    'label' => __('Status'),
                    'values' => $statuses
                ]
            ]
        ]
    );
    return $this;
}


/**
 * @return string
 */
public function getGridUrl() {
    return $this->getUrl('warehousecombination/*/index', ['_current' => true]);
}

/**
 * @param \THR\WarehouseCombination\Model\combination|\Magento\Framework\Object $row
 * @return string
 */
public function getRowUrl($row) {

    return $this->getUrl(
        'warehousecombination/*/edit',
        ['combination_id' => $row->getId()]
    );

}


  static public function getOptionArray0() {
     $data_array=array(); 
     $data_array[0]='NJNV';
     $data_array[1]='FBO';
        return($data_array);
  }

  static public function getValueArray0() {
        $data_array=array();
     foreach(\THR\WarehouseCombination\Block\Adminhtml\Combination\Grid::getOptionArray0() as $k=>$v){
           $data_array[]=array('value'=>$k,'label'=>$v);
     }
        return($data_array);

  }

  static public function getOptionArray2() {
     $data_array=array(); 
     $data_array[0]='NJPP';
     $data_array[1]='FBA';
     $data_array[2]='OFFICE';
     $data_array[3]='STORAGE';
        return($data_array);
  }

  static public function getValueArray2() {

        $data_array=array();

         $warehouse  =  $this->_combinationHelper->getCombinationWarehouse();

        foreach ($warehouse as $key => $_warehouse) {
            $data_array[] = [
                'label' => $_warehouse['title'],
                'value' => $_warehouse['warehouse_id']
            ];
        }

    /*  foreach(\THR\WarehouseCombination\Block\Adminhtml\Combination\Grid::getOptionArray2() as $k=>$v){
           $data_array[]=array('value'=>$k,'label'=>$v);
     } */
        return($data_array);

  }


  /* private function _getCombination() {


     // $collection =  $this->_objectManager->create('\Amasty\MultiInventory\Model\WarehouseFactory');

     // $warehouse  =  $collection->create()->getCollection()->addFieldToFilter('is_combination', array('eq' => 'Y'));

     $warehouse  =  $this->_combinationHelper->getCombinationWarehouse();

     // return $warehouse->getData();

     return $warehouse;


  } */

}

and In my Data.php Helper

namespace THR\WarehouseCombination\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context;

use Amasty\MultiInventory\Model\WarehouseFactory;

class Data extends AbstractHelper {

protected $_WarehouseFactory;

public function __construct( Context $context, WarehouseFactory $WarehouseFactory ) {

  parent::__construct($context);

  $this->_WarehouseFactory = $WarehouseFactory;

}

public function getCombinationWarehouse() {

  $resultPage = $this->_WarehouseFactory->create();

  $collection = $resultPage->getCollection()->addFieldToFilter('is_combination', array('eq' => 'Y')); //Get Collection of module data

  return $collection->getData();

}

}

and I am using It in my Main.php From

$fieldset->addField(
     'warehosue_list',
     'multiselect',
     [
        'label' => __('Warehouse List'),
        'title' => __('Warehouse List'),
        'name' => 'warehosue_list',

        'values' => \THR\WarehouseCombination\Block\Adminhtml\Combination\Grid::getValueArray2(),
        // 'values' => \THR\WarehouseCombination\Model\WarehouseBlocks\BlockCombinations,
        'disabled' => $isElementDisabled
     ]
  );

this is not working, I am not using ui data component trying to do in this style but not working can anybody help.

Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.