Skip to content

How to Add Dynamic Mass Action in Admin Grid for Assign a Customer Group Magento 2

How to Add Dynamic Mass Action in Admin Grid for Assign a Customer Group Magento 2

Vendor/Extension/view/adminhtml/ui_component/data_listing.xml

 <massaction name="listing_massaction">
   <action name="assign_to_group">
    <settings>
        <type>assign_to_group</type>
        <label translate="true">Assign a Customer Group</label>
        <actions class="VendoreExtensionUiComponentMassActionGroupOptions"/>
        <url path="mageefy_guest_to_customer/guest/massAssignGroup"/>
    </settings>
  </action>

Vendor/Extension/UI/Component/MassAction/Group/Options.php

  <?php

   namespace VendorExtensionUiComponentMassActionGroup;

   use MagentoFrameworkPhrase;
   use MagentoFrameworkUrlInterface;
   use MagentoCustomerModelResourceModelGroupCollectionFactory;

  class Options implements JsonSerializable
  {
/**
 * @var array
 */
protected $options;

/**
 * @var CollectionFactory
 */
protected $collectionFactory;

/**
 * Additional options params
 *
 * @var array
 */
protected $data;

/**
 * @var UrlInterface
 */
protected $urlBuilder;

/**
 * Base URL for subactions
 *
 * @var string
 */
protected $urlPath;

/**
 * Param name for subactions
 *
 * @var string
 */
protected $paramName;

/**
 * Additional params for subactions
 *
 * @var array
 */
protected $additionalData = [];

/**
 * Constructor
 *
 * @param CollectionFactory $collectionFactory
 * @param UrlInterface $urlBuilder
 * @param array $data
 */
public function __construct(
    CollectionFactory $collectionFactory,
    UrlInterface $urlBuilder,
    array $data = []
) {
    $this->collectionFactory = $collectionFactory;
    $this->data = $data;
    $this->urlBuilder = $urlBuilder;
}

/**
 * Get action options
 *
 * @return array
 */
#[ReturnTypeWillChange]
public function jsonSerialize()
{
    if ($this->options === null) {
        $options = $this->collectionFactory->create()->setRealGroupsFilter()->toOptionArray();
        $this->prepareData();
        foreach ($options as $optionCode) {
            $this->options[$optionCode['value']] = [
                'type' => 'customer_group_' . $optionCode['value'],
                'label' => __($optionCode['label']),
                '__disableTmpl' => true
            ];

            if ($this->urlPath && $this->paramName) {
                $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
                    $this->urlPath,
                    [$this->paramName => $optionCode['value']]
                );
            }

            // Log the generated URL
            error_log('Generated URL: ' . $this->options[$optionCode['value']]['url']);

            $this->options[$optionCode['value']] = array_merge_recursive(
                $this->options[$optionCode['value']],
                $this->additionalData
            );
        }

        $this->options = array_values($this->options);
    }

    return $this->options;
}

/**
 * Prepare additional data for subactions
 *
 * @return void
 */
protected function prepareData()
{
    foreach ($this->data as $key => $value) {
        switch ($key) {
            case 'urlPath':
                $this->urlPath = $value;
                // Log the urlPath
                error_log('urlPath: ' . $this->urlPath);
                break;
            case 'paramName':
                $this->paramName = $value;
                break;
            case 'confirm':
                foreach ($value as $messageName => $message) {
                    $this->additionalData[$key][$messageName] = (string) new Phrase($message);
                }
                break;
            default:
                $this->additionalData[$key] = $value;
                break;
          }
        }
     }
  }

i try many way but not working and not call my controller