Blog

How to create Admin Module in Magento2

In this article, we are going to learn how to create admin module in Magento2.

There are some actions which we are going to implement like:-

  • create menu
  • create sub-menu
  • display grid on the sub-menu
  • display search box
  • display pagination
  • display column controls
  • display filters
  • display bookmarks
  • display mass-actions

Let’s start with creating menu.

Create Admin Module

Create The Module Configuration File Named Module.Xml

Create app/code/Thecoachsmb/Grid/etc/module.xml file

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Thecoachsmb_Grid">
    </module>
</config>

Create Registration.php file for registering module

Create app/code/Thecoachsmb/Grid/registration.php file

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Thecoachsmb_Grid',
    __DIR__
);

Create Route file routes.xml

Now, the next step is to add an admin router of the module.Β For that, we need to create aΒ routes.xmlΒ fileΒ In app/code/Thecoachsmb/Grid/etc/adminhtml/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="admin">
        <route id="smbgrid" frontName="smbgrid">
            <module name="Thecoachsmb_Grid" before="Magento_Adminhtml"/> 
        </route> 
    </router> 
</config>

Create Menu

Next, we will have to add link in admin menu for which we need to create menu.xml

Create app/code/Thecoachsmb/Grid/etc/adminhtml/menu.xml file

Menu Name – Thecoachsmb

Sub-Menu Name – Articles

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="Thecoachsmb::top_level" title="Thecoachsmb" module="Thecoachsmb_Grid" sortOrder="9999" resource="Magento_Backend::content"/>
        <add id="Thecoachsmb_Grid::manage_mlcustomform" title="Articles" module="Thecoachsmb_Grid" sortOrder="9999" resource="Thecoachsmb_Grid::manage_mlcustomform" parent="Thecoachsmb::top_level" action="smbgrid/customform"/>
    </menu>
</config>

Create Table

To create the table, create app/code/Thecoachsmb/Grid/etc/db_schema.xml file

If you want to learn multiple operations on the table, follow this article.

Here Table Name – thecoachsmb_article_grid

Columns – article_id, first_name, last_name, email, phone, message, image, created_at

<?xml version="1.0" ?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="thecoachsmb_article_grid" resource="default" engine="innodb" comment="Article Grid Table">
        <column xsi:type="smallint" name="id" padding="6" unsigned="true" nullable="false" identity="true" comment="ID"/>
        <column xsi:type="text" name="first_name" nullable="false" comment="First Name"/>
        <column xsi:type="text" name="last_name" nullable="false" comment="Last Name"/>
        <column xsi:type="text" name="email" nullable="false" comment="Email"/>
        <column xsi:type="int" name="phone" nullable="false" padding="10" default="0" identity="false" comment="Phone"/>
        <column xsi:type="text" name="message" nullable="true" comment="Message"/>
        <column xsi:type="int" name="status" padding="11" unsigned="false" nullable="false" default="1" identity="false" comment="Status"/>
        <column xsi:type="text" name="image" nullable="true" comment="Image"/>
        <column xsi:type="datetime" name="created_at" nullable="false" comment="Created Date" default="CURRENT_TIMESTAMP"/>
        <constraint xsi:type="primary" referenceId="PRIMARY"> 
            <column name="id"/>
        </constraint>        
        <index referenceId="THECOACHSMB_CUSTOMFORM_FIRST_NAME" indexType="fulltext">
            <column name="first_name"/>
        </index>
        <index referenceId="THECOACHSMB_CUSTOMFORM_LAST_NAME" indexType="fulltext">
            <column name="last_name"/>
        </index>
        <index referenceId="THECOACHSMB_CUSTOMFORM_EMAIL" indexType="fulltext">
            <column name="email"/>
        </index>
        <index referenceId="THECOACHSMB_CUSTOMFORM_MESSAGE" indexType="fulltext">
            <column name="message"/>
        </index>
        <index referenceId="THECOACHSMB_CUSTOMFORM_IMAGE" indexType="fulltext">
            <column name="image"/>
        </index>
    </table>
</schema>

Now, run the below command to update this table in the databse:

php bin/magento setup:db-declaration:generate-whitelist --module-name=Thecoachsmb_Grid

and

php bin/magento setup:upgrade && php bin/magento se:s:d -f && php bin/magento c:f
This will create table in the database. To check the table, go to the database, search for this table.

Create the Model file

After that, we will need to take care of the business logic and a single instance of object. So, let’s create Grid.php inΒ  app/code/Thecoachsmb/Grid/Model/Customform.php file

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Model;

use Thecoachsmb\Grid\Api\Data\CustomformInterface;
use Thecoachsmb\Grid\Api\Data\CustomformInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;

class Customform extends \Magento\Framework\Model\AbstractModel
{

     protected $_eventPrefix = 'thecoachsmb_customform';
     protected $dataObjectHelper;
     protected $customformDataFactory;


