I have Need to run some script after logged in admin user in magento 2.
But, Its not calling for me.
What i did:
I have created js file this location.
app/code/Vendor/Module/view/adminhtml/web/js/seo_field.js
seo_field.js
define([
"jquery"
], function($){
console.log('user logged in');
});
Then create my Observer:
app/code/Vendor/Module/Observer/BackendAuthUserLoginSuccess.php
BackendAuthUserLoginSuccess.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class BackendAuthUserLoginSuccess implements ObserverInterface
{
protected $logger;
protected $_request;
protected $authSession;
public function __construct(
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\App\RequestInterface $request,
\Magento\Backend\Model\Auth\Session $authSession
)
{
$this->logger = $logger;
$this->_request = $request;
$this->authSession = $authSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
if ($this->authSession->getUser()->getAclRole() == 4) { ?>
<script type="text/x-magento-init">
{
"*": {
"Vendor_Module/js/seo_field":{}
}
}
</script>
<?php $this->logger->addDebug($this->authSession->getUser()->getEmail());
}
}
}
After Called this Observer, its does not call this x-magento-init
Suggest me, What's problem in this code and how to fix this.
1 Answer 1
I have never used this method for adminhtml but I presume it's the same as on the frontend, in which case you're missing js from your path. Try changing
"Vendor_Module/seo_field"
to
"Vendor_Module/js/seo_field"
-
its not works for meRajkumar Elumalai– Rajkumar Elumalai2017年10月03日 13:57:28 +00:00Commented Oct 3, 2017 at 13:57
-
Damn it, that's my only input as I'm a front-end dev so I can't help with the rest of the code sorry. I still think this is correct though so hopefully it helps once the main issue is resolved. Good luck!Ben Crook– Ben Crook2017年10月03日 13:59:28 +00:00Commented Oct 3, 2017 at 13:59
Explore related questions
See similar questions with these tags.