1

How I can change page header title from controller in admin page?

enter image description here

In the picture I want to change Dashboard to something else. I tried below code in execute method of the controller

$pageFactory = $this->resultPageFactory->create();
$pageFactory->getConfig()->getTitle()->prepend(__("Your Title"));

It is adding the title to the browsers tab but not changing the header title in the main page. How to change main page title?

Below is the html shown on inspecting in the browser.

<div class="page-header-hgroup col-l-8 col-m-6">
<div class="page-title-wrapper">
    <h1 class="page-title">Dashboard</h1>
    </div>
</div>

I have also tried below code

 $this->_view->getLayout()->getBlock('page.main.title')->setPageTitle('Something else');
 $this->_view->renderLayout();

It says Error: Call to a member function setPageTitle() on bool.

3 Answers 3

0

its loading from below file

\vendor\magento\module-backend\Controller\Adminhtml\Dashboard\Index.php
0

This is the place where you can change the title -
your_installed_folder_root/vendor/magento/module-backend/Controller/Adminhtml/Dashboard/Index.php
Please check this screenshot at line number 41, you can edit it from here.
enter image description here


And I have tested it also. After replace then save and just do hard refresh browser will show you this change. As shown in the below picture.
enter image description here

EDITED: As your code is custom module so this is the file location is

/var/www/html/local_m241/lcl_m241/app/code/your_vendor/your_module/Controller/Adminhtml/Items/Index.php:

Test Items is the Heading here. enter image description here

   15          $resultPage = $this->resultPageFactory->create();
   16          $resultPage->setActiveMenu('Biren_Crudimage::test');
   17:         $resultPage->getConfig()->getTitle()->prepend(__('Test Items'));
   18          $resultPage->addBreadcrumb(__('Test'), __('Test'));
   19          $resultPage->addBreadcrumb(__('Items'), __('Items'));
2
  • I also tried this core file change for testing but it didn't work for me. It is changing title in the tab only not on the page.
    – amitshree
    Jun 19, 2021 at 17:04
  • I edited my solution and at the bottom side, I added the code that is for custom module. Jun 19, 2021 at 19:25
0

I was able to make it work using below code in the controller:

 $this->_initAction();
 $this->_view->getLayout()->getBlock('blocks_blocks_edit');
 $this->_view->getPage()->getConfig()->getTitle()->prepend(__("test title")); // Added this line
 $this->_view->renderLayout();

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.