We have seen various ways to edit, in few of the previous blogs. But we have still to see how can we add new blogs from admin.
Magento shows the Add New buttons just above the grid. So we will also display the “Add New Blog” button on the blog listing page.
For that we have to edit the ui component file for the grid because magento allows us to add buttons above the grid from ui component file itself. So we have to edit the view/adminhtml/ui_component/blogmanager_blog_listing.xml
Here as you can see we have added an item tag inside listing arguments to add the button. Similarly you can add multiple buttons. And as you can see the code for the button is very self-explanatory.
Now we need to create the controller action file for the new blog. Note that the url for the new blog will be blog/manage/new that means the action class will be New. But new is a reserve word so we won’t be able to create a class with that name. However magento allows us to create NewAction (append Action after New) class for this kind of situation and it internally manages it. That means the action class file will be Controller/Adminhtml/Manage/NewAction.php
The only thing to note here is $this->_forward(‘edit’); line. It forwards the current request to the edit action that means to the edit page. Note that it does not redirect but loads the edit page on the current url (that is the new page url). So it will show the edit page’s structure but form fields will be blank as shown below.

But you won’t be able to create new blog yet. For that we have edit the save controller i.e. Controller/Adminhtml/Manage/Save.php
Here we have added the else section to create the new blogs entry in the table.
Now you can add new blog as shown below,

As you can see here for the User column the we have a blank cell. It is because we are performing left join with the customer table to get the data based on id.
We can create a class for that column, or we can edit the collection class of the grid to show “Admin” instead of empty. If you recall the grid collection class is Model/ResourceModel/Blog/Grid/Collection.php
->columns(‘IFNULL(cgf.name, “Admin”) AS user_name’) this is the line that we have to focus on. The columns will create pseudo column. The actual sql query that we get at the end is like shown below,

Note that the filters will not work for the text “Admin” for the User column. We have to do some complex changes if we want the filter to work too. Which we will be ignoring for now.
Now the grid should look fine,

Next