Showing Store Name in Magento Admin Grid
In Magento, displaying store-specific data in an admin grid requires fetching the corresponding store name and website using the provided store ID. This allows you to display information like “Store Name” or “Website” alongside your custom grid values.
Fetching Store Information from the Database
To retrieve the store name and website details based on a given store ID, you can query the core_store table directly in your module’s collection model. Here’s an example of how to achieve this:
/** @var \Magento\Framework\App\ResourceConnection $resource */
$storeTable = $resource->getTableName('core_store');
$query = "SELECT name FROM {$storeTable} WHERE store_id = {$storeId}";
$connection = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
$result = $connection->fetchAll($query);
if ($result) {
$storeName = reset($result)['name'];
}
In this snippet, replace {$storeId} with the actual store ID you want to fetch information for.
Integrating Store Name in Your Grid
Now that you have fetched the necessary data, you can integrate the store name into your grid’s column. This involves modifying your module’s grid/columns.xml file and using a new custom action class to handle the display logic.
- Add a new
<column>element for your “Store” column in thecolumns.xmlfile:
<column name="store_column">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
...
<item name="filter" xsi:type="string">text</item>
<item name="label" translate="true" xsi:type="string">Store Name</item>
<item name="sortable" xsi:type="boolean">false</item>
</item>
</argument>
</column>
- Create a new custom action class to handle the store name display:
namespace Your\Namespace\Controller\Adminhtml\YourModule;
use Magento\Framework\App\Action\HttpGetActionInterface as ActionInterface;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\View\Result\Page;
use Magento\Backend\Model\View\Result\PageFactory;
class YourGridController extends \Magento\Backend\App\Action implements ActionInterface
{
protected $resultPageFactory = null;
public function __construct(
ActionInterface $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Store Grid'));
return $resultPage;
}
}
- Update your grid’s collection to include the store name in each row:
class YourModule\Block\Adminhtml\Grid extends \Magento\Backend\Block\Widget\Grid\Extended
{
protected $_collection;
public function __construct(
Context $context,
Registry $registry,
GridFactory $gridFactory,
CollectionFactory $collectionFactory,
array $data = []
) {
parent::__construct($context, $registry, $gridFactory, $data);
$this->_collection = $collectionFactory->create();
}
protected function _prepareCollection()
{
$collection = $this->_collection->addFieldToFilter('store_id', 5); // Replace with your filter logic
$this->setCollection($collection);
return parent::_prepareCollection();
}
}
Conclusion
Displaying store-specific information in a Magento admin grid requires fetching the relevant data using the provided store ID and integrating it into your custom grid configuration. By following this approach, you can create grids that display essential metadata alongside your custom values.
For more detailed guidance on Magento development best practices and real-world examples, visit https://laravelcompany.com.