I have created admin grid and associated UI form. If I add a new item, the current (first)admin name who added the item should save in DB. That (first)admin name would not be change. But if the second admin logged in(using acl) and started to edit the existing items, while saving the item, It changes the created person name as current admin(Second admin)name. So please suggest me a solution to overcome this problem, to make created person name as unchangeable if second admin logged in.
Controller for save.php
<?php
namespace XXX\HomeSlider\Controller\Adminhtml\Post;
class Save extends \Magento\Backend\App\Action
{
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Backend\Model\View\Result\RedirectFactory $resultRedirectFactory,
\Magento\Backend\Model\Auth\Session $authSession
)
{
$this->resultRedirectFactory = $resultRedirectFactory;
$this->authSession = $authSession;
parent::__construct($context);
}
public function execute()
{
$id = $this->getRequest()->getParam('id');
$postModel = $this->_objectManager->create('OX\HomeSlider\Model\Post');
if ($id) {
$postModel = $postModel->load($id);
}
$post = $this->getRequest()->getParam('general');
$user = $this->getCurrentUser();
$post['created_by'] = $user;
$post['updated_by'] = $user;
if(empty($post['id'])) {
$post['id'] = null;
}
if (isset($post['image'])) {
$post['image'] = $post['image'][0]['name'];
} else {
$post['image'] = null;
}
$postModel->setData($post);
$postModel->save();
return $this->resultRedirectFactory->create()->setPath('homeslider/post/index');
}
public function getCurrentUser()
{
return $this->authSession->getUser()->getUsername();
}
}
Block file additional_info.php
<?php
namespace OX\HomeSlider\Block\Adminhtml\Edit\Tab\View;
use Magento\Framework\Exception\NoSuchEntityException;
use OX\HomeSlider\Model\Post;
class AdditionalInfo extends \Magento\Backend\Block\Template
{
protected $_coreRegistry;
protected $authSession;
protected $_timezone;
public function __construct(
\Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezone, \Magento\Backend\Model\Auth\Session $authSession, array $data = []
)
{
$this->_coreRegistry = $registry;
$this->authSession = $authSession;
$this->_timezone = $timezone;
parent::__construct($context, $data);
}
public function getCurrentUser()
{
return $this->authSession->getUser()->getUsername();
}
public function getCreatedAt()
{
$postModel = $this->_coreRegistry->registry('homepageslider_post')->getCreatedAt();
return $date = $this->_timezone->date($postModel)->format('d/m/y H:i:s A');
}
public function getUpdatedAt()
{
$postModel = $this->_coreRegistry->registry('homepageslider_post')->getUpdatedAt();
return $date = $this->_timezone->date($postModel)->format('d/m/y H:i:s A');
}
public function getUpdatedUser()
{
return $this->authSession->getUser()->getUsername();
}
}
Template file additionalinfo.phtml
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/** @var \OX\HomeSlider\Block\Adminhtml\Edit\Tab\View\AdditionalInfo $block */
$adminName = $block->getCurrentUser();
$createdAt = $block->getCreatedAt();
$updatedAt = $block->getUpdatedAt();
$updatedBy = $block->getUpdatedUser();
?>
<div class="fieldset-wrapper customer-information">
<div class="fieldset-wrapper-title">
<span class="title"><?= $block->escapeHtml(__('Additional Information')) ?></span>
</div>
<table class="admin__table-secondary">
<tbody>
<?= $block->getChildHtml() ?>
<tr>
<th><?= $block->escapeHtml(__('Created By:')) ?></th>
<td><?= $block->escapeHtml($adminName) ?></td>
</tr>
<tr>
<th><?= $block->escapeHtml(__('Created At:')) ?></th>
<td><?= $block->escapeHtml($createdAt) ?></td>
</tr>
<tr>
<th><?= $block->escapeHtml(__('Updated At:')) ?></th>
<td><?= $block->escapeHtml($updatedAt) ?></td>
</tr>
<tr>
<th><?= $block->escapeHtml(__('Updated By:')) ?></th>
<td><?= $block->escapeHtml($updatedBy) ?></td>
</tr>
</tbody>
</table>
</div>
<?php