0

I am learning magento extention creation, and I try to add a "Create New" record in my admin grid. So far I made this grid:

namespace MageGuide\FirstModule\Block\Adminhtml;

use MageGuide\FirstModule\Model\Factories\BlogPost\CollectionFactory as BlogPostCollectionFactory;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Helper\Data;
use Magento\Framework\ObjectManagerInterface;
use Magento\Backend\Block\Widget\Grid\Extended;
use Magento\Framework\Registry;


class Grid extends Extended
{
// also you can use Magento Default CollectionFactory

    protected $registry;
    protected $_objectManager = null;
    protected BlogPostCollectionFactory $demoFactory;

    public function __construct(
        Context $context,
        Data $backendHelper,
        Registry $registry,
        ObjectManagerInterface $objectManager,
        BlogPostCollectionFactory $demoFactory,
        array $data = []
    ) {
        $this->_objectManager = $objectManager;
        $this->registry = $registry;
        $this -> demoFactory = $demoFactory;
        parent::__construct($context, $backendHelper, $data);
    }
    protected function _construct()
    {
        parent::_construct();
        $this->setId('blog_post_id');
        $this->setDefaultSort('created_at');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
    }
    protected function _prepareCollection()
    {
        $demo = $this->demoFactory->create()
            ->addFieldToSelect('*');

        $demo->addFieldToFilter('blog_post_id', array('neq' => ''));

        $this->setCollection($demo);

        return parent::_prepareCollection();
    }
    protected function _prepareColumns()
    {

        $this->addColumn(
            'id',
            [
                'header' => __('ID'),
                'type' => 'number',
                'index' => 'blog_post_id',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
            ]
        );
        $this->addColumn(
            'title',
            [
                'header' => __('Blog Post Title'),
                'type' => 'text',
                'index' => 'title',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
            ]
        );

        $this->addColumn(
            'skus',
            [
                'header' => __('Product Skus'),
                'type' => 'text',
                'index' => 'skus',
                'header_css_class' => 'col-id',
                'column_css_class' => 'col-id',
            ]
        );

        $this->addColumn(
            'created_at',
            [
                'header' => __('Created At'),
                'index' => 'creation_dt',
                'type' => 'datetime',
            ]
        );

        $this->addColumn(
            'updated_at',
            [
                'header' => __('Last Update Datetime'),
                'index' => 'update_dt',
                'type' => 'datetime',
            ]
        );

        return parent::_prepareColumns();
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/actionName', ['_current' => true]);
    }
}

And I load it via this contoller:


namespace MageGuide\FirstModule\Controller\Adminhtml\Index;

use MageGuide\FirstModule\Block\Adminhtml\Grid as AdminGrid;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    protected $resultPageFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    )
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
        $this->_view->loadLayout();
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('BlogPosts'));
        $resultPage->setActiveMenu('MageGuide_FirstModule::home');
        $resultPage->addBreadcrumb(__('BlogPosts'), __('BlogPosts'));
        $this->_addContent($this->_view->getLayout()->createBlock(AdminGrid::class));
        $this->_view->renderLayout();
    }
    protected function _isAllowed()
    {
        return true;
    }
}

Also I made a plain Page where later I will add a form that creates a new BlogPost:

namespace MageGuide\FirstModule\Controller\Adminhtml\BlogpostForm;

use Magento\Backend\App\Action;

class Index extends Action
{
    /** @var \Magento\Framework\View\Result\PageFactory  */
    protected $resultPageFactory;
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }
    /**
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }

    protected function _isAllowed()
    {
        return true;
    }
}

That using the following template /app/code/MageGuide/FirstModule/view/adminhtml/layout/adminblogposts_blogpostform_index.xml:

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left"  xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>
            Add BLogpost
        </title>
    </head>
    <body>
        <referenceContainer name="content">
            Hello
        </referenceContainer>
    </body>
</page>

What I want is upon showing the Grid to add a button-hyperlink Create New where a new page will open for showing the form page. Do you know how I can do that?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.