Displaying Custom Column in Customer Grid Based on System Setting
In Magento, you can customize the customer grid by adding new columns via XML overrides. However, if you want to conditionally display a column based on a system setting, you’ll need to use a combination of XML and PHP. In this post, we will guide you through the process.
Understanding the Current Setup
You have already added a custom column in your customer_listing.xml file:
<columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
<actionsColumn name="order_as_customer" class="Foo\Bar\Ui\Component\Listing\Column\CustomColumnData">
<settings>
<indexField>entity_id</indexField>
<label translate="true">Title</label>
</settings>
</actionsColumn>
</columns>
This setup creates a new column named “Order as Customer” using the custom class Foo\Bar\Ui\Component\Listing\Column\CustomColumnData.
Conditional Display Based on System Settings
To display this column only when the module is enabled, you need to create an observer that checks the system configuration value and hides or shows the column accordingly.
Step 1: Create a Custom Observer
Create a custom observer for magento_customer_grid_prepare_layout event in your module. This event is triggered before rendering the customer grid layout, giving us the opportunity to modify it.
public function execute(\Magento\Framework\Event\Observer $observer)
{
// Get the enabled status from system configuration
$enabled = (bool) $this->scopeConfig->getValue('your_module/general/enabled');
if (!$enabled) {
// If not enabled, remove the custom column
$columns = $observer->getLayout()->getChildBlock('customer_listing.customer_columns');
foreach ($columns as $column) {
if ($column->getName() === 'order_as_customer') {
$column->setIsActive(false);
}
}
}
return $this;
}
Note that you should replace 'your_module/general/enabled' with the path to your module’s enabled system setting.
Step 2: Register the Observer
Register this observer in your etc/events.xml file:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="magento_customer_grid_prepare_layout">
<observer name="your_module_remove_custom_column_if_disabled" instance="Your\Module\Observer\RemoveColumnIfDisabled"/>
</event>
</config>
Conclusion
In conclusion, to conditionally display a custom column in the customer grid based on a system setting, you need to create an observer that checks the setting and hides or shows the column before the grid is rendered. This approach allows you to maintain the flexibility of using XML for adding columns while keeping control over their visibility through PHP logic.
Remember to replace placeholders like 'your_module' with your actual module name throughout the code examples provided above.
For more detailed information on creating custom components and observers in Magento, we recommend checking out our article on Extending Magento UI Components for a comprehensive guide.