0

I have created admin grid and associated form. In that, If I add a new item the time and date is automatically saved. But the name of who is added the item and who is edited the item is also need to be save in DB.How to retrieve these things and dispaly it in Backend ? Please suggest me a solution ... Thanks in advance.

I have created below files to display it

xxx\HomeSlider\Block\Adminhtml\Edit\Tab\View\AdditionalInfo.php

<?php

namespace xxx\HomeSlider\Block\Adminhtml\Edit\Tab\View;

use Magento\Framework\Exception\NoSuchEntityException;
use xxx\HomeSlider\Model\Post;

class AdditionalInfo extends \Magento\Backend\Block\Template
{

    /**
     * Interval in minutes that shows how long customer will be marked 'Online'
     * since his last activity. Used only if it's impossible to get such setting
     * from configuration.
     */
    const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;

    protected $dateTime;


    protected $coreRegistry;
    protected $authSession;

    public function __construct(
    \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Backend\Model\Auth\Session $authSession, array $data = []
    )
    {

        $this->authSession = $authSession;

        parent::__construct($context, $data);
    }

    public function getCurrentUser()
    {
        return $this->authSession->getUser();
    }

}

Additionalinfo.phtml

 <?php
    $adminName = $block->getCurrentUser();

    ?>
    <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 At:')) ?></th>
                    <td><?= $block->escapeHtml($adminName) ?></td>
                </tr>

            </tbody>
        </table>


    </div>
    <?php

Controller file save.php

<?php

namespace OX\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
    )
    {
        $this->resultRedirectFactory = $resultRedirectFactory;
        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');
        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');
    }

}

Controller for edit.php

<?php
namespace OX\HomeSlider\Controller\Adminhtml\Post;
class Edit extends \Magento\Backend\App\Action
{
    protected $_resultPage;
    protected $_resultPageFactory = false;
    protected $_coreRegistry;
    public function __construct(
    \Magento\Backend\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Framework\Registry $coreRegistry
    )
    {
        parent::__construct($context);
        $this->_resultPageFactory = $resultPageFactory;
        $this->_coreRegistry = $coreRegistry;
    }
    public function execute()
    {
        $id = $this->getRequest()->getParam('id');
        $post = $this->_objectManager->create('OX\HomeSlider\Model\Post');
        if ($id) {
            $post = $post->load($id);
            if ($post->getId()) {
                $this->_coreRegistry->register('homepageslider_post', $post);
            }
        }

        $resultPage = $this->getResultPage();
        $resultPage->getConfig()->getTitle()->prepend(__('Image Information'));
        return $resultPage;
    }
    public function getResultPage()
    {
        if (is_null($this->_resultPage)) {
            $this->_resultPage = $this->_resultPageFactory->create();
        }
        return $this->_resultPage;
    }
}

1 Answer 1

1

you can use this to get current admin details :-

$this->authSession->getUser()->getUsername();//get admin username
$this->authSession->getUser()->getEmail();//get admin email

To save admin username in database change controller file of save action to below

<?php

namespace OX\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['username'] = $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();
    }

}
8
  • Then how to save this user name in to DB
    – Jaisa
    Commented Nov 21, 2017 at 12:54
  • where you want to save username of admin ?
    – mansi
    Commented Nov 21, 2017 at 13:46
  • In my custom table which is stored in DB
    – Jaisa
    Commented Nov 21, 2017 at 13:47
  • $username = $this->authSession->getUser()->getUsername(); $model = $this->modelnameFactory->create(); $model->setUsername($username); $model->save();
    – mansi
    Commented Nov 21, 2017 at 13:51
  • See my updated post.
    – Jaisa
    Commented Nov 21, 2017 at 13:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.