1

I am building a custom page in the magento 2 backend for a form to set a customer's password. I have the page opening up from a button on the customer form that passes over the ID. The page looks to work fine, but I seem to be having issues getting the template to show.

This is the block:

 namespace Foo\CustomerResetPass\Block\Adminhtml\resetpassform;

 use \Magento\Backend\Block\Widget\Form\Generic;

 class ResetPassForm extends Generic //\Magento\Backend\Block\Template
 {
     protected $_template = 'resetPass.phtml';

     public function __construct(
         \Magento\Backend\Block\Template\Context $context,
         \Magento\Framework\Registry $registry,
         \Magento\Framework\Data\FormFactory $formFactory,
         \Magento\Store\Model\System\Store $systemStore,
         array $data = []
     ) {
        $this->_systemStore = $systemStore;
         parent::__construct($context, $registry, $formFactory, $data);
     }

    protected function _construct()
     {
         parent::_construct();
         $this->setId('manufacturer_form');
         $this->setTitle(__('Manufacturer Information'));
     }

     public function getModel()
     {
         return $this->_coreRegistry->registry('resetpassform');
     }


     protected function _toHtml() {
         return $this->_template;
     }
 }

This is the controller:

 <?php

      namespace   Foo\CustomerResetPass\Controller\Adminhtml\resetpassform;

      use Magento\Backend\App\Action\Context;
      use Magento\Customer\Model\CustomerRegistry;
      use Magento\Customer\Model\Customer;
      use Magento\Framework\Stdlib\StringUtils as StringHelper;
      use Magento\Framework\App\Config\ScopeConfigInterface;
      use Magento\Framework\App\State as AppState;
      use Magento\Framework\View\Result\PageFactory as ResultPageFactory;
      use Magento\Framework\View\Result\LayoutFactory as ResultLayoutFactory;
      //use Magento\Framework\Registry as Registry;

      class ResetPassForm extends \Magento\Backend\App\Action
      {
          /**
           * @var \Magento\Framework\View\Result\PageFactory
           */
          protected $resultPageFactory;

          /**
           * @var CustomerRegistry
           */
          private $_customerRegistry;

          /**
           * @var Customer
           */
          private $_customer;

          /**
           * @var StringHelper
           */
          private $_stringHelper;

          /**
           * @var ScopeConfigInterface
           */
          private $_scopeConfig;

          /**
           * @var AppState
           */
          private $_appState;


        protected $_coreRegistry = null;


          /**
           * @param CustomerRegistry $customerRegistry
           * @param StringHelper $stringHelper
           * @param ScopeConfigInterface $scopeConfig
           * @param AppState $appState
           * @param \Magento\Backend\App\Action\Context $context
           */

          public function __construct(
              Context $context,
              CustomerRegistry $customerRegistry,
              StringHelper $stringHelper,
              ScopeConfigInterface $scopeConfig,
              AppState $appState,
              ResultPageFactory $resultPageFactory,
            ResultLayoutFactory $resultLayoutFactory,
              //Registry $registry
            \Magento\Framework\Registry $registry
          )
          {
              $this->_appState = $appState;
              $this->_scopeConfig = $scopeConfig;
              $this->_stringHelper = $stringHelper;
              $this->_customerRegistry = $customerRegistry;
            $this->_coreRegistry = $registry;
              $this->resultPageFactory = $resultPageFactory;
              parent::__construct($context, $registry);
          }

          /**
           * {@inheritdoc}
           */
          function execute()
          {
              if (!$this->_appState->getAreaCode()) {
                  $this->_appState->setAreaCode('adminhtml');
              }
            $model = $this->_objectManager->create('Foo\CustomerResetPass\Model\resetpassform\resetpassform');
            $this->_coreRegistry->register('resetpassform', $model);


              $this->_customer = $this->_customerRegistry->retrieve($this->getRequest()->getParam('customer_id'));
              $this->_customer
                  ->changePassword($this->getRequest()->getParam('password'))
                  ->save();
              $resultsPage = $this->resultPageFactory->create();

            $resultsPage->getConfig()->getTitle()->prepend(__('Edit Password'));

              return  $resultsPage;

          }

          /**
           * Make sure that password complies with minimum security requirements.
           *
           * @param string $password
           * @throws \Magento\Framework\Exception\InputException
           */
          protected function checkPasswordStrength($password)
          {
              $length = $this->_stringHelper->strlen($password);
              if ($length > \Magento\Customer\Model\AccountManagement::MAX_PASSWORD_LENGTH) {
                  throw new \Magento\Framework\Exception\InputException(
                      __(
                          'Please enter a password with at most %1 characters.',
                          \Magento\Customer\Model\AccountManagement::MAX_PASSWORD_LENGTH
                      )
                  );
              }
              $configMinPasswordLength = $this->getMinPasswordLength();
              if ($length < $configMinPasswordLength) {
                  throw new \Magento\Framework\Exception\InputException(
                      __(
                          'Please enter a password with at least %1 characters.',
                          $configMinPasswordLength
                      )
                  );
              }
              if ($this->_stringHelper->strlen(trim($password)) != $length) {
                  throw new \Magento\Framework\Exception\InputException(__('The password can\'t begin or end with a space.'));
              }

              $requiredCharactersCheck = $this->makeRequiredCharactersCheck($password);
              if ($requiredCharactersCheck !== 0) {
                  throw new \Magento\Framework\Exception\InputException(
                      __(
                          'Minimum of different classes of characters in password is %1.' .
                          ' Classes of characters: Lower Case, Upper Case, Digits, Special Characters.',
                          $requiredCharactersCheck
                      )
                  );
              }
          }

          /**
           * Retrieve minimum password length
           *
           * @return int
           */
          protected function getMinPasswordLength()
          {
              return $this->_scopeConfig->getValue(\Magento\Customer\Model\AccountManagement::XML_PATH_MINIMUM_PASSWORD_LENGTH);
          }

          /**
           * Check password for presence of required character sets
           *
           * @param string $password
           * @return int
           */
          protected function makeRequiredCharactersCheck($password)
          {
              $counter = 0;
              $requiredNumber = $this->_scopeConfig->getValue(\Magento\Customer\Model\AccountManagement::XML_PATH_REQUIRED_CHARACTER_CLASSES_NUMBER);
              $return = 0;

              if (preg_match('/[0-9]+/', $password)) {
                  $counter ++;
              }
              if (preg_match('/[A-Z]+/', $password)) {
                  $counter ++;
              }
              if (preg_match('/[a-z]+/', $password)) {
                  $counter ++;
              }
              if (preg_match('/[^a-zA-Z0-9]+/', $password)) {
                  $counter ++;
              }

              if ($counter < $requiredNumber) {
                  $return = $requiredNumber;
              }

              return $return;
          }

            protected function _isAllowed()
          {
              return true;
          }


      }

This is the route:

 <?xml version="1.0"?>

 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
     <router id="admin">
         <route id="customerresetpass" frontName="customerresetpass">
             <module  before="Magento_Backend"  name="Foo_CustomerResetPass" />
         </route>
     </router>
 </config>

This is the Layout:

 <?xml version="1.0"?>

 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd" layout="admin-2columns-left">
     <update handle="formkey"/>
     <body>
         <referenceBlock name="page.main.title">
             <action method="setPageTitle">
                 <argument name="title" translate="true" xsi:type="string">Password Reset</argument>
             </action>
         </referenceBlock>
         <referenceContainer name="content">
            <referenceBlock name="resetpassform">
                 <block class="Foo\CustomerResetPass\Block\Adminhtml\resetpassform\resetpassform" name="resetpassform" template="Foo::resetPass.phtml"/>
            </referenceBlock>
            <uiComponent name="resetpassform" />
         </referenceContainer>
     </body>
 </page>

And this is the template:

 <p><?php /* @escapeNotVerified */ echo __('Please enter new customer password below.') ?></p>
 <form id="customerresetpass" method="post" action="<?php echo $block->getUrl('*/*/post') ?>">
     <input type="hidden" value="<?php echo $this->getRequest()->getParam('customer_id'); ?>">
     <label for="password">Password</label>
     <input type="text" id="password" />
     <input type="submit" value="Reset Password" />
 </form>

This is my first time building a page in the backend and the example I found on the magento devdocs wound up breaking the CLI. So if anyone can help or has any pointers in the right direction it would be greatly appreciated.

Thanks in advance!!!

Your Answer

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

Browse other questions tagged or ask your own question.