     /**
      * @param \Magento\Framework\Model\Context $context
      * @param \Magento\Framework\Registry $registry
      * @param CustomformInterfaceFactory $customformDataFactory
      * @param DataObjectHelper $dataObjectHelper
      * @param \Thecoachsmb\Grid\Model\ResourceModel\Customform $resource
      * @param \Thecoachsmb\Grid\Model\ResourceModel\Customform\Collection $resourceCollection
      * @param array $data
      */
      public function __construct(
          \Magento\Framework\Model\Context $context,
          \Magento\Framework\Registry $registry,
          CustomformInterfaceFactory $customformDataFactory,
          DataObjectHelper $dataObjectHelper,
          \Thecoachsmb\Grid\Model\ResourceModel\Customform $resource,
          \Thecoachsmb\Grid\Model\ResourceModel\Customform\Collection $resourceCollection,
          array $data = []
    ) {
         $this->customformDataFactory = $customformDataFactory;
         $this->dataObjectHelper = $dataObjectHelper;
         parent::__construct($context, $registry, $resource, $resourceCollection, $data);
     }

      /**
       * Retrieve customform model with customform data
       * @return CustomformInterface
       */
       public function getDataModel()
       {
             $customformData = $this->getData();

             $customformDataObject = $this->customformDataFactory->create();
             $this->dataObjectHelper->populateWithArray(
             $customformDataObject,
             $customformData,
             CustomformInterface::class
      );

      return $customformDataObject;
      }
}

Now, Create the InterfaceΒ  file

Create CustomformInterface.phpΒ inΒ app\code\Thecoachsmb\Grid\Api\DataΒ folder.

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Api\Data;

interface CustomformInterface extends \Magento\Framework\Api\ExtensibleDataInterface
{
     const ID = 'id';
     const MESSAGE = 'message';
     const FIRST_NAME = 'first_name';
     const LAST_NAME = 'last_name';
     const CREATED_AT = 'created_at';
     const EMAIL = 'email';
     const PHONE = 'phone';
     const IMAGE = 'image';
     const STATUS = 'status';

     /**
      * Get id
      * @return string|null
      */
      public function getId();

    /**
      * Set id
      * @param string $id
      * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
      */
      public function setId($id);

     /**
      * Retrieve existing extension attributes object or create a new one.
      * @return \Thecoachsmb\Grid\Api\Data\CustomformExtensionInterface|null
      */
      public function getExtensionAttributes();

      /**
       * Set an extension attributes object.
       * @param \Thecoachsmb\Grid\Api\Data\CustomformExtensionInterface $extensionAttributes
       * @return $this
       */
       public function setExtensionAttributes(
           \Thecoachsmb\Grid\Api\Data\CustomformExtensionInterface $extensionAttributes
       );

      /**
       * Get first_name
       * @return string|null
       */
       public function getFirstName();

       /**
        * Set first_name
        * @param string $firstName
        * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
        */
        public function setFirstName($firstName);

       /**
        * Get last_name
        * @return string|null
        */
       public function getLastName();

       /**
        * Set last_name
        * @param string $lastName
        * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
        */
       public function setLastName($lastName);

      /**
       * Get email
       * @return string|null
       */
       public function getEmail();

       /**
        * Set email
        * @param string $email
        * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
        */
        public function setEmail($email);

        /**
         * Get phone
         * @return string|null
         */
        public function getPhone();

       /**
        * Set phone
        * @param string $phone
        * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
        */
       public function setPhone($phone);

     /**
     * Get message
     * @return string|null
     */
     public function getMessage();

    /**
    * Set message
    * @param string $message
    * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
    */
    public function setMessage($message);

    /**
    * Get image
    * @return string|null
    */
    public function getImage();

    /**
    * Set image
    * @param string $image
    * @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
    */
    public function setImage($image);

/**
* Get status
* @return string|null
*/
public function getStatus();

/**
* Set status
* @param string $status
* @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
*/
public function setStatus($status);

/**
* Get created_at
* @return string|null
*/
public function getCreatedAt();

/**
* Set created_at
* @param string $createdAt
* @return \Thecoachsmb\Grid\Api\Data\CustomformInterface
*/
public function setCreatedAt($createdAt);
}

Create the Resource Model file

Now, for table β€˜thecoachsmb_article_grid’, create the ResourceModel File Customform.php in Thecoachsmb\Grid\Model\ResourceModel location.

<?php

namespace Thecoachsmb\Grid\Model\ResourceModel;

class Customform extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{

/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('thecoachsmb_article_grid', 'id'); //here "thecoachsmb_article_grid" is table name and "id" is the primary key of custom table
}
}

Create the Collection file Collection.php

In app/code/Thecoachsmb/Grid/Model/ResourceModel/Customform/Collection.php

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Model\ResourceModel\Customform;

class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{

/**
* @var string
*/
protected $_idFieldName = 'id';
protected $_previewFlag;
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(
\Thecoachsmb\Grid\Model\Customform::class,
\Thecoachsmb\Grid\Model\ResourceModel\Customform::class
);
$this->_map['fields']['id'] = 'main_table.id';
}
}

Create Controller to display grid

Now, we will create controller file Customform.phpΒ inΒ app/code/Thecoachsmb/Grid/Controller/Adminhtml

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Controller\Adminhtml;

abstract class Customform extends \Magento\Backend\App\Action
{

const ADMIN_RESOURCE = 'Thecoachsmb_Grid::top_level';
protected $_coreRegistry;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry
) {
$this->_coreRegistry = $coreRegistry;
parent::__construct($context);
}

/**
* Init page
*
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @return \Magento\Backend\Model\View\Result\Page
*/
public function initPage($resultPage)
{
$resultPage->setActiveMenu(self::ADMIN_RESOURCE)
->addBreadcrumb(__('Thecoachsmb'), __('Thecoachsmb'))
->addBreadcrumb(__('Customform'), __('Customform'));
return $resultPage;
}
}

Create Index.php file app/code/Thecoachsmb/Grid/Controller/Adminhtml/CustomformΒ 

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Controller\Adminhtml\Customform;

class Index extends \Magento\Backend\App\Action
{

protected $resultPageFactory;

/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}

/**
* Index action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Thecoachsmb::top_level');
$resultPage->addBreadcrumb(__('Customform'), __('Customform'));
$resultPage->addBreadcrumb(__('Manage Customform'), __('Manage Customform'));
$resultPage->getConfig()->getTitle()->prepend(__("Manage Customform"));
return $resultPage;
}
}

Display Grid

Ceate layout file smbgrid_grid_index.xmlΒ inΒ app/code/Thecoachsmb/Grid/view/adminhtml/layout

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="styles"/>
<body>
<referenceContainer name="content">
<uiComponent name="smbgrid_customform_listing"/>
</referenceContainer>
</body>
</page>


Create UI gridΒ 

We’llΒ  create smbgrid_customform_listing.xml file in app/code/Thecoachsmb/Grid/view/adminhtml/ui_component folder as following.

<?xml version="1.0" ?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="provider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing_data_source</item>
</item>
</argument>
<settings>
<spinner>smbgrid_customform_columns</spinner>
<deps>
<dep>smbgrid_customform_listing.smbgrid_customform_listing_data_source</dep>
</deps>
<buttons>
<button name="add">
<url path="*/*/new"/>
<class>primary</class>
<label translate="true">Add new Customform</label>
</button>
</buttons>
</settings>
<dataSource name="smbgrid_customform_listing_data_source" component="Magento_Ui/js/grid/provider">
<settings>
<updateUrl path="mui/index/render"/>
</settings>
<aclResource>Thecoachsmb_Customform::Customform</aclResource>
<dataProvider name="smbgrid_customform_listing_data_source" class="Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider">
<settings>
<requestFieldName>id</requestFieldName>
<primaryFieldName>id</primaryFieldName>
</settings>
</dataProvider>
</dataSource>
<listingToolbar name="listing_top">
<settings>
<sticky>true</sticky>
</settings>
<bookmark name="bookmarks"/>
<columnsControls name="columns_controls"/>
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing.smbgrid_customform_columns.ids</item>
<item name="indexField" xsi:type="string">id</item>
</item>
</argument>
<action name="delete">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">delete</item>
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="url" path="smbgrid/Customform/massDelete"/>
<item name="confirm" xsi:type="array">
<item name="name" xsi:type="string" translate="true">Delete items</item>
<item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
</item>
</item>
</argument>
</action> 
<action name="edit">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="type" xsi:type="string">edit</item>
<item name="label" xsi:type="string" translate="true">Edit</item>
<item name="callback" xsi:type="array">
<item name="provider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing.smbgrid_customform_columns_editor</item>
<item name="target" xsi:type="string">editSelected</item>
</item>
</item>
</argument>
</action>
<action name="disable">
<settings>
<url path="smbgrid/Customform/massDisable"/>
<type>disable</type>
<label translate="true">Disable</label>
</settings>
</action>
<action name="enable">
<settings>
<url path="smbgrid/Customform/massEnable"/>
<type>enable</type>
<label translate="true">Enable</label>
</settings>
</action> 
</massaction>
<filters name="listing_filters"/>
<paging name="listing_paging"/>
<filterSearch name="fulltext">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="provider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing_data_source</item>
<item name="chipsProvider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing.listing_top.listing_filters_chips</item>
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing.listing_top.bookmarks</item>
<item name="namespace" xsi:type="string">current.search</item>
</item>
</item>
</argument>
</filterSearch>
</listingToolbar>
<columns name="smbgrid_customform_columns">
<settings>
<editorConfig>
<param name="selectProvider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing.smbgrid_customform_columns.ids</param>
<param name="enabled" xsi:type="boolean">true</param>
<param name="indexField" xsi:type="string">id</param>
<param name="clientConfig" xsi:type="array">
<item name="saveUrl" xsi:type="url" path="smbgrid/Customform/inlineEdit"/>
<item name="validateBeforeSave" xsi:type="boolean">false</item>
</param>
</editorConfig>
<childDefaults>
<param name="fieldAction" xsi:type="array">
<item name="provider" xsi:type="string">smbgrid_customform_listing.smbgrid_customform_listing.smbgrid_customform_columns_editor</item>
<item name="target" xsi:type="string">startEdit</item>
<item name="params" xsi:type="array">
<item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
<item name="1" xsi:type="boolean">true</item>
</item>
</param>
</childDefaults>
</settings>
<selectionsColumn name="ids">
<settings>
<indexField>id</indexField>
</settings>
</selectionsColumn>
<column name="id">
<settings>
<filter>text</filter>
<sorting>asc</sorting>
<label translate="true">ID</label>
</settings>
</column>
<column name="first_name">
<settings>
<filter>text</filter>
<label translate="true">first_name</label>
<editor>
<editorType>text</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</editor>
</settings>
</column>
<column name="last_name">
<settings>
<filter>text</filter>
<label translate="true">last_name</label>
<editor>
<editorType>text</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</editor>
</settings>
</column>
<column name="email">
<settings>
<filter>text</filter>
<label translate="true">email</label>
<editor>
<editorType>text</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</editor>
</settings>
</column>
<column name="message">
<settings>
<filter>text</filter>
<label translate="true">message</label>
<editor>
<editorType>text</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</editor>
</settings>
</column>
<column name="image" class="Thecoachsmb\Grid\Ui\Component\Listing\Column\Thumbnail">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/thumbnail</item>
<item name="sortable" xsi:type="boolean">false</item>
<item name="has_preview" xsi:type="string">1</item> 
<item name="label" xsi:type="string" translate="true">Image</item>
</item>
</argument>
</column>
<column name="status">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Thecoachsmb\Grid\Model\Source\Status</item>
<item name="config" xsi:type="array">
<item name="editor" xsi:type="array">
<item name="editorType" xsi:type="string">select</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
<item name="filter" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="label" xsi:type="string" translate="true">Status</item>
<item name="dataType" xsi:type="string">select</item>
<item name="sortOrder" xsi:type="number">30</item>
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
</item>
</argument>
</column>
<column name="created_at" class="Magento\Ui\Component\Listing\Columns\Date" component="Magento_Ui/js/grid/columns/date">
<settings>
<filter>dateRange</filter>
<dataType>date</dataType>
<label translate="true">created_at</label>
<editor>
<editorType>date</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</editor>
</settings>
</column>
<column name="phone">
<settings>
<filter>text</filter>
<label translate="true">phone</label>
<editor>
<editorType>text</editorType>
<validation>
<rule name="required-entry" xsi:type="boolean">false</rule>
</validation>
</editor>
</settings>
</column>
<actionsColumn name="actions" class="Thecoachsmb\Grid\Ui\Component\Listing\Column\CustomformActions">
<settings>
<indexField>id</indexField>
<resizeEnabled>false</resizeEnabled>
<resizeDefaultWidth>107</resizeDefaultWidth>
</settings>
</actionsColumn>
</columns>
</listing>

 

Create di.xml

Create di.xml file in app/code/Thecoachsmb/Grid/etc

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Thecoachsmb\Grid\Api\CustomformRepositoryInterface" type="Thecoachsmb\Grid\Model\CustomformRepository"/>
<preference for="Thecoachsmb\Grid\Api\Data\CustomformInterface" type="Thecoachsmb\Grid\Model\Data\Customform"/>
<preference for="Thecoachsmb\Grid\Api\Data\CustomformSearchResultsInterface" type="Magento\Framework\Api\SearchResults"/>
<virtualType name="Thecoachsmb\Grid\Model\ResourceModel\Customform\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">smbgrid_customform</argument>
<argument name="resourceModel" xsi:type="string">Thecoachsmb\Grid\Model\ResourceModel\Customform\Collection</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="smbgrid_customform_listing_data_source" xsi:type="string">Thecoachsmb\Grid\Model\ResourceModel\Customform\Grid\Collection</item>
</argument>
</arguments>
</type>
<virtualType name="Thecoachsmb\Grid\CustomformImageUpload" type="Thecoachsmb\Grid\Model\ImageUploader">
<arguments>
<argument name="baseTmpPath" xsi:type="string">smbgrid/customform/tmp</argument>
<argument name="basePath" xsi:type="string">smbgrid/customform</argument>
<argument name="allowedExtensions" xsi:type="array">
<item name="jpg" xsi:type="string">jpg</item>
<item name="jpeg" xsi:type="string">jpeg</item>
<item name="gif" xsi:type="string">gif</item>
<item name="png" xsi:type="string">png</item>
</argument>
</arguments>
</virtualType>
<type name="Thecoachsmb\Grid\Controller\Adminhtml\Customform\FileUploader\Save">
<arguments>
<argument name="imageUploader" xsi:type="object">Thecoachsmb\Grid\CustomformImageUpload</argument>
</arguments>
</type>
</config>

 

Create CustomformSearchResultsInterface.php in Thecoachsmb\Customform\Api\Data folder

<?php
declare(strict_types=1);

namespace Thecoachsmb\Customform\Api\Data;

interface CustomformSearchResultsInterface extends \Magento\Framework\Api\SearchResultsInterface
{

/**
* Get Customform list.
* @return \Thecoachsmb\Customform\Api\Data\CustomformInterface[]
*/
public function getItems();

/**
* Set id list.
* @param \Thecoachsmb\Customform\Api\Data\CustomformInterface[] $items
* @return $this
*/
public function setItems(array $items);
}

 

For Adding Data

Create NewAction.php in app/code/Thecoachsmb/Grid/Adminhtml/Customform folder

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Controller\Adminhtml\Customform;

class NewAction extends \Thecoachsmb\Grid\Controller\Adminhtml\Customform
{

protected $resultForwardFactory;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
) {
$this->resultForwardFactory = $resultForwardFactory;
parent::__construct($context, $coreRegistry);
}

/**
* New action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Forward $resultForward */
$resultForward = $this->resultForwardFactory->create();
return $resultForward->forward('edit');
}
}

Call the Edit Action

Here we forward the action to edit, create smbgrid_customform_new.xml file in app/code/Thecoachsmb/Grid/view/adminhtml/layout folder

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="smbgrid_customform_edit"/>
</page>

Edit Action

Create Edit.php in app/code/Thecoachsmb/Grid/Adminhtml/Customform folder

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Controller\Adminhtml\Customform;

class Edit extends \Thecoachsmb\Grid\Controller\Adminhtml\Customform
{

protected $resultPageFactory;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context, $coreRegistry);
}

/**
* Edit action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
// 1. Get ID and create model
$id = $this->getRequest()->getParam('id');
$model = $this->_objectManager->create(\Thecoachsmb\Grid\Model\Customform::class);

// 2. Initial checking
if ($id) {
$model->load($id);
if (!$model->getId()) {
$this->messageManager->addErrorMessage(__('This Customform no longer exists.'));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
$this->_coreRegistry->register('smbgrid_customform', $model);

// 3. Build edit form
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$this->initPage($resultPage)->addBreadcrumb(
$id ? __('Edit Customform') : __('New Customform'),
$id ? __('Edit Customform') : __('New Customform')
);
$resultPage->getConfig()->getTitle()->prepend(__('Customforms'));
$resultPage->getConfig()->getTitle()->prepend($model->getId() ? __('Edit Customform %1', $model->getId()) : __('New Customform'));
return $resultPage;
}
}

Call the Edit Action

Here we forward the action to edit, create smbgrid_customform_edit.xml file in app/code/Thecoachsmb/Grid/view/adminhtml/layout folder

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="styles"/>
<body>
<referenceContainer name="content">
<uiComponent name="smbgrid_customform_form"/>
</referenceContainer>
</body>
</page>

Create Ui Grid for Form

Now lets create theΒ  ui_component smbgrid_customform_form.xml file in app/code/Thecoachsmb/Grid/view/adminhtml/ui_component folder

<?xml version="1.0" ?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
       <item name="js_config" xsi:type="array">
           <item name="provider" xsi:type="string">smbgrid_customform_form.customform_form_data_source</item></item>
           <item name="label" xsi:type="string" translate="true">General Information</item>
           <item name="template" xsi:type="string">templates/form/collapsible</item>
           </argument>
              <settings>
                 <buttons>
                    <button name="back" class="Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit\BackButton"/>
                    <button name="delete" class="Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit\DeleteButton"/>
                    <button name="reset" class="Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit\ResetButton"/>
                    <button name="save" class="Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit\SaveButton"/>
                    <button name="save_and_continue" class="Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit\SaveAndContinueButton"/>
                 </buttons>
                 <namespace>smbgrid_customform_form</namespace>
                 <dataScope>data</dataScope>
                 <deps>
                    <dep>smbgrid_customform_form.customform_form_data_source</dep>
                 </deps>
             </settings>
             <dataSource name="customform_form_data_source">
               <argument name="data" xsi:type="array">
                  <item name="js_config" xsi:type="array">
                  <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
                  </item>
              </argument>
             <settings>
                <submitUrl path="*/*/save"/>
             </settings>
             <dataProvider name="customform_form_data_source" class="Thecoachsmb\Grid\Model\Customform\DataProvider">
             <settings>
             <requestFieldName>id</requestFieldName>
             <primaryFieldName>id</primaryFieldName>
             </settings>
       </dataProvider>
       </dataSource>
       <fieldset name="general">
           <settings>
              <label>General</label>
           </settings>
           <field name="first_name" formElement="textarea" sortOrder="10">
             <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                  <item name="source" xsi:type="string">Customform</item>
               </item>
             </argument>
             <settings>
               <dataType>text</dataType>
               <label translate="true">First Name</label>
              <dataScope>first_name</dataScope>
              <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
              </validation>
            </settings>
          </field>
          <field name="last_name" formElement="textarea" sortOrder="20">
              <argument name="data" xsi:type="array">
                 <item name="config" xsi:type="array">
                   <item name="source" xsi:type="string">Customform</item>
                 </item>
              </argument>
             <settings>
             <dataType>text</dataType>
             <label translate="true">Last Name</label>
             <dataScope>last_name</dataScope>
             <validation>
               <rule name="required-entry" xsi:type="boolean">true</rule>
             </validation>
            </settings>
         </field>
         <field name="email" formElement="textarea" sortOrder="30">
            <argument name="data" xsi:type="array">
                 <item name="config" xsi:type="array">
                    <item name="source" xsi:type="string">Customform</item>
                </item>
           </argument>
           <settings>
              <dataType>text</dataType>
              <label translate="true">Email</label>
              <dataScope>email</dataScope>
               <validation>
                <rule name="required-entry" xsi:type="boolean">true</rule>
              </validation>
          </settings>
         </field>
<field name="phone" formElement="input" sortOrder="40">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">Customform</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">Phone</label>
<dataScope>phone</dataScope>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
</settings>
</field>
<field name="message" formElement="textarea" sortOrder="50">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">Customform</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">Message</label>
<dataScope>message</dataScope>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
</settings>
</field>
<field name="image">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="source" xsi:type="string">image</item>
<item name="label" xsi:type="string" translate="true">Image</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="previewTmpl" xsi:type="string">Thecoachsmb_Grid/image-preview</item>
<item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
<item name="required" xsi:type="boolean">false</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="smbgrid/customform_fileUploader/save"/>
</item>
</item>
</argument>
</field>
<field name="status" formElement="checkbox">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">Customform</item>
<item name="default" xsi:type="number">1</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
<dataType>boolean</dataType>
<label translate="true">Enable</label>
</settings>
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="number">0</map>
<map name="true" xsi:type="number">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
</field>
</fieldset>
</form>

Create Column Action File

Create CustomformActions.php file in app/code/Thecoachsmb/Grid/Ui/Component/Listing/Column

content is:-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Ui\Component\Listing\Column;

class CustomformActions extends \Magento\Ui\Component\Listing\Columns\Column
{

const URL_PATH_EDIT = 'smbgrid/customform/edit';
const URL_PATH_DELETE = 'smbgrid/customform/delete';
protected $urlBuilder;
const URL_PATH_DETAILS = 'smbgrid/customform/details';

/**
* @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
* @param \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param array $components
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\UiComponent\ContextInterface $context,
\Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
\Magento\Framework\UrlInterface $urlBuilder,
array $components = [],
array $data = []
) {
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}

/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
if (isset($item['id'])) {
$item[$this->getData('name')] = [
'edit' => [
'href' => $this->urlBuilder->getUrl(
static::URL_PATH_EDIT,
[
'id' => $item['id']
]
),
'label' => __('Edit')
],
'delete' => [
'href' => $this->urlBuilder->getUrl(
static::URL_PATH_DELETE,
[
'id' => $item['id']
]
),
'label' => __('Delete'),
'confirm' => [
'title' => __('Delete %1',$item['id']),
'message' => __('Are you sure you wan\'t to delete a %1 record ?',$item['id'])
]
]
];
}
}
}

return $dataSource;
}
}

Create Thumbnail UI Component

Create Thumbnail.php in app/code/Thecoachsmb/Grid/Ui/Component/Listing/Column folder

and the content is :-

<?php

namespace Thecoachsmb\Grid\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\StoreManagerInterface;

class Thumbnail extends \Magento\Ui\Component\Listing\Columns\Column
{
const NAME = 'image';
const ALT_FIELD = 'name';
protected $storeManager;

/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param \Magento\Catalog\Helper\Image $imageHelper
* @param \Magento\Framework\UrlInterface $urlBuilder
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
StoreManagerInterface $storeManager,
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->storeManager = $storeManager;
}

/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
$path = $this->storeManager->getStore()->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
);
foreach ($dataSource['data']['items'] as & $item) {
if ($item['image']) {
$item[$fieldName . '_src'] = $path.'thecoachsmb/customform'.$item['image'];
$item[$fieldName . '_alt'] = $item['first_name'].' '.$item['last_name'];
$item[$fieldName . '_orig_src'] = $path.'thecoachsmb/customform'.$item['image'];
}else{
// please place your placeholder image at pub/media/thecoachsmb/customform/placeholder/placeholder.jpg
$item[$fieldName . '_src'] = $path.'thecoachsmb/customform/placeholder/placeholder.jpg';
$item[$fieldName . '_alt'] = 'Place Holder';
$item[$fieldName . '_orig_src'] = $path.'thecoachsmb/customform/placeholder/placeholder.jpg';
}
}
}

return $dataSource;
}
}

 

Create DataProviderΒ 

Create DataProvider.php file in app/code/Thecoachsmb/Grid/Model/Customform

and the content is:

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Model\Customform;

use Thecoachsmb\Grid\Model\ResourceModel\Customform\CollectionFactory;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Filesystem;
use Magento\Framework\App\Filesystem\DirectoryList;

class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

protected $loadedData;
protected $collection;
protected $filesystem;
protected $dataPersistor;
/**
* Store manager
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;

/**
* Constructor
*
* @param string $name
* @param string $primaryFieldName
* @param string $requestFieldName
* @param CollectionFactory $collectionFactory
* @param DataPersistorInterface $dataPersistor
* @param array $meta
* @param array $data
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
CollectionFactory $collectionFactory,
DataPersistorInterface $dataPersistor,
\Magento\Store\Model\StoreManagerInterface $storeManager,
Filesystem $filesystem,
array $meta = [],
array $data = []
) {
$this->collection = $collectionFactory->create();
$this->dataPersistor = $dataPersistor;
$this->filesystem = $filesystem;
$this->storeManager = $storeManager;
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
}

/**
* Get data
*
* @return array
*/
public function getData()
{
if (isset($this->loadedData)) {
return $this->loadedData;
}
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$destinationPath = $mediaDirectory->getAbsolutePath('thecoachsmb/customform');
$items = $this->collection->getItems();
foreach ($items as $model) {
$itemData = $model->getData();
if ($model->getImage()) {
$imageName = $itemData['image']; // Your database field 
//unset($itemData['image']);
$itemData['image'] = array(
array(
'name' => $imageName,
'url' => $this->storeManager
->getStore()
->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
).'thecoachsmb/customform'.$itemData['image'] // Should return a URL to view the image. For example, http://domain.com/pub/media/../../imagename.jpeg
)
);
}
$this->loadedData[$model->getId()] = $itemData;
}
$data = $this->dataPersistor->get('smbgrid');

if (!empty($data)) {
$model = $this->collection->getNewEmptyItem();
$model->setData($data);
$this->loadedData[$model->getId()] = $model->getData();
$this->dataPersistor->clear('smbgrid');
}

return $this->loadedData;
}

}

Add Buttons on the Form:

On the admin form page, we are going to provide below buttons:-

  • Back
  • Reset
  • Delete
  • Save and Continue Edit
  • Save

Let us start with adding “Back” button-

Add Back Button on the Form Page

To provide the Back Button, create BackButton.php in Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit folder

and content will be-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class BackButton extends GenericButton implements ButtonProviderInterface
{

/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Back'),
'on_click' => sprintf("location.href = '%s';", $this->getBackUrl()),
'class' => 'back',
'sort_order' => 10
];
}

/**
* Get URL for back (reset) button
*
* @return string
*/
public function getBackUrl()
{
return $this->getUrl('*/*/');
}
}

Add Reset Button on the Form Page

To provide the reset Button, create ResetButton.php in Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit folder

and content will be-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class ResetButton extends GenericButton implements ButtonProviderInterface
{

/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Reset'),
'class' => 'reset',
'on_click' => 'location.reload();',
'sort_order' => 30
];
}
}

Add Delete Button on the Form Page

To provide the delete Button, create DeleteButton.php in Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit folder

and content will be-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class DeleteButton extends GenericButton implements ButtonProviderInterface
{

/**
* @return array
*/
public function getButtonData()
{
$data = [];
if ($this->getModelId()) {
$data = [
'label' => __('Delete Customform'),
'class' => 'delete',
'on_click' => 'deleteConfirm(\'' . __(
'Are you sure you want to do this?'
) . '\', \'' . $this->getDeleteUrl() . '\')',
'sort_order' => 20,
];
}
return $data;
}

/**
* Get URL for delete button
*
* @return string
*/
public function getDeleteUrl()
{
return $this->getUrl('*/*/delete', ['id' => $this->getModelId()]);
}
}

In this file, we extended from GenericButton. so let’s create this file GenericButton.php.

Create GenericButton.php in Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit folder

and content will be-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit;

use Magento\Backend\Block\Widget\Context;

abstract class GenericButton
{

protected $context;

/**
* @param \Magento\Backend\Block\Widget\Context $context
*/
public function __construct(Context $context)
{
$this->context = $context;
}

/**
* Return model ID
*
* @return int|null
*/
public function getModelId()
{
return $this->context->getRequest()->getParam('id');
}

/**
* Generate url by route and parameters
*
* @param string $route
* @param array $params
* @return string
*/
public function getUrl($route = '', $params = [])
{
return $this->context->getUrlBuilder()->getUrl($route, $params);
}
}

Add Save Button on the Form Page

To provide the delete Button, create SaveButton.php in Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit folder

and content will be-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class SaveButton extends GenericButton implements ButtonProviderInterface
{

/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save Customform'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 90,
];
}
}

Add “Save and Continue Edit” Button on the Form Page

To provide the delete Button, create SaveAndContinueButton.php in Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit folder

and content will be-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Block\Adminhtml\Customform\Edit;

use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class SaveAndContinueButton extends GenericButton implements ButtonProviderInterface
{

/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Save and Continue Edit'),
'class' => 'save',
'data_attribute' => [
'mage-init' => [
'button' => ['event' => 'saveAndContinueEdit'],
],
],
'sort_order' => 80,
];
}
}

Save the Form Data

To save the Form Data, create Save.php file in Thecoachsmb\Grid\Controller\Adminhtml\Customform folder

and the content is-

<?php
declare(strict_types=1);

namespace Thecoachsmb\Grid\Controller\Adminhtml\Customform;

use Magento\Framework\Exception\LocalizedException;

class Save extends \Magento\Backend\App\Action
{

protected $dataPersistor;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
) {
$this->dataPersistor = $dataPersistor;
parent::__construct($context);
}

/**
* Save action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$data = $this->getRequest()->getPostValue();
if ($data) {
$id = $this->getRequest()->getParam('id');

$model = $this->_objectManager->create(\Thecoachsmb\Customform\Model\Customform::class)->load($id);
if (!$model->getId() && $id) {
$this->messageManager->addErrorMessage(__('This Customform no longer exists.'));
return $resultRedirect->setPath('*/*/');
}

if (isset($data['image'][0]['name']) && isset($data['image'][0]['tmp_name'])) {
$data['image'] =$data['image'][0]['name'];
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()->get(
'Thecoachsmb\Customform\CustomformImageUpload'
);
$this->imageUploader->moveFileFromTmp($data['image']);
} elseif (isset($data['image'][0]['name']) && !isset($data['image'][0]['tmp_name'])) {
$data['image'] = $data['image'][0]['name'];
} else {
$data['image'] = null;
}
$model->setData($data);

try {
$model->save();
$this->messageManager->addSuccessMessage(__('You saved the Customform.'));
$this->dataPersistor->clear('smbcustomform');

if ($this->getRequest()->getParam('back')) {
return $resultRedirect->setPath('*/*/edit', ['id' => $model->getId()]);
}
return $resultRedirect->setPath('*/*/');
} catch (LocalizedException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the Customform.'));
}

$this->dataPersistor->set('smbcustomform', $data);
return $resultRedirect->setPath('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
}
return $resultRedirect->setPath('*/*/');
}
}

MassDelete Action:

Create MassDelete.php in Thecoachsmb\Customform\Controller\Adminhtml\Customform folder

Content is here:

<?php
declare(strict_types=1);

namespace Thecoachsmb\Customform\Controller\Adminhtml\Customform;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Thecoachsmb\Customform\Model\ResourceModel\Customform\CollectionFactory;

/**
* Class MassDelete
* @package Thecoachsmb\Customform\Controller\Adminhtml\Customform
*/

class MassDelete extends \Magento\Backend\App\Action
{
/**
* @var Filter
*/
protected $filter;
/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
{
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context);
}
/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$collectionSize = $collection->getSize();

foreach ($collection as $page) {
$page->delete();
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}

Show Options For Status:

Create Status.php in Thecoachsmb\Customform\Model\Source folder

<?php

namespace Thecoachsmb\Customform\Model\Source;

use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;
use Magento\Eav\Model\Entity\Attribute\Source\SourceInterface;
use Magento\Framework\Data\OptionSourceInterface;

/**
* Item status functionality model
*/
class Status extends AbstractSource implements SourceInterface, OptionSourceInterface
{
/**#@+
* Item Status values
*/
const STATUS_ENABLED = 1;

const STATUS_DISABLED = 0;

/**#@-*/

/**
* Retrieve option array
*
* @return string[]
*/
public static function getOptionArray()
{
return [self::STATUS_ENABLED => __('Enabled'), self::STATUS_DISABLED => __('Disabled')];
}

/**
* Retrieve option array with empty value
*
* @return string[]
*/
public function getAllOptions()
{
$result = [];

foreach (self::getOptionArray() as $index => $value) {
$result[] = ['value' => $index, 'label' => $value];
}

return $result;
}
}



Leave a Reply

Your email address will not be published. Required fields are marked *

πŸš€ Let’s Connect on LinkedIn! πŸš€

Want to level up your skills and knowledge? πŸš€
Follow us on LinkedIn to get:
βœ… Expert insights on business growth
βœ… Daily tips to sharpen your skills
βœ… Exclusive updates from The Coach SMB

Let's grow together!