Skip to main content
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