0

In my custom module amap, I have amap.routing.yml with (among others) this route:

amap.distribution_inscription_many_form:
  path: '/amap/editPlanningMany'
  defaults:
    _form: '\Drupal\amap\Form\DistributionInscriptionManyForm'
    _title: 'Update Planning'
  requirements:
    _permission: 'edit distribution inscription entities'

I'd like to have a link in the title of the route, something like
Update Planning <a href="/amap/editPlanningOne">Click here</a>

Drupal 8 API documentation says in Structure of routes

_title: The page title for the route. It may differ from the menu link title.
_title_arguments: Additional arguments for the title text passed along to t().
_title_context: Additional context information for the title text passed along to t().
_title_callback: A PHP callable (typically classname::method) returning the page title for the route.

but I don't know how to use it...
How can I do that?

1

In you routing.yml change _title to _title_callback as below:

amap.distribution_inscription_many_form:
  path: '/amap/editPlanningMany'
  defaults:
    _form: '\Drupal\amap\Form\DistributionInscriptionManyForm'
    _title_callback: '\Drupal\amap\Controller\TitleController::title'
  requirements:
    _permission: 'edit distribution inscription entities'

Create a "TitleController" controller in you amap module unser "src/Controller"

TitleController.php

<?php

namespace Drupal\amap\Controller;

use Drupal\Core\Controller\ControllerBase;

class TitleController extends ControllerBase {

  public function title() {
    return t('Update Planning <a href="/amap/editPlanningOne">Click here</a>');
  }

}

Now your title will be callable from the controller and you can give whatever that likes you here.

  • I tried something like return t('Update Planning') . ' ' . t('<a href="/amap/editPlanningOne">Click here</a>'); and it doesn't work! Any idea why? – gbmapo May 15 at 7:13
  • IMO you have to use only one t() to render proper URL. – Ajay Reddy May 15 at 9: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.