How to get the custom module collection?

How to display custom module collection data at frontend?

How to use Blocks to display custom module collection data at frontend?

How to display custom module collection data in list page and view page?

Magento, data interfaces, data models, and models.

This post will cover above all questions. I suppose you have inserted data into your custom table using Model, Resource Model. If you haven’t done it you may want to check my previous post click here.

Display custom module data list page Magento 2

Display custom module data list page Magento 2

Display custom module data view page Magento 2

Display custom module data view page Magento 2

In the previous post, we have inserted data into our custom module using a form at frontend. Now I am going to continue where we left in the previous post. So for me, I have a custom module Vky_Test so files will be for that module. Let’s get started.

Let’s say I want to display my custom module collection data in a list. Like if I run the URL – www.magento2.com/test/index/listdata the page should show the list. So we don’t have listdata action for our index controller in the custom module Vky_Test.

Display custom module collection data in the list page

Create Vky/Test/Controller/Index/ListData.php

We are using the layout in the action so we need to create a layout file for the list page.

Create Vky/Test/view/frontend/layout/test_index_listdata.xml

In the layout file, we are using block – Vky\Test\Block\TestListData  and a phtml file – Vky_Test::listdata.phtml . So we need to create both.

Create Vky/Test/Block/TestListData.php

In the block file, we are setting a page title and getting the collection of our custom module in getTestCollection() function. We will return this collection from here so we can use it in our listdata.phtml file for the list page.

Create Vky/Test/view/frontend/templates/listdata.phtml

In this file, we are getting the collection using our TestListData block and displayed in the data table. This is it for the list page. Run below commands from your root directory.

Check the URL – www.magento2.com/test/index/listdata. You should have the list of your data in the data table. There is an action column in the data table which is for the view of a single data. So we need to work on that.

Display custom module collection data in the view page

To display a single record we want our URL to be this – www.magento2.com/test/index/view/id/1 where we are passing the id of the record in the URL. So we will need a View action, the layout file, a block, a phtml file.

Create Vky/Test/Controller/Index/View.php

Create Vky/Test/view/frontend/layout/test_index_view.xml

Create Vky/Test/Block/TestView.php

Here we are getting the id from the URL then load the single record and returning it to the view.phtml file using getSingleData() function.

Create Vky/Test/view/frontend/templates/view.phtml

It’s Done. Run below commands from your root directory.

Now you can click on the View from the action column in the data table. A single record should display on your view page.

That’s how you display data in Magento 2. I hope it helps. See you later.

Next: How to add pagination for custom collection in Magento 2
(Check the post)

 

No Comments

Post A Comment