0

I am creating a custom module. I want to assign Customer Group/s to one of my custom table.

In my Grid.php I did customization like this

Path : app\code\Myconpany\BusinessPartner\Block\Adminhtml\Businesspartner

In protected function _prepareColumns()

$this->addColumn(
        'customer_group',
        [
            'header' => __('Customer Group'),
            'index' => 'customer_group',
            'type' => 'options',
            'options' => $this->getOptionArray2()
        ]
    );

In getOptionArray2() function

public function getOptionArray2()
{
    $customerGroups = $this->_customerGroup->toOptionArray();
    
    $data_arrays=array(); 

    foreach($customerGroups as $cg){
     
      $data_arrays[$cg['value']]=$cg['label'];

    }

    return($data_arrays);
}

Above all my customer group/s are retrieve

In main.php file

$fieldset->addField(
        'customer_group',
        'select',
        [
            'label' => __('Customer Group'),
            'title' => __('Customer Group'),
            'name' => 'customer_group',
            
            'options' => $this->getOptionArray2(),
            'disabled' => $isElementDisabled
        ]
    );  

But my customer group/s are not loading in the form. enter image description here

1 . What I missed here?

2 . Is there any way to get customer group?

  • What is $this->_customerGroup's type? – Zachary Craig Feb 22 '18 at 12:19
  • that i am getting from this \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup, – Mujahidh Feb 22 '18 at 12:24
  • Is there any reason you're not passing that directly to options in your fieldset initialization? I would assume you don't need to modify anything and that ->toOptionArray() would be what you pass to a fieldset directly. – Zachary Craig Feb 22 '18 at 12:31
  • previously getOptionArray2 is a static function.in static function i cannot use $this. – Mujahidh Feb 22 '18 at 13:09
  • Have you defined getOptionArray2() in your main.php file? – Vivek Kumar Feb 22 '18 at 13:35
2

Thank you for attempting me to help in this.

Here what i missed is not correctly trying to access my getOptionArray2() function in main.php from grid.php. I fixed this with some changes in my code. I will explain in detail so future users can use this approach.

In Grid.php i want to get the customer groups.i will mention the essential steps to achieve this.Rest of the codes are same.

<?php
namespace Myconpany\BusinessPartner\Block\Adminhtml\Businesspartner;

class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
//Other protected variables
/**
 * Customer Group
 *
 * @var \Magento\Customer\Model\ResourceModel\Group\Collection
 */
protected $_customerGroup;

public function __construct(
//--**--//
\Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup,
    array $data = []
) {
    $this->_customerGroup = $customerGroup; 
    parent::__construct($context, $backendHelper, $data);
}

//In _**prepareColumns** function
protected function _prepareColumns()
{
    /* codes going here */

    $this->addColumn(
        'customer_group',
        [
            'header' => __('Customer Group'),
            'index' => 'customer_group',
            'type' => 'options',
            'options' => $this->getOptionArray2()
        ]
    );
/* codes going here */
}

function getOptionArray2() : In Grid.php

public function getOptionArray2()
{

    $customerGroups = $this->_customerGroup->toOptionArray();

    $data_arrays=array(); 

    foreach($customerGroups as $cg){
      $data_arrays[$cg['value']]=$cg['label'];
    }
    return($data_arrays);        
}

Normally if we creating using any module creators, this function should be a static function.But i wrote it as public function.

My function is getValueArray2() : In Grid.php

public function getValueArray2()
{
    $data_array=array();
    foreach($this->getOptionArray2() as $k=>$v){
       $data_array[]=array('value'=>$k,'label'=>$v);        
    }
    return($data_array);
}

Normally if we creating using any module creators, this function should be a static function.But i wrote it as public function.

In Main.php where the form is populating we want to do some customization like this.Want to create an instance of my grid.php and access the function through this instance.

<?php
namespace Myconpany\BusinessPartner\Block\Adminhtml\Businesspartner\Edit\Tab;
/**
* Businesspartner edit form main tab
*/
class Main extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{ 
protected $_grid; 
public function __construct(\Eight25media\BusinessPartner\Block\Adminhtml\Businesspartner\Grid $grid,
    array $data = []
) {
    $this->_grid = $grid;
    parent::__construct($context, $registry, $formFactory, $data);
}

My _prepareForm function like this

protected function _prepareForm()
{ 
    /* codes going here */

    $fieldset->addField(
        'customer_group',
        'select',
        [
            'label' => __('Customer Group'),
            'title' => __('Customer Group'),
            'name' => 'customer_group',

            'options' => $this->_grid->getOptionArray2(),
            'disabled' => $isElementDisabled
        ]
    );  
/* codes going here */
return parent::_prepareForm();
}
| improve this answer | |
0

I simply did the following in my form.

public function __construct(
    ....
    \Magento\Customer\Ui\Component\Listing\Column\Group\Options $groups,
    array $data = []
) {
    ....
    $this->groups = $groups;
    parent::__construct($context, $registry, $formFactory, $data);
}

....

$fieldset->addField(
    'customer_group_id',
    'select',
    [
        'label'    => __('Group'),
        'name'     => 'customer_group_id',
        'required' => true,
        'values'   => $this->groups->toOptionArray(),
    ]
);

Hope this helps!

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.