In this article, we are going to discuss about applying condition on the massaction. The following are the steps to implement this.
Step 1: Display Massaction on the content Pages in Magento2
Step 2:Β Apply Condition to Show or Hide Newly Created Massaction
Lets start…
Step 1: Display Massaction on the content Pages in Magento2
Lets assume that you need to display massaction on the Admin – Content Pages.
Create file located at Thecoachsmb/ToggleMassaction/view/adminhtml/ui_component/cms_page_listing.xml
and content will be:-
<?xml version="1.0" encoding="UTF-8"?> <!-- /** * Copyright Β© Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ --> <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> Β <listingToolbar name="listing_top"> Β Β Β <massaction name="listing_massaction" class="Thecoachsmb\ToggleMassaction\Ui\Component\MassAction"> Β Β Β Β Β <action name="custommassaction"> Β Β Β Β Β Β Β <settings> Β Β Β Β Β Β Β Β Β <confirm> Β Β Β Β Β Β Β Β Β Β Β <message translate="true">Are you sure to perform Action for selected customers?</message> Β Β Β Β Β Β Β Β Β Β Β <title translate="true">Custom MassAction</title> Β Β Β Β Β Β Β Β Β </confirm> Β Β Β Β Β Β Β Β Β <url path="extension/controller/massAction"/> <!-- your custom controller to handle request --> Β Β Β Β Β Β Β Β Β <type>custommassaction</type> Β Β Β Β Β Β Β Β Β <label translate="true">Custom MassAction</label> Β Β Β Β Β Β Β </settings> Β Β Β Β Β </action> Β Β Β </massaction> Β </listingToolbar> </listing>
Step 2:Β Apply Condition to Show or Hide Newly Created Massaction
Create file at
Thecoachsmb\ToggleMassaction\Ui\Component\MassAction.php
and content will be :
<?php namespace Thecoachsmb\ToggleMassaction\Ui\Component; use Magento\Contact\Helper\Data; class MassAction extends \Magento\Ui\Component\MassAction { Β /** Β Β * @param \Magento\Framework\App\Helper\Context $context Β Β * @param \Magento\Customer\Model\Session $customerSession Β Β * @param CustomerViewHelper $customerViewHelper Β Β */ Β Β public function __construct( Β Β Β \Magento\Framework\App\Helper\Context $context, Β Β Β Data $contacthelper Β ) { Β Β Β $this->contacthelper = $contacthelper; Β Β Β parent::__construct($context); Β } Β Β public function prepare() Β { Β Β Β parent::prepare(); Β Β Β $result = $this->contacthelper->isEnabled(); Β Β Β if ($result == 0) { Β Β Β Β Β $config = $this->getConfiguration(); Β Β Β Β Β $notAllowedActions = ['custommassaction']; Β Β Β Β Β $allowedActions = []; Β Β Β Β Β foreach ($config['actions'] as $action) { Β Β Β Β Β Β Β if (!in_array($action['type'], $notAllowedActions)) { Β Β Β Β Β Β Β Β Β $allowedActions[] = $action; Β Β Β Β Β Β Β } Β Β Β Β Β } Β Β Β Β Β $config['actions'] = $allowedActions; Β Β Β Β Β $this->setData('config', (array)$config); Β Β Β } Β } }
We have applied the condition that the if the contact us module is enabled then show this massaction.