In this technical blog, we will learn about how to create a custom EAV model extension using Magento 2. Especially, Magento 2 uses EAV structure in many core modules. However, it’s a more complex structure than a simple relation table model.
Before start creates the module, Let’s understand what is eav model and why it’s used by Magento 2.
What is the EAV model in Magento 2 ?
Magento 2 EAV (Entity Attribute Value) data module which is used for more flexibility and scalable for your data. EAV structure is best to make an abundance of sense for a generic eCommerce solution.
Entity: It’s store data. In the case of Magento, it’s such as like customer product, category, and etc.
Attribute: Separate property of each entity such as like name, weight, email address.
Value: The value of a given entity and attribute.
Why use the EAV model structure ?
- It will provide more flexibility to access, retrieve, store the data and attribute.
- Quick to implement.
- Developers can add attributes to any entity such as product, category, customer, order etc without modifying the core database structure
There are some disadvantages of EAV model structure :
- It’s a more complex structure than flat table structure.
- SQL query execution is more slower than flat table strucuture.
Example :
- Product name store in catalog_product_entity_varchar table by Magento 2.
- Id of the product store in catalog_product_entity_int table by Magento 2.
- Description about product store in catalog_product_entity_text table by Magento 2.
- Price of the product store in catalog_product_entity_decimal table by Magento 2.
EAV entities in Magento 2 :
As a result, you can see on the eav_entity_type table :
- customer
- customer_address
- catalog_category
- catalog_product
So, let’s now create module with EAV model structure for Magento 2. First, If you want to create simple module you can create from here.
You may also like this :
- Magento 2 : Create admin grid and form without ui component
- Magento 2 : Create UI Component Grid and Form
Follow this below steps to create custom EAV module in Magento 2 :
1) First of all, create app/code/RH/HelloWorld/etc/module.xml for the declaration module:
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="RH_HelloWorld" setup_version="1.0.0" >
</module>
</config>
2) After that, create app/code/RH/HelloWorld/registration.php for registration of your module :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'RH_HelloWorld',
__DIR__
);
3) After that, create app/code/RH/HelloWorld/etc/acl.xml for admin acl file :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="RH_HelloWorld::helloworld" title="Hello World" sortOrder="10" />
</resource>
</resources>
</acl>
</config>
4) Next to, create app/code/RH/HelloWorld/etc/di.xml file for setup collection files for UI component grid and config of collection :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Eav\Model\Entity\AttributeCache">
<arguments>
<argument name="unsupportedTypes" xsi:type="array">
<item name="rh_helloworld" xsi:type="string">rh_helloworld</item>
</argument>
</arguments>
</type>
<type name="RH\HelloWorld\UI\Component\Listing\DataProvider">
<arguments>
<argument name="collection" xsi:type="object" shared="false">RH\HelloWorld\Model\ResourceModel\HelloWorld\Collection</argument>
<argument name="filterPool" xsi:type="object" shared="false">RefGridFilterPool</argument>
</arguments>
</type>
<virtualType name="RefGridFilterPool" type="Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool">
<arguments>
<argument name="appliers" xsi:type="array">
<item name="regular" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\RegularFilter</item>
<item name="fulltext" xsi:type="object">Magento\Framework\View\Element\UiComponent\DataProvider\FulltextFilter</item>
</argument>
</arguments>
</virtualType>
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
<arguments>
<argument name="collections" xsi:type="array">
<item name="rh_helloworld_helloworld_listing_data_source" xsi:type="string">RH\HelloWorld\Model\ResourceModel\HelloWorld\Grid\Collection</item>
</argument>
</arguments>
</type>
<type name="RH\HelloWorld\Model\ResourceModel\HelloWorld\Grid\Collection">
<arguments>
<argument name="eventPrefix" xsi:type="string">rh_helloworld_helloworld_grid_collection</argument>
<argument name="eventObject" xsi:type="string">rh_helloworld_helloworld_grid_collection</argument>
<argument name="resourceModel" xsi:type="string">RH\HelloWorld\Model\ResourceModel\HelloWorld</argument>
</arguments>
</type>
</config>
5) Next , create app/code/RH/HelloWorld/etc/adminhtml/routes.xml file for admin panel router :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="rh_helloworld" frontName="rh_helloworld">
<module name="RH_HelloWorld" />
</route>
</router>
</config>
6) Next, Create app/code/RH/HelloWorld/etc/adminhtml/menu.xml for set admin menu :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="RH_HelloWorld::helloworld" title="Hello World" module="RH_HelloWorld" sortOrder="50" parent="Magento_Backend::content" action="rh_helloworld/helloworld" resource="RH_HelloWorld::helloworld"/>
</menu>
</config>
7) After that, create app/code/RH/HelloWorld/Block/Adminhtml/HelloWorld/Edit/BackButton.php file for back button in UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
class BackButton implements ButtonProviderInterface
{
/**
* Url Builder
*
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;
/**
* @param Context $context
*/
public function __construct(Context $context)
{
$this->urlBuilder = $context->getUrlBuilder();
}
/**
* @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 button
*
* @return string
*/
public function getBackUrl()
{
return $this->urlBuilder->getUrl('*/*/');
}
}
8) After that, Create app/code/RH/HelloWorld/Block/Adminhtml/HelloWorld/Edit/DeleteButton.php file for delete records button in UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
/**
* Class DeleteButton
*/
class DeleteButton implements ButtonProviderInterface
{
/**
* Url Builder
*
* @var \Magento\Framework\UrlInterface
*/
protected $urlBuilder;
/**
* Registry
*
* @var Registry
*/
protected $registry;
/**
* @param Context $context
* @param Registry $registry
*/
public function __construct(
Context $context,
Registry $registry
) {
$this->urlBuilder = $context->getUrlBuilder();
$this->registry = $registry;
}
/**
* Delete records confirm popup
*
* @return array
*/
public function getButtonData()
{
if ($this->registry->registry('entity_id') > 0) {
$data = [
'label' => __('Delete'),
'class' => 'delete',
'id' => 'helloworld-edit-delete-button',
'data_attribute' => [
'url' => $this->getDeleteUrl(),
],
'on_click' =>
'deleteConfirm(\'' . __("Are you sure you want to do this?") . '\', \'' . $this->getDeleteUrl() . '\')',
'sort_order' => 20,
];
return $data;
}
}
/**
* Delete records controller URL
*
* @return string
*/
public function getDeleteUrl()
{
return $this->urlBuilder->getUrl('*/*/delete', ['entity_id' => $this->registry->registry('entity_id')]);
}
}
9) After that, create app/code/RH/HelloWorld/Block/Adminhtml/HelloWorld/Edit/ResetButton.php file for reset records button in UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
/**
* Class ResetButton
*/
class ResetButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
return [
'label' => __('Reset'),
'class' => 'reset',
'on_click' => 'location.reload();',
'sort_order' => 20,
];
}
}
10) After that, create app/code/RH/HelloWorld/Block/Adminhtml/HelloWorld/Edit/SaveAndContinueButton.php file for save and continue button in UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
/**
* Class SaveAndContinueButton
*/
class SaveAndContinueButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
$data = [
'label' => __('Save and Continue Edit'),
'class' => 'save',
'data_attribute' => [
'mage-init' => [
'button' => ['event' => 'saveAndContinueEdit'],
],
],
'sort_order' => 50,
];
return $data;
}
}
11) After that, create app/code/RH/HelloWorld/Block/Adminhtml/HelloWorld/Edit/SaveButton.php file for save and continue button in UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
/**
* Class SaveButton
*/
class SaveButton implements ButtonProviderInterface
{
/**
* @return array
*/
public function getButtonData()
{
$data = [
'label' => __('Save'),
'class' => 'save primary',
'data_attribute' => [
'mage-init' => ['button' => ['event' => 'save']],
'form-role' => 'save',
],
'sort_order' => 60,
];
return $data;
}
}
12) After that, create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/Add.php file for new record UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Framework\Controller\ResultFactory;
class Add extends \Magento\Backend\App\Action
{
/**
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
$resultPage->getConfig()->getTitle()->prepend(__('Add New Record'));
return $resultPage;
}
}
13) After that, create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/Delete.php file for delete record UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use RH\HelloWorld\Model\HelloWorldFactory;
class Delete extends Action
{
/**
* @var HelloWorldFactory
*/
protected $helloworldFactory;
/**
* @param Context $context
* @param HelloWorldFactory $helloworldFactory
*/
public function __construct(
Context $context,
HelloWorldFactory $helloworldFactory
) {
$this->helloworldFactory = $helloworldFactory;
parent::__construct($context);
}
/**
* For allow to access or not
*
* {@inheritdoc}
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('RH_HelloWorld::helloworld');
}
/**
* Delete action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$resultRedirect = $this->resultRedirectFactory->create();
$id = $this->getRequest()->getParam('entity_id', null);
try {
$helloWorldData = $this->helloworldFactory->create()->load($id);
if ($helloworldData->getId()) {
$helloworldData->delete();
$this->messageManager->addSuccessMessage(__('You deleted the record.'));
} else {
$this->messageManager->addErrorMessage(__('Record does not exist.'));
}
} catch (\Exception $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}
return $resultRedirect->setPath('*/*');
}
}
14) After that, create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/Edit.php file for existing record edit UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Registry;
use Magento\Framework\View\Result\PageFactory;
use RH\HelloWorld\Model\HelloWorldFactory;
class Edit extends Action
{
/**
* Core registry
*
* @var Registry
*/
protected $_coreRegistry = null;
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var HelloWorldFactory
*/
protected $helloworldFactory;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
* @param Registry $registry
* @param HelloWorldFactory $helloworldFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory,
Registry $registry,
HelloWorldFactory $helloworldFactory
) {
$this->resultPageFactory = $resultPageFactory;
$this->_coreRegistry = $registry;
$this->helloworldFactory = $helloworldFactory;
parent::__construct($context);
}
/**
* For allow to access or not
*
* {@inheritdoc}
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('RH_HelloWorld::helloworld');
}
/**
* Edit
*
* @return \Magento\Backend\Model\View\Result\Page | \Magento\Backend\Model\View\Result\Redirect
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$id = $this->getRequest()->getParam('entity_id');
$helloworldData = $this->helloworldFactory->create();
if ($id) {
$helloworldData->load($id);
if (!$helloworldData->getId()) {
$this->messageManager->addErrorMessage(__('This record no longer exists.'));
/** \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
return $resultRedirect->setPath('*/*/');
}
}
$data = $this->_session->getFormData(true);
if (!empty($data)) {
$helloworldData->addData($data);
}
$this->_coreRegistry->register('entity_id', $id);
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('RH_HelloWorld::helloworld');
$resultPage->getConfig()->getTitle()->prepend(__('Edit Record'));
return $resultPage;
}
}
15) After that, create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/Index.php file for admin UI grid page :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* For allow to access or not
*
* @return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('RH_HelloWorld::helloworld');
}
/**
* Index action
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('RH_HelloWorld::helloworld');
$resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
return $resultPage;
}
}
16) After that, create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/InlineEdit.php file for inline edit record UI grid :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
use RH\HelloWorld\Model\ResourceModel\HelloWorld\Collection;
class InlineEdit extends Action
{
/**
* @var JsonFactory
*/
protected $jsonFactory;
/**
* @var Collection
*/
protected $helloworldCollection;
/**
* @param Context $context
* @param Collection $helloworldCollection
* @param JsonFactory $jsonFactory
*/
public function __construct(
Context $context,
Collection $helloworldCollection,
JsonFactory $jsonFactory
) {
parent::__construct($context);
$this->jsonFactory = $jsonFactory;
$this->helloworldCollection = $helloworldCollection;
}
/**
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->jsonFactory->create();
$error = false;
$messages = [];
$post_items = $this->getRequest()->getParam('items', []);
if (!($this->getRequest()->getParam('isAjax') && count($post_items))) {
return $resultJson->setData([
'messages' => [__('Please correct the data sent.')],
'error' => true,
]);
}
try {
$this->helloworldCollection
->setStoreId($this->getRequest()->getParam('store', 0))
->addFieldToFilter('entity_id', ['in' => array_keys($post_items)])
->walk('saveCollection', [$post_items]);
} catch (\Exception $e) {
$messages[] = __('There was an error saving the data: ') . $e->getMessage();
$error = true;
}
return $resultJson->setData([
'messages' => $messages,
'error' => $error,
]);
}
}
17) Create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/MassDelete.php file for mass delete records :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Ui\Component\MassAction\Filter;
use RH\HelloWorld\Model\ResourceModel\HelloWorld\Collection;
/**
* Class MassDelete
*/
class MassDelete extends Action
{
/**
* @var Filter
*/
protected $filter;
/**
* @var Collection
*/
protected $helloworldCollection;
/**
* @param Context $context
* @param Filter $filter
* @param Collection $helloworldCollection
*/
public function __construct(
Context $context,
Filter $filter,
Collection $helloworldCollection
) {
$this->filter = $filter;
$this->helloworldCollection = $helloworldCollection;
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->helloworldCollection);
$collectionSize = $collection->getSize();
$collection->walk('delete');
$this->messageManager->addSuccessMessage(__('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('*/*/');
}
}
18) Create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/Save.php file for save records :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use RH\HelloWorld\Model\HelloWorldFactory;
class Save extends Action
{
/**
* @var HelloWorldFactory
*/
protected $helloworldFactory;
/**
* @param Context $context
* @param HelloWorldFactory $helloworldFactory
*/
public function __construct(
Context $context,
HelloWorldFactory $helloworldFactory
) {
$this->helloworldFactory = $helloworldFactory;
parent::__construct($context);
}
/**
* For allow to access or not
*
* return boolean
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('RH_HelloWorld::helloworld');
}
/**
* Save action
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
$storeId = (int) $this->getRequest()->getParam('store_id');
$data = $this->getRequest()->getParams();
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($data) {
$params = [];
$helloworldData = $this->helloworldFactory->create();
$helloworldData->setStoreId($storeId);
$params['store'] = $storeId;
if (empty($data['entity_id'])) {
$data['entity_id'] = null;
} else {
$helloworldData->load($data['entity_id']);
$params['entity_id'] = $data['entity_id'];
}
$helloworldData->addData($data);
$this->_eventManager->dispatch(
'rh_helloworld_helloworld_prepare_save',
['object' => $this->helloworldFactory, 'request' => $this->getRequest()]
);
try {
$helloworldData->save();
$this->messageManager->addSuccessMessage(__('You saved this record.'));
$this->_getSession()->setFormData(false);
if ($this->getRequest()->getParam('back')) {
$params['entity_id'] = $helloworldData->getId();
$params['_current'] = true;
return $resultRedirect->setPath('*/*/edit', $params);
}
return $resultRedirect->setPath('*/*/');
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the record.'));
}
$this->_getSession()->setFormData($this->getRequest()->getPostValue());
return $resultRedirect->setPath('*/*/edit', $params);
}
return $resultRedirect->setPath('*/*/');
}
}
19) Create app/code/RH/HelloWorld/Controller/Adminhtml/HelloWorld/Validate.php file for check data validation on form submit :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Controller\Adminhtml\HelloWorld;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Controller\Result\JsonFactory;
class Validate extends Action
{
/**
* @var JsonFactory
*/
protected $jsonFactory;
/**
* @var \Magento\Framework\DataObject
*/
protected $response;
/**
* @param Context $context
* @param JsonFactory $jsonFactory
*/
public function __construct(
Context $context,
JsonFactory $jsonFactory
) {
parent::__construct($context);
$this->jsonFactory = $jsonFactory;
$this->response = new \Magento\Framework\DataObject();
}
/**
* Check if required fields is not empty
*
* @param array $data
*/
public function validateRequireEntries(array $data)
{
$requiredFields = [
'identifier' => __('Hello World Identifier'),
];
foreach ($data as $field => $value) {
if (in_array($field, array_keys($requiredFields)) && $value == '') {
$this->_addErrorMessage(
__('To apply changes you should fill in required "%1" field', $requiredFields[$field])
);
}
}
}
/**
* Add error message validation
*
* @param $message
*/
protected function _addErrorMessage($message)
{
$this->response->setError(true);
if (!is_array($this->response->getMessages())) {
$this->response->setMessages([]);
}
$messages = $this->response->getMessages();
$messages[] = $message;
$this->response->setMessages($messages);
}
/**
* AJAX customer validation action
*
* @return \Magento\Framework\Controller\Result\Json
*/
public function execute()
{
$this->response->setError(0);
$this->validateRequireEntries($this->getRequest()->getParams());
$resultJson = $this->jsonFactory->create()->setData($this->response);
return $resultJson;
}
}
20) Main model file : app/code/RH/HelloWorld/Model/HelloWorld.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Model\AbstractModel;
class HelloWorld extends AbstractModel implements IdentityInterface
{
/**
* cache tag
*/
const CACHE_TAG = 'rh_helloworld_helloworld';
/**
* entity_type_id for save Entity Type ID value
*/
const KEY_ENTITY_TYPE_ID = 'entity_type_id';
/**
* attribute_set_id for save Attribute Set ID value
*/
const KEY_ATTR_TYPE_ID = 'attribute_set_id';
/**
* @var string
*/
protected $_cacheTag = 'rh_helloworld_helloworld';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'rh_helloworld_helloworld';
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
parent::_construct();
$this->_init(\RH\HelloWorld\Model\ResourceModel\HelloWorld::class);
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
}
/**
* Save from collection data
*
* @param array $data
* @return $this|bool
*/
public function saveCollection(array $data)
{
if (isset($data[$this->getId()])) {
$this->addData($data[$this->getId()]);
$this->getResource()->save($this);
}
return $this;
}
/**
* Set attribute set entity type id
*
* @param int $entityTypeId
* @return $this
*/
public function setEntityTypeId($entityTypeId)
{
return $this->setData(self::KEY_ENTITY_TYPE_ID, $entityTypeId);
}
/**
* {@inheritdoc}
*/
public function getEntityTypeId()
{
return $this->getData(self::KEY_ENTITY_TYPE_ID);
}
/**
* Set attribute set id
*
* @param int $attrSetId
* @return $this
*/
public function setAttributeSetId($attrSetId)
{
return $this->setData(self::KEY_ATTR_TYPE_ID, $attrSetId);
}
/**
* {@inheritdoc}
*/
public function getAttributeSetId()
{
return $this->getData(self::KEY_ATTR_TYPE_ID);
}
/**
* @throws \Magento\Framework\Exception\LocalizedException
* @return \Magento\Catalog\Model\ResourceModel\Category
* @deprecated because resource models should be used directly
*/
protected function _getResource()
{
return parent::_getResource();
}
/**
* Retrieve default attribute set id
*
* @return int
*/
public function getDefaultAttributeSetId()
{
return $this->getResource()->getEntityType()->getDefaultAttributeSetId();
}
}
21) Resource model file : app/code/RH/HelloWorld/Model/ResourceModel/HelloWorld.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model\ResourceModel;
use Magento\Eav\Model\Entity\AbstractEntity;
use Magento\Eav\Model\Entity\Context;
use Magento\Framework\DataObject;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use RH\HelloWorld\Setup\HelloWorldSetup;
class HelloWorld extends AbstractEntity
{
/**
* Store id
*
* @var int
*/
protected $_storeId = null;
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* @var EntityManager
*/
protected $entityManager;
/**
* @param Context $context
* @param StoreManagerInterface $storeManager
* @param array $data
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
array $data = []
) {
parent::__construct($context, $data);
$this->setType(HelloWorldSetup::ENTITY_TYPE_CODE);
$this->setConnection(HelloWorldSetup::ENTITY_TYPE_CODE . '_read', HelloWorldSetup::ENTITY_TYPE_CODE . '_write');
$this->_storeManager = $storeManager;
}
/**
* Set attribute set id and entity type id value
*
* @param \Magento\Framework\DataObject $customer
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _beforeSave(\Magento\Framework\DataObject $object)
{
$object->setAttributeSetId($object->getAttributeSetId() ?: $this->getEntityType()->getDefaultAttributeSetId());
$object->setEntityTypeId($object->getEntityTypeId() ?: $this->getEntityType()->getEntityTypeId());
return parent::_beforeSave($object);
}
/**
* Return Entity Type instance
*
* @return \Magento\Eav\Model\Entity\Type
*/
public function getEntityType()
{
if (empty($this->_type)) {
$this->setType(HelloWorldSetup::ENTITY_TYPE_CODE);
}
return parent::getEntityType();
}
/**
* Retrieve HelloWorld entity default attributes
*
* @return string[]
*/
protected function _getDefaultAttributes()
{
return [
'attribute_set_id',
'entity_type_id',
'created_at',
'updated_at',
];
}
/**
* Set store Id
*
* @param integer $storeId
* @return $this
*/
public function setStoreId($storeId)
{
$this->_storeId = $storeId;
return $this;
}
/**
* Return store id
*
* @return integer
*/
public function getStoreId()
{
if ($this->_storeId === null) {
return $this->_storeManager->getStore()->getId();
}
return $this->_storeId;
}
/**
* Set Attribute values to be saved
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
* @param mixed $value
* @return $this
*/
protected function _saveAttribute($object, $attribute, $value)
{
$table = $attribute->getBackend()->getTable();
if (!isset($this->_attributeValuesToSave[$table])) {
$this->_attributeValuesToSave[$table] = [];
}
$entityIdField = $attribute->getBackend()->getEntityIdField();
$storeId = $object->getStoreId() ?: Store::DEFAULT_STORE_ID;
$data = [
$entityIdField => $object->getId(),
'entity_type_id' => $object->getEntityTypeId(),
'attribute_id' => $attribute->getId(),
'value' => $this->_prepareValueForSave($value, $attribute),
'store_id' => $storeId,
];
if (!$this->getEntityTable() || $this->getEntityTable() == \Magento\Eav\Model\Entity::DEFAULT_ENTITY_TABLE) {
$data['entity_type_id'] = $object->getEntityTypeId();
}
if ($attribute->isScopeStore()) {
/**
* Update attribute value for store
*/
$this->_attributeValuesToSave[$table][] = $data;
} elseif ($attribute->isScopeWebsite() && $storeId != Store::DEFAULT_STORE_ID) {
/**
* Update attribute value for website
*/
$storeIds = $this->_storeManager->getStore($storeId)->getWebsite()->getStoreIds(true);
foreach ($storeIds as $storeId) {
$data['store_id'] = (int) $storeId;
$this->_attributeValuesToSave[$table][] = $data;
}
} else {
/**
* Update global attribute value
*/
$data['store_id'] = Store::DEFAULT_STORE_ID;
$this->_attributeValuesToSave[$table][] = $data;
}
return $this;
}
}
22) Hello world attributes file : app/code/RH/HelloWorld/Model/ResourceModel/Attribute.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model\ResourceModel;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute as EavAttribute;
use Magento\Eav\Model\ResourceModel\Entity\Type;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Store\Model\StoreManagerInterface;
use RH\HelloWorld\Setup\HelloWorldSetup;
class Attribute extends EavAttribute
{
/**
* Eav config
*
* @var Config
*/
protected $_eavConfig;
/**
* @param Context $context [description]
* @param StoreManagerInterface $storeManager [description]
* @param Type $eavEntityType [description]
* @param Config $eavConfig [description]
* @param string $connectionName [description]
*/
public function __construct(
Context $context,
StoreManagerInterface $storeManager,
Type $eavEntityType,
Config $eavConfig,
$connectionName = null
) {
$this->_eavConfig = $eavConfig;
parent::__construct($context, $storeManager, $eavEntityType, $connectionName);
}
/**
* Trigger actions before object save
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
*/
protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
{
$applyTo = $object->getApplyTo();
if (is_array($applyTo)) {
$object->setApplyTo(implode(',', $applyTo));
}
return parent::_beforeSave($object);
}
/**
* Trigger actions after object save
*
* @param AbstractModel $object
* @return $this
*/
protected function _afterSave(AbstractModel $object)
{
$this->_clearUselessAttributeValues($object);
return parent::_afterSave($object);
}
/**
* Clear useless attribute values
*
* @param AbstractModel $object
* @return $this
*/
protected function _clearUselessAttributeValues(AbstractModel $object)
{
$origData = $object->getOrigData();
if ($object->isScopeGlobal() && isset(
$origData['is_global']
) && ScopedAttributeInterface::SCOPE_GLOBAL != $origData['is_global']
) {
$attributeStoreIds = array_keys($this->_storeManager->getStores());
if (!empty($attributeStoreIds)) {
$delCondition = [
'attribute_id = ?' => $object->getId(),
'store_id IN(?)' => $attributeStoreIds,
];
$this->getConnection()->delete($object->getBackendTable(), $delCondition);
}
}
return $this;
}
/**
* Delete entity
*
* @param AbstractModel $object
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function deleteEntity(AbstractModel $object)
{
if (!$object->getEntityAttributeId()) {
return $this;
}
$select = $this->getConnection()->select()->from(
$this->getTable('eav_entity_attribute')
)->where(
'entity_attribute_id = ?',
(int) $object->getEntityAttributeId()
);
$result = $this->getConnection()->fetchRow($select);
if ($result) {
$attribute = $this->_eavConfig->getAttribute(
HelloWorldSetup::ENTITY_TYPE_CODE,
$result['attribute_id']
);
$backendTable = $attribute->getBackend()->getTable();
if ($backendTable) {
$select = $this->getConnection()->select()->from(
$attribute->getEntity()->getEntityTable(),
'entity_id'
)->where(
'attribute_set_id = ?',
$result['attribute_set_id']
);
$clearCondition = [
'attribute_id =?' => $attribute->getId(),
'entity_id IN (?)' => $select,
];
$this->getConnection()->delete($backendTable, $clearCondition);
}
}
$condition = ['entity_attribute_id = ?' => $object->getEntityAttributeId()];
$this->getConnection()->delete($this->getTable('eav_entity_attribute'), $condition);
return $this;
}
}
23) Collection file : app/code/RH/HelloWorld/Model/ResourceModel/HelloWorld/Collection.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model\ResourceModel\HelloWorld;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\EntityFactory as EavEntityFactory;
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
use Magento\Eav\Model\ResourceModel\Helper;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Validator\UniversalFactory;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
/**
* Class Collection
* @package RH\HelloWorld\Model\ResourceModel\HelloWorld
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Collection extends AbstractCollection
{
/**
* @var string
*/
protected $_idFieldName = 'entity_id';
/**
* @var $_storeId
*/
protected $_storeId;
/**
* @var StoreManagerInterface
*/
protected $_storeManager;
/**
* @param EntityFactory $entityFactory
* @param LoggerInterface $logger
* @param FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param Config $eavConfig
* @param ResourceConnection $resource
* @param EavEntityFactory $eavEntityFactory
* @param Helper $resourceHelper
* @param UniversalFactory $universalFactory
* @param StoreManagerInterface $storeManager
* @param AdapterInterface|null $connection
*/
public function __construct(
EntityFactory $entityFactory,
LoggerInterface $logger,
FetchStrategyInterface $fetchStrategy,
ManagerInterface $eventManager,
Config $eavConfig,
ResourceConnection $resource,
EavEntityFactory $eavEntityFactory,
Helper $resourceHelper,
UniversalFactory $universalFactory,
StoreManagerInterface $storeManager,
AdapterInterface $connection = null
) {
$this->_storeManager = $storeManager;
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$eavConfig,
$resource,
$eavEntityFactory,
$resourceHelper,
$universalFactory,
$connection
);
}
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init(\RH\HelloWorld\Model\HelloWorld::class, \RH\HelloWorld\Model\ResourceModel\HelloWorld::class);
}
/**
* Set store scope
*
* @param int|string|\Magento\Store\Model\Store $store
* @return $this
*/
public function setStore($store)
{
$this->setStoreId($this->_storeManager->getStore($store)->getId());
return $this;
}
/**
* Set store scope
*
* @param int|string|\Magento\Store\Api\Data\StoreInterface $storeId
* @return $this
*/
public function setStoreId($storeId)
{
if ($storeId instanceof \Magento\Store\Api\Data\StoreInterface) {
$storeId = $storeId->getId();
}
$this->_storeId = (int) $storeId;
return $this;
}
/**
* Return current store id
*
* @return int
*/
public function getStoreId()
{
if ($this->_storeId === null) {
$this->setStoreId($this->_storeManager->getStore()->getId());
}
return $this->_storeId;
}
/**
* Retrieve default store id
*
* @return int
*/
public function getDefaultStoreId()
{
return \Magento\Store\Model\Store::DEFAULT_STORE_ID;
}
/**
* Retrieve attributes load select
*
* @param string $table
* @param array|int $attributeIds
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
*/
protected function _getLoadAttributesSelect($table, $attributeIds = [])
{
if (empty($attributeIds)) {
$attributeIds = $this->_selectAttributes;
}
$storeId = $this->getStoreId();
$connection = $this->getConnection();
$entityTable = $this->getEntity()->getEntityTable();
$indexList = $connection->getIndexList($entityTable);
$entityIdField = $indexList[$connection->getPrimaryKeyName($entityTable)]['COLUMNS_LIST'][0];
if ($storeId) {
$joinCondition = [
't_s.attribute_id = t_d.attribute_id',
"t_s.{$entityIdField} = t_d.{$entityIdField}",
$connection->quoteInto('t_s.store_id = ?', $storeId),
];
$select = $connection->select()->from(
['t_d' => $table],
['attribute_id']
)->join(
['e' => $entityTable],
"e.{$entityIdField} = t_d.{$entityIdField}",
['e.entity_id']
)->where(
"e.entity_id IN (?)",
array_keys($this->_itemsById)
)->where(
't_d.attribute_id IN (?)',
$attributeIds
)->joinLeft(
['t_s' => $table],
implode(' AND ', $joinCondition),
[]
)->where(
't_d.store_id = ?',
$connection->getIfNullSql('t_s.store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID)
);
} else {
$select = $connection->select()->from(
['t_d' => $table],
['attribute_id']
)->join(
['e' => $entityTable],
"e.{$entityIdField} = t_d.{$entityIdField}",
['e.entity_id']
)->where(
"e.entity_id IN (?)",
array_keys($this->_itemsById)
)->where(
'attribute_id IN (?)',
$attributeIds
)->where(
'store_id = ?',
$this->getDefaultStoreId()
);
}
return $select;
}
/**
* @param \Magento\Framework\DB\Select $select
* @param string $table
* @param string $type
* @return \Magento\Framework\DB\Select
*/
protected function _addLoadAttributesSelectValues($select, $table, $type)
{
$storeId = $this->getStoreId();
if ($storeId) {
$connection = $this->getConnection();
$valueExpr = $connection->getCheckSql('t_s.value_id IS NULL', 't_d.value', 't_s.value');
$select->columns(
['default_value' => 't_d.value', 'store_value' => 't_s.value', 'value' => $valueExpr]
);
} else {
$select = parent::_addLoadAttributesSelectValues($select, $table, $type);
}
return $select;
}
/**
* Adding join statement to collection select instance
*
* @param string $method
* @param object $attribute
* @param string $tableAlias
* @param array $condition
* @param string $fieldCode
* @param string $fieldAlias
* @return \Magento\Eav\Model\Entity\Collection\AbstractCollection
*/
protected function _joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias)
{
if (isset($this->_joinAttributes[$fieldCode]['store_id'])) {
$storeId = $this->_joinAttributes[$fieldCode]['store_id'];
} else {
$storeId = $this->getStoreId();
}
$connection = $this->getConnection();
if ($storeId != $this->getDefaultStoreId() && !$attribute->isScopeGlobal()) {
/**
* Add joining default value for not default store
* if value for store is null - we use default value
*/
$defCondition = '(' . implode(') AND (', $condition) . ')';
$defAlias = $tableAlias . '_default';
$defAlias = $this->getConnection()->getTableName($defAlias);
$defFieldAlias = str_replace($tableAlias, $defAlias, $fieldAlias);
$tableAlias = $this->getConnection()->getTableName($tableAlias);
$defCondition = str_replace($tableAlias, $defAlias, $defCondition);
$defCondition .= $connection->quoteInto(
" AND " . $connection->quoteColumnAs("{$defAlias}.store_id", null) . " = ?",
$this->getDefaultStoreId()
);
$this->getSelect()->{$method}(
[$defAlias => $attribute->getBackend()->getTable()],
$defCondition,
[]
);
$method = 'joinLeft';
$fieldAlias = $this->getConnection()->getCheckSql(
"{$tableAlias}.value_id > 0",
$fieldAlias,
$defFieldAlias
);
$this->_joinAttributes[$fieldCode]['condition_alias'] = $fieldAlias;
$this->_joinAttributes[$fieldCode]['attribute'] = $attribute;
} else {
$storeId = $this->getDefaultStoreId();
}
$condition[] = $connection->quoteInto(
$connection->quoteColumnAs("{$tableAlias}.store_id", null) . ' = ?',
$storeId
);
return parent::_joinAttributeToSelect($method, $attribute, $tableAlias, $condition, $fieldCode, $fieldAlias);
}
}
24) Grid collection file : app/code/RH/HelloWorld/Model/ResourceModel/HelloWorld/Grid/Collection.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model\ResourceModel\HelloWorld\Grid;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\EntityFactory as EavEntityFactory;
use Magento\Eav\Model\ResourceModel\Helper;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Validator\UniversalFactory;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use RH\HelloWorld\Model\ResourceModel\HelloWorld\Collection as HelloWorldCollection;
/**
* Class Collection
* @package RH\HelloWorld\Model\ResourceModel\HelloWorld\Grid
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Collection extends HelloWorldCollection implements SearchResultInterface
{
/**
* @var AggregationInterface
*/
protected $aggregations;
/**
* @param EntityFactory $entityFactory
* @param LoggerInterface $logger
* @param FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param Config $eavConfig
* @param ResourceConnection $resource
* @param EavEntityFactory $eavEntityFactory
* @param Helper $resourceHelper
* @param UniversalFactory $universalFactory
* @param StoreManagerInterface $storeManager
* @param [type] $eventPrefix
* @param [type] $eventObject
* @param [type] $resourceModel
* @param string $model
* @param AdapterInterface|null $connection
*/
public function __construct(
EntityFactory $entityFactory,
LoggerInterface $logger,
FetchStrategyInterface $fetchStrategy,
ManagerInterface $eventManager,
Config $eavConfig,
ResourceConnection $resource,
EavEntityFactory $eavEntityFactory,
Helper $resourceHelper,
UniversalFactory $universalFactory,
StoreManagerInterface $storeManager,
$eventPrefix,
$eventObject,
$resourceModel,
$model = 'RH\HelloWorld\Ui\Component\Listing\DataProvider\Document',
AdapterInterface $connection = null
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$eavConfig,
$resource,
$eavEntityFactory,
$resourceHelper,
$universalFactory,
$storeManager,
$connection
);
$this->_eventPrefix = $eventPrefix;
$this->_eventObject = $eventObject;
$this->_init($model, $resourceModel);
}
/**
* @return AggregationInterface
*/
public function getAggregations()
{
return $this->aggregations;
}
/**
* @param AggregationInterface $aggregations
* @return $this
*/
public function setAggregations($aggregations)
{
$this->aggregations = $aggregations;
}
/**
* Retrieve all ids for collection
* Backward compatibility with EAV collection
*
* @param int $limit
* @param int $offset
* @return array
*/
public function getAllIds($limit = null, $offset = null)
{
return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
}
/**
* Get search criteria.
*
* @return SearchCriteriaInterface|null
*/
public function getSearchCriteria()
{
return null;
}
/**
* Set search criteria.
*
* @param SearchCriteriaInterface $searchCriteria
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(SearchCriteriaInterface $searchCriteria = null)
{
return $this;
}
/**
* Get total count.
*
* @return int
*/
public function getTotalCount()
{
return $this->getSize();
}
/**
* Set total count.
*
* @param int $totalCount
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setTotalCount($totalCount)
{
return $this;
}
/**
* Set items list.
*
* @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setItems(array $items = null)
{
return $this;
}
}
25) Then, create app/code/RH/HelloWorld/Model/ResourceModel/Eav/Attribute.php file for attribute EAV resource model file :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model\ResourceModel\Eav;
use Magento\Eav\Model\Entity\Attribute as EavAttribute;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use RH\HelloWorld\Setup\HelloWorldSetup;
/**
* Class Attribute
* @package RH\HelloWorld\Model\ResourceModel\Eav
*/
class Attribute extends EavAttribute implements ScopedAttributeInterface
{
/**
* Constants
*/
const MODULE_NAME = 'RH_HelloWorld';
const KEY_IS_GLOBAL = 'is_global';
const KEY_IS_STATIC = 'static';
/**
* Event object name
*
* @var string
*/
protected $_eventObject = 'attribute';
/**
* Array with labels
*
* @var array
*/
protected static $_labels = null;
/**
* Event prefix
*
* @var string
*/
protected $_eventPrefix = HelloWorldSetup::ENTITY_TYPE_CODE . '_attribute';
/**
* @return void
*/
protected function _construct()
{
$this->_init(\RH\HelloWorld\Model\ResourceModel\Attribute::class);
}
/**
* Processing object before save data
*
* @return \Magento\Framework\Model\AbstractModel
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function beforeSave()
{
$this->setData('modulePrefix', self::MODULE_NAME);
if (isset($this->_origData[self::KEY_IS_GLOBAL])) {
if (!isset($this->_data[self::KEY_IS_GLOBAL])) {
$this->_data[self::KEY_IS_GLOBAL] = self::SCOPE_GLOBAL;
}
}
return parent::beforeSave();
}
/**
* Processing object after save data
*
* @return \Magento\Framework\Model\AbstractModel
*/
public function afterSave()
{
/**
* Fix saving attribute in admin
*/
$this->_eavConfig->clear();
return parent::afterSave();
}
/**
* Return is attribute global
*
* @return integer
*/
public function getIsGlobal()
{
if ($this->getBackendType() === self::KEY_IS_STATIC) {
return true;
}
return $this->_getData(self::KEY_IS_GLOBAL);
}
/**
* Retrieve attribute is global scope flag
*
* @return bool
*/
public function isScopeGlobal()
{
return $this->getIsGlobal() == self::SCOPE_GLOBAL;
}
/**
* Retrieve attribute is website scope website
*
* @return bool
*/
public function isScopeWebsite()
{
return $this->getIsGlobal() == self::SCOPE_WEBSITE;
}
/**
* Retrieve attribute is store scope flag
*
* @return bool
*/
public function isScopeStore()
{
return !$this->isScopeGlobal() && !$this->isScopeWebsite();
}
/**
* Retrieve store id
*
* @return int
*/
public function getStoreId()
{
$dataObject = $this->getDataObject();
if ($dataObject) {
return $dataObject->getStoreId();
}
return $this->getData('store_id');
}
/**
* Retrieve source model
*
* @return \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
*/
public function getSourceModel()
{
$model = $this->getData('source_model');
if (empty($model)) {
if ($this->getBackendType() == 'int' && $this->getFrontendInput() == 'select') {
return $this->_getDefaultSourceModel();
}
}
return $model;
}
/**
* Get default attribute source model
*
* @return string
*/
public function _getDefaultSourceModel()
{
return 'Magento\Eav\Model\Entity\Attribute\Source\Table';
}
/**
* {@inheritdoc}
*/
public function afterDelete()
{
$this->_eavConfig->clear();
return parent::afterDelete();
}
}
26) Then, create app/code/RH/HelloWorld/Model/ResourceModel/Attribute/Collection.php file for attribute collection file :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Model\ResourceModel\Attribute;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\EntityFactory as EavEntityFactory;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection as EavCollection;
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
use Magento\Framework\Data\Collection\EntityFactory;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
use Psr\Log\LoggerInterface;
use RH\HelloWorld\Setup\HelloWorldSetup;
class Collection extends EavCollection
{
/**
* Entity factory
*
* @var EavEntityFactory
*/
protected $_eavEntityFactory;
/**
* @param EntityFactory $entityFactory
* @param LoggerInterface $logger
* @param FetchStrategyInterface $fetchStrategy
* @param ManagerInterface $eventManager
* @param Config $eavConfig
* @param EavEntityFactory $eavEntityFactory
* @param AdapterInterface|null $connection
* @param AbstractDb|null $resource
*/
public function __construct(
EntityFactory $entityFactory,
LoggerInterface $logger,
FetchStrategyInterface $fetchStrategy,
ManagerInterface $eventManager,
Config $eavConfig,
EavEntityFactory $eavEntityFactory,
AdapterInterface $connection = null,
AbstractDb $resource = null
) {
$this->_eavEntityFactory = $eavEntityFactory;
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $eavConfig, $connection, $resource);
}
/**
* Main select object initialization.
*
* @return $this
*/
protected function _initSelect()
{
$this->getSelect()->from(
['main_table' => $this->getResource()->getMainTable()]
)->where(
'main_table.entity_type_id=?',
$this->_eavEntityFactory->create()->setType(HelloWorldSetup::ENTITY_TYPE_CODE)->getTypeId()
)->join(
['additional_table' => $this->getTable(HelloWorldSetup::EAV_ENTITY_TYPE_CODE . '_eav_attribute')],
'additional_table.attribute_id = main_table.attribute_id'
);
return $this;
}
/**
* @return $this
*/
public function getFilterAttributesOnly()
{
$this->getSelect()->where('additional_table.is_filterable', 1);
return $this;
}
/**
* @param int $status
* @return $this
*/
public function addVisibilityFilter($status = 1)
{
$this->getSelect()->where('additional_table.is_visible', $status);
return $this;
}
/**
* Specify attribute entity type filter
*
* @param int $typeId
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setEntityTypeFilter($typeId)
{
return $this;
}
}
27) Then, create app/code/RH/HelloWorld/Setup/EavTablesSetup.php file for install eav tables :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Setup;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\SchemaSetupInterface;
class EavTablesSetup
{
/**
* @var SchemaSetupInterface
*/
protected $setup;
/**
* @param SchemaSetupInterface $setup
*/
public function __construct(
SchemaSetupInterface $setup
) {
$this->setup = $setup;
}
/**
* create all eav tables
*/
public function createEavTables($entityCode)
{
$this->createEAVMainTable($entityCode);
$this->createEntityTable($entityCode, 'datetime', Table::TYPE_DATETIME);
$this->createEntityTable($entityCode, 'decimal', Table::TYPE_DECIMAL, '12,4');
$this->createEntityTable($entityCode, 'int', Table::TYPE_INTEGER);
$this->createEntityTable($entityCode, 'text', Table::TYPE_TEXT, '64k');
$this->createEntityTable($entityCode, 'varchar', Table::TYPE_TEXT, 255);
}
/**
* create eav attributes tables and add foreign keys
*/
protected function createEAVMainTable($entityCode)
{
$tableName = $entityCode . '_eav_attribute';
$tableName = 'rh_helloworld_eav_attribute';
$table = $this->setup->getConnection()->newTable(
$this->setup->getTable($tableName)
)->addColumn(
'attribute_id',
Table::TYPE_SMALLINT,
null,
['identity' => false, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Attribute Id'
)->addColumn(
'is_global',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '1'],
'Is Global'
)->addColumn(
'is_filterable',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Is Filterable'
)->addColumn(
'is_visible',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '1'],
'Is Visible'
)
->addColumn(
'is_wysiwyg_enabled',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Attribute uses WYSIWYG'
)->addColumn(
'validate_rules',
Table::TYPE_TEXT,
'64k',
[],
'Validate Rules'
)->addColumn(
'is_system',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Is System'
)->addColumn(
'sort_order',
Table::TYPE_INTEGER,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Sort Order'
)->addColumn(
'data_model',
Table::TYPE_TEXT,
255,
[],
'Data Model'
)->addForeignKey(
$this->setup->getFkName($tableName, 'attribute_id', 'eav_attribute', 'attribute_id'),
'attribute_id',
$this->setup->getTable('eav_attribute'),
'attribute_id',
Table::ACTION_CASCADE
)->setComment(
'RH HelloWorld Eav Attribute'
);
$this->setup->getConnection()->createTable($table);
}
/**
* create eav entities tables and add foreign keys
*/
protected function createEntityTable($entityCode, $type, $valueType, $valueLength = null)
{
$tableName = $entityCode . '_' . $type;
$table = $this->setup->getConnection()
->newTable($this->setup->getTable($tableName))
->addColumn(
'value_id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Value ID'
)
->addColumn(
'entity_type_id',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Entity Type ID'
)
->addColumn(
'attribute_id',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Attribute ID'
)
->addColumn(
'store_id',
Table::TYPE_SMALLINT,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Store ID'
)
->addColumn(
'entity_id',
Table::TYPE_INTEGER,
null,
['unsigned' => true, 'nullable' => false, 'default' => '0'],
'Entity ID'
)
->addColumn(
'value',
$valueType,
$valueLength,
[],
'Value'
)
->addIndex(
$this->setup->getIdxName(
$tableName,
['entity_id', 'attribute_id', 'store_id', 'entity_type_id'],
AdapterInterface::INDEX_TYPE_UNIQUE
),
['entity_id', 'attribute_id', 'store_id', 'entity_type_id'],
['type' => AdapterInterface::INDEX_TYPE_UNIQUE]
)
->addIndex(
$this->setup->getIdxName($tableName, ['entity_id']),
['entity_id']
)
->addIndex(
$this->setup->getIdxName($tableName, ['attribute_id']),
['attribute_id']
)
->addIndex(
$this->setup->getIdxName($tableName, ['store_id']),
['store_id']
)
->addIndex(
$this->setup->getIdxName($tableName, ['entity_type_id']),
['entity_type_id']
)
->addForeignKey(
$this->setup->getFkName(
$tableName,
'attribute_id',
'eav_attribute',
'attribute_id'
),
'attribute_id',
$this->setup->getTable('eav_attribute'),
'attribute_id',
Table::ACTION_CASCADE
)
->addForeignKey(
$this->setup->getFkName(
$tableName,
'entity_id',
$entityCode,
'entity_id'
),
'entity_id',
$this->setup->getTable($entityCode),
'entity_id',
Table::ACTION_CASCADE
)
->addForeignKey(
$this->setup->getFkName($tableName, 'store_id', 'store', 'store_id'),
'store_id',
$this->setup->getTable('store'),
'store_id',
Table::ACTION_CASCADE
)
->addForeignKey(
$this->setup->getFkName($tableName, 'entity_type_id', 'eav_entity_type', 'entity_type_id'),
'entity_type_id',
$this->setup->getTable('eav_entity_type'),
'entity_type_id',
Table::ACTION_CASCADE
)
->setComment($entityCode . ' ' . $type . 'Attribute Backend Table');
$this->setup->getConnection()->createTable($table);
}
}
28) Then, create app/code/RH/HelloWorld/Setup/InstallData.php file for install entities :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Setup;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use RH\HelloWorld\Setup\HelloWorldSetupFactory;
class InstallData implements InstallDataInterface
{
/**
* HelloWorld setup factory
*
* @var HelloWorldSetupFactory
*/
protected $helloworldSetupFactory;
/**
* Init
*
* @param HelloWorldSetupFactory $helloworldSetupFactory
*/
public function __construct(
HelloWorldSetupFactory $helloworldSetupFactory
) {
$this->helloworldSetupFactory = $helloworldSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var HelloWorldSetup $helloworldSetup */
$helloworldSetup = $this->helloworldSetupFactory->create(['setup' => $setup]);
$setup->startSetup();
$helloworldSetup->installEntities();
$entities = $helloworldSetup->getDefaultEntities();
foreach ($entities as $entityName => $entity) {
$helloworldSetup->addEntityType($entityName, $entity);
}
$setup->endSetup();
}
}
29) Then, Create app/code/RH/HelloWorld/Setup/InstallSchema.php file for install schema :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Setup;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use RH\HelloWorld\Setup\EavTablesSetupFactory;
use RH\HelloWorld\Setup\HelloWorldSetup;
class InstallSchema implements InstallSchemaInterface
{
/**
* @var EavTablesSetupFactory
*/
protected $eavTablesSetupFactory;
/**
* Init
*
* @internal param EavTablesSetupFactory $EavTablesSetupFactory
*/
public function __construct(
EavTablesSetupFactory $eavTablesSetupFactory
) {
$this->eavTablesSetupFactory = $eavTablesSetupFactory;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$tableName = HelloWorldSetup::ENTITY_TYPE_CODE;
/**
* Create entity Table
*/
$table = $setup->getConnection()
->newTable($setup->getTable($tableName))
->addColumn(
'entity_id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
'Entity ID'
)->setComment('Entity Table');
$table->addColumn(
'entity_type_id',
Table::TYPE_SMALLINT,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0',
],
'Entity Type ID'
)->addIndex(
$setup->getIdxName($tableName, ['entity_type_id']),
['entity_type_id']
)->addForeignKey(
$setup->getFkName(
'rh_helloworld_helloworld',
'entity_type_id',
'eav_entity_type',
'entity_type_id'
),
'entity_type_id',
$setup->getTable('eav_entity_type'),
'entity_type_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE,
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
);
$table->addColumn(
'attribute_set_id',
Table::TYPE_SMALLINT,
null,
[
'unsigned' => true,
'nullable' => false,
'default' => '0',
],
'Attribute Set ID'
)->addIndex(
$setup->getIdxName($tableName, ['attribute_set_id']),
['attribute_set_id']
)->addForeignKey(
$setup->getFkName(
'rh_helloworld_helloworld',
'attribute_set_id',
'eav_attribute_set',
'attribute_set_id'
),
'attribute_set_id',
$setup->getTable('eav_attribute_set'),
'attribute_set_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE,
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
);
// Add more static attributes here...
$table->addColumn(
'created_at',
Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
'Creation Time'
)->addColumn(
'updated_at',
Table::TYPE_TIMESTAMP,
null,
['nullable' => false, 'default' => Table::TIMESTAMP_INIT_UPDATE],
'Update Time'
);
$setup->getConnection()->createTable($table);
/** @var \RH\HelloWorld\Setup\EavTablesSetup $eavTablesSetup */
$eavTablesSetup = $this->eavTablesSetupFactory->create(['setup' => $setup]);
$eavTablesSetup->createEavTables(HelloWorldSetup::ENTITY_TYPE_CODE);
$setup->endSetup();
}
}
30) Then, create app/code/RH/HelloWorld/Setup/HelloWorldSetup.php file for eav attribute list file :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Setup;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Setup\EavSetup;
class HelloWorldSetup extends EavSetup
{
/**
* Entity type for Hello World EAV attributes
*/
const ENTITY_TYPE_CODE = 'rh_helloworld_helloworld';
/**
* EAV Entity type for Hello World EAV attributes
*/
const EAV_ENTITY_TYPE_CODE = 'rh_helloworld';
/**
* Retrieve Entity Attributes
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function getAttributes()
{
$attributes = [];
$attributes['main_title'] = [
'group' => 'General',
'type' => 'varchar',
'label' => 'Main Title',
'input' => 'text',
'global' => ScopedAttributeInterface::SCOPE_STORE,
'required' => '1',
'user_defined' => false,
'default' => '',
'unique' => false,
'position' => '10',
'note' => '',
'visible' => '1',
'wysiwyg_enabled' => '0',
];
// Add your more entity attributes here...
return $attributes;
}
/**
* Retrieve default entities
*
* @return array
*/
public function getDefaultEntities()
{
$entities = [
self::ENTITY_TYPE_CODE => [
'entity_model' => 'RH\HelloWorld\Model\ResourceModel\HelloWorld',
'attribute_model' => 'RH\HelloWorld\Model\ResourceModel\Eav\Attribute',
'table' => self::ENTITY_TYPE_CODE,
'increment_model' => null,
'additional_attribute_table' => 'rh_helloworld_eav_attribute',
'entity_attribute_collection' => 'RH\HelloWorld\Model\ResourceModel\Attribute\Collection',
'attributes' => $this->getAttributes(),
],
];
return $entities;
}
}
31) Then, create app/code/RH/HelloWorld/Ui/Component/Form/HelloWorld/DataProvider.php file for data provider file for UI Form :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Ui\Component\Form\HelloWorld;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\FilterPool;
use Magento\Ui\DataProvider\AbstractDataProvider;
use RH\HelloWorld\Model\ResourceModel\HelloWorld\Collection;
class DataProvider extends AbstractDataProvider
{
/**
* @var Collection
*/
protected $collection;
/**
* @var FilterPool
*/
protected $filterPool;
/**
* @var array
*/
protected $loadedData;
/**
* @var RequestInterface
*/
protected $request;
/**
* @param string $name [description]
* @param string $primaryFieldName [description]
* @param string $requestFieldName [description]
* @param Collection $collection [description]
* @param FilterPool $filterPool [description]
* @param RequestInterface $request [description]
* @param array $meta [description]
* @param array $data [description]
*/
public function __construct(
$name,
$primaryFieldName,
$requestFieldName,
Collection $collection,
FilterPool $filterPool,
RequestInterface $request,
array $meta = [],
array $data = []
) {
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
$this->collection = $collection;
$this->filterPool = $filterPool;
$this->request = $request;
}
/**
* Get data
*
* @return array
*/
public function getData()
{
if (!$this->loadedData) {
$storeId = (int) $this->request->getParam('store');
$this->collection->setStoreId($storeId)->addAttributeToSelect('*');
$items = $this->collection->getItems();
foreach ($items as $item) {
$item->setStoreId($storeId);
$this->loadedData[$item->getEntityId()] = $item->getData();
break;
}
}
return $this->loadedData;
}
}
32) Then, create app/code/RH/HelloWorld/Ui/Component/Listing/Column/Bookmark.php file for displays different columns in UI grid :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Api\BookmarkManagementInterface;
use Magento\Ui\Api\BookmarkRepositoryInterface;
class Bookmark extends \Magento\Ui\Component\Bookmark
{
/**
* @var \RH\HelloWorld\Model\HelloWorld
*/
protected $helloworld;
/**
* @param ContextInterface $context
* @param \RH\HelloWorld\Model\HelloWorld $helloworld
* @param BookmarkRepositoryInterface $bookmarkRepository
* @param BookmarkManagementInterface $bookmarkManagement
* @param {Array} array $components
* @param {Array} array $data
*/
public function __construct(
ContextInterface $context,
\RH\HelloWorld\Model\HelloWorld $helloworld,
BookmarkRepositoryInterface $bookmarkRepository,
BookmarkManagementInterface $bookmarkManagement,
array $components = [],
array $data = []
) {
parent::__construct($context, $bookmarkRepository, $bookmarkManagement, $components, $data);
$this->helloworld = $helloworld;
}
/**
* Register component
*
* @return void
*/
public function prepare()
{
$namespace = $this->getContext()->getRequestParam('namespace', $this->getContext()->getNamespace());
$config = [];
if (!empty($namespace)) {
$storeId = $this->getContext()->getRequestParam('store');
if (empty($storeId)) {
$storeId = $this->getContext()->getFilterParam('store_id');
}
$bookmarks = $this->bookmarkManagement->loadByNamespace($namespace);
/** @var \Magento\Ui\Api\Data\BookmarkInterface $bookmark */
foreach ($bookmarks->getItems() as $bookmark) {
if ($bookmark->isCurrent()) {
$config['activeIndex'] = $bookmark->getIdentifier();
}
$config = array_merge_recursive($config, $bookmark->getConfig());
if (!empty($storeId)) {
$config['current']['filters']['applied']['store_id'] = $storeId;
}
}
}
$this->setData('config', array_replace_recursive($config, $this->getConfiguration($this)));
parent::prepare();
$jsConfig = $this->getConfiguration($this);
$this->getContext()->addComponentDefinition($this->getComponentName(), $jsConfig);
}
}
33) Then, create app/code/RH/HelloWorld/Ui/Component/Listing/Column/HelloWorldActions.php file for mass action column in UI grid :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Ui\Component\Listing\Column;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Ui\Component\Listing\Columns\Column;
class HelloWorldActions extends Column
{
/**
* Url path
*/
const URL_PATH_EDIT = 'rh_helloworld/helloworld/edit';
const URL_PATH_DELETE = 'rh_helloworld/helloworld/delete';
/**
* @var UrlInterface
*/
protected $urlBuilder;
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
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'])) {
$storeId = $this->context->getFilterParam('store_id');
foreach ($dataSource['data']['items'] as &$item) {
if (isset($item['entity_id'])) {
$item[$this->getData('name')]['edit'] = [
'href' => $this->urlBuilder->getUrl(
self::URL_PATH_EDIT,
['entity_id' => $item['entity_id'], 'store' => $storeId]
),
'label' => __('Edit'),
'hidden' => false,
];
$item[$this->getData('name')]['delete'] = [
'href' => $this->urlBuilder->getUrl(
self::URL_PATH_DELETE,
['entity_id' => $item['entity_id'], 'store' => $storeId]
),
'label' => __('Delete'),
'confirm' => [
'title' => __('Delete ' . $item['main_title']),
'message' => __('Are you sure you wan\'t to delete a ' . $item['main_title'] . ' record?'),
],
'hidden' => false,
];
}
}
}
return $dataSource;
}
}
34) Then, create app/code/RH/HelloWorld/Ui/Component/Listing/DataProvider/Document.php file for primary id field name :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Ui\Component\Listing\DataProvider;
class Document extends \Magento\Framework\View\Element\UiComponent\DataProvider\Document
{
protected $_idFieldName = 'entity_id';
public function getIdFieldName()
{
return $this->_idFieldName;
}
}
35) Then, create app/code/RH/HelloWorld/Ui/Component/Listing/DataProvider.php file for UI Grid data provider file :
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
namespace RH\HelloWorld\Ui\Component\Listing;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider as UiDataProvider;
class DataProvider extends UiDataProvider
{
/**
* @param SearchResultInterface $searchResult
* @return array
*/
protected function searchResultToOutput(SearchResultInterface $searchResult)
{
/** Add here needed EAV attributes to display on grid **/
$searchResult->setStoreId($this->request->getParam('store', 0))->addAttributeToSelect('*');
return parent::searchResultToOutput($searchResult);
}
/**
* @return void
*/
protected function prepareUpdateUrl()
{
$storeId = $this->request->getParam('store', 0);
if ($storeId) {
$this->data['config']['update_url'] = sprintf(
'%s%s/%s',
$this->data['config']['update_url'],
'store',
$storeId
);
}
return parent::prepareUpdateUrl();
}
}
36) Then, create app/code/RH/HelloWorld/view/adminhtml/layout/rh_helloworld_helloworld_add.xml file for set UI Form layout for new record :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<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="rh_helloworld_helloworld_form"/>
</referenceContainer>
</body>
</page>
37) Then, create app/code/RH/HelloWorld/view/adminhtml/layout/rh_helloworld_helloworld_edit.xml file for set UI Form layout for edit record :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="page.main.actions">
<block class="Magento\Backend\Block\Store\Switcher" name="rh_helloworld_helloworld.store.switcher" template="Magento_Backend::store/switcher.phtml" >
<action method="setUseConfirm">
<argument name="params" xsi:type="string">1</argument>
</action>
</block>
</referenceContainer>
<referenceContainer name="content">
<uiComponent name="rh_helloworld_helloworld_form"/>
</referenceContainer>
</body>
</page>
38) Then, create app/code/RH/HelloWorld/view/adminhtml/layout/rh_helloworld_helloworld_index.xml file for set UI grid layout :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<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="page.main.actions">
<block class="Magento\Backend\Block\Store\Switcher" name="adminhtml.report.grid.store.switcher" template="Magento_Backend::store/switcher.phtml">
<arguments>
<argument name="use_confirm" xsi:type="string">0</argument>
<argument name="switch_websites" xsi:type="string">0</argument>
<argument name="switch_store_groups" xsi:type="string">0</argument>
<argument name="switch_store_views" xsi:type="string">1</argument>
</arguments>
</block>
</referenceContainer>
<referenceContainer name="content">
<uiComponent name="rh_helloworld_helloworld_listing"/>
</referenceContainer>
</body>
</page>
39) Then, create app/code/RH/HelloWorld/view/adminhtml/ui_component/rh_helloworld_helloworld_form.xml file For UI Form layout :
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<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">rh_helloworld_helloworld_form.rh_helloworld_helloworld_form_data_source</item>
<item name="deps" xsi:type="string">rh_helloworld_helloworld_form.rh_helloworld_helloworld_form_data_source</item>
</item>
<item name="config" xsi:type="array">
<item name="dataScope" xsi:type="string">data</item>
</item>
<item name="template" xsi:type="string">templates/form/collapsible</item>
<item name="label" xsi:type="string" translate="true">Hello World Form</item>
<item name="buttons" xsi:type="array">
<item name="back" xsi:type="string">RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit\BackButton</item>
<item name="reset" xsi:type="string">RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit\ResetButton</item>
<item name="delete" xsi:type="string">RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit\DeleteButton</item>
<item name="save" xsi:type="string">RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit\SaveButton</item>
<item name="save_and_continue" xsi:type="string">RH\HelloWorld\Block\Adminhtml\HelloWorld\Edit\SaveAndContinueButton</item>
</item>
</argument>
<dataSource name="rh_helloworld_helloworld_form_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">RH\HelloWorld\Ui\Component\Form\HelloWorld\DataProvider</argument>
<argument name="name" xsi:type="string">rh_helloworld_helloworld_form_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">entity_id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="submit_url" xsi:type="url" path="*/*/save"/>
<item name="validate_url" xsi:type="url" path="*/*/validate"/>
</item>
</argument>
</argument>
<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>
</dataSource>
<fieldset name="main_fieldset">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string" translate="true">Main Information</item>
</item>
</argument>
<field name="main_title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Main Title</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">main_title</item>
<item name="dataScope" xsi:type="string">main_title</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
<!-- Add more field here -->
</fieldset>
</form>
40) In last, create app/code/RH/HelloWorld/view/adminhtml/ui_component/rh_helloworld_helloworld_listing.xml file for UI Grid layout :
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Code standard by : RH
*/
-->
<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">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing_data_source</item>
<item name="deps" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing_data_source</item>
</item>
<item name="spinner" xsi:type="string">rh_helloworld_helloworld_columns</item>
<item name="buttons" xsi:type="array">
<item name="add" xsi:type="array">
<item name="name" xsi:type="string">add</item>
<item name="label" xsi:type="string" translate="true">Add New Record</item>
<item name="class" xsi:type="string">primary</item>
<item name="url" xsi:type="string">*/*/add</item>
</item>
</item>
</argument>
<dataSource name="rh_helloworld_helloworld_listing_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<argument name="class" xsi:type="string">RH\HelloWorld\Ui\Component\Listing\DataProvider</argument>
<argument name="name" xsi:type="string">rh_helloworld_helloworld_listing_data_source</argument>
<argument name="primaryFieldName" xsi:type="string">entity_id</argument>
<argument name="requestFieldName" xsi:type="string">entity_id</argument>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="update_url" xsi:type="url" path="mui/index/render"/>
</item>
</argument>
</argument>
<argument name="data" xsi:type="array">
<item name="js_config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
</item>
</argument>
</dataSource>
<container name="listing_top">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">ui/grid/toolbar</item>
</item>
</argument>
<bookmark name="bookmarks" class="\RH\HelloWorld\Ui\Component\Listing\Column\Bookmark">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/controls/bookmarks/bookmarks</item>
<item name="storageConfig" xsi:type="array">
<item name="saveUrl" xsi:type="url" path="mui/bookmark/save"/>
<item name="deleteUrl" xsi:type="url" path="mui/bookmark/delete"/>
<item name="namespace" xsi:type="string">rh_helloworld_helloworld_listing</item>
</item>
</item>
</argument>
</bookmark>
<columnsControls name="columns_controls"/>
<!-- <bookmark name="bookmarks">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="storageConfig" xsi:type="array">
<item name="namespace" xsi:type="string">rh_helloworld_helloworld_listing</item>
</item>
</item>
</argument>
</bookmark> -->
<component name="columns_controls">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="columnsData" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns</item>
</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/controls/columns</item>
<item name="displayArea" xsi:type="string">dataGridActions</item>
</item>
</argument>
</component>
<filterSearch name="fulltext">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing_data_source</item>
<item name="chipsProvider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.listing_filters_chips</item>
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.bookmarks</item>
<item name="namespace" xsi:type="string">current.search</item>
</item>
</item>
</argument>
</filterSearch>
<filters name="listing_filters">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="columnsProvider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns</item>
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.bookmarks</item>
<item name="namespace" xsi:type="string">current.filters</item>
</item>
<item name="templates" xsi:type="array">
<item name="filters" xsi:type="array">
<item name="select" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
<item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item>
</item>
</item>
</item>
<item name="childDefaults" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.listing_filters</item>
<item name="imports" xsi:type="array">
<item name="visible" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns.${ $.index }:visible</item>
</item>
</item>
</item>
</argument>
</filters>
<massaction name="listing_massaction">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns.ids</item>
<item name="indexField" xsi:type="string">entity_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="*/*/massDelete"/>
<item name="confirm" xsi:type="array">
<item name="title" 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">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns_editor</item>
<item name="target" xsi:type="string">editSelected</item>
</item>
</item>
</argument>
</action>
</massaction>
<paging name="listing_paging">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.bookmarks</item>
<item name="namespace" xsi:type="string">current.paging</item>
</item>
<item name="selectProvider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns.ids</item>
</item>
</argument>
</paging>
</container>
<columns name="rh_helloworld_helloworld_columns">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.bookmarks</item>
<item name="namespace" xsi:type="string">current</item>
</item>
<item name="editorConfig" xsi:type="array">
<item name="selectProvider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_columns.ids</item>
<item name="enabled" xsi:type="boolean">false</item>
<item name="indexField" xsi:type="string">entity_id</item>
<item name="clientConfig" xsi:type="array">
<item name="saveUrl" xsi:type="url" path="*/*/inlineEdit"/>
<item name="validateBeforeSave" xsi:type="boolean">false</item>
</item>
</item>
<item name="childDefaults" xsi:type="array">
<item name="fieldAction" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.rh_helloworld_helloworld_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>
</item>
<item name="storageConfig" xsi:type="array">
<item name="provider" xsi:type="string">rh_helloworld_helloworld_listing.rh_helloworld_helloworld_listing.listing_top.bookmarks</item>
<item name="root" xsi:type="string">columns.${ $.index }</item>
<item name="namespace" xsi:type="string">current.${ $.storageConfig.root }</item>
</item>
</item>
</item>
</argument>
<selectionsColumn name="ids">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="resizeEnabled" xsi:type="boolean">false</item>
<item name="resizeDefaultWidth" xsi:type="string">55</item>
<item name="indexField" xsi:type="string">entity_id</item>
</item>
</argument>
</selectionsColumn>
<column name="entity_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">textRange</item>
<item name="sorting" xsi:type="string">asc</item>
<item name="label" xsi:type="string" translate="true">Id</item>
<item name="sortOrder" xsi:type="number">20</item>
</item>
</argument>
</column>
<column name="main_title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Main Title</item>
<item name="sortOrder" xsi:type="number">30</item>
</item>
</argument>
</column>
<!-- Add more columns here -->
<actionsColumn name="actions" class="RH\HelloWorld\Ui\Component\Listing\Column\HelloWorldActions">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="indexField" xsi:type="string">entity_id</item>
<item name="sortOrder" xsi:type="number">60</item>
</item>
</argument>
</actionsColumn>
</columns>
</listing>
Now, execute this below commands :
php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c
I hope this blog is easy to understand about create custom EAV model extension in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution