1

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.

0

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 me – Rajkumar .E Oct 3 '17 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 Oct 3 '17 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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