Drupal 8 Title Change for Dynamic Routs

There are lots of ways to change the title of routs in Drupal 8 but some times you find some thing difficult but the answer is as follows:

1) create a service that tags:

 

 mymodule.route_subscriber:
    class: Drupal\mymodule\Routing\SomeWebFormSubscriber
    tags:
      - { name: event_subscriber }


2) Create The Class

<?php

namespace Drupal\mymodule\Routing;

use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;

/**
 * Class SomeWebFormSubscriber.
 *
 * @package Drupal\mymodule\Routing
 * Listens to the dynamic route events.
 */
class SomeWebFormSubscriber extends RouteSubscriberBase {

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    if ($route = $collection->get('entity.webform_submission.canonical')) {
      // $route->setDefault('_title', 'Test');
      $route->setDefault('_title_callback', 'Drupal\mymodule\Controller\WebformSubmissionTitleController::title');
    }
  }

}

:Above you can see 2 options (_title)  if the rout has _title set or ('_title_callback')

3) if the rout has _title_callback:
in my cace i am overiding a webform submission title:

<?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformRequestInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Provides custom title for webform submissions.
 */
class WebformSubmissionTitleController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * Webform request handler.
   *
   * @var \Drupal\webform\WebformRequestInterface
   */
  protected $requestHandler;

  /**
   * EntityTypeManager definition.
   *
   * @var EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new WebformSubmissionController object.
   *
   * @param \Drupal\webform\WebformRequestInterface $request_handler
   *   The webform request handler.
   */
  public function __construct(WebformRequestInterface $request_handler, EntityTypeManagerInterface $entity_type_manager) {
    $this->requestHandler = $request_handler;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('webform.request'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * Route title callback.
   *
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   The webform submission.
   *
   * @return mixed
   *   The webform submission as a render array.
   */
  public function title(WebformSubmissionInterface $webform_submission) {
    $data = $webform_submission->getData();
    if (!empty($data['hidden_node_id'])) {
      $storage = $this->entityTypeManager->getStorage('node');
      $node = $storage->load($data['hidden_node_id']);
      if (is_object($node)) {
        $title = $node->getTitle();
        return $this->t('Investigation: @title', ['@title' => $title]);
      }
      else {
        return $this->t('Submission');
      }
    }
    else {
      $source_entity = $this->requestHandler->getCurrentSourceEntity('webform_submission');
      $t_args = [
        '@form' => ($source_entity) ? $source_entity->label() : $webform_submission->getWebform()->label(),
        '@id' => $webform_submission->serial(),
      ];
      return $this->t('@form: Submission #@id', $t_args);
    }
  }

}

 

Category: