n How to translate page title of custom controller in drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

This snippet shows you how to show a title depending on your language.

mymodule.routing.yml:

mymodule.custom_page:
  path: '/my-custom-page'
  defaults:
    _title_callback: '\Drupal\mymodule\Controller\customPageController::getTitle'
    _controller: '\Drupal\mymodule\Controller\customPageController::render'

 

customPageController.php:

namespace Drupal\mymodule\Controller;
use Drupal\Core\Controller\ControllerBase;

class customPageController extends ControllerBase {
  public static function render() {
    
  }

  /**
   * Returns a page title.
   */
  public function getTitle() {
  
    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
    switch($language) {
      case 'en':
        $title = 'hello';
        break;
      case 'fr':
        $title = 'salut';
        break;
    }
    return  $title;
  }

}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook