fe6566cddb32a35885c433cc843861ba35040618
[yaffs-website] / menu_test / src / Controller / MenuTestController.php
1 <?php
2
3 namespace Drupal\menu_test\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Routing\RouteMatchInterface;
7 use Drupal\Core\Theme\ThemeManagerInterface;
8 use Drupal\Core\Theme\ThemeNegotiatorInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Controller routines for menu_test routes.
13  */
14 class MenuTestController extends ControllerBase {
15
16   /**
17    * The theme manager.
18    *
19    * @var \Drupal\Core\Theme\ThemeManagerInterface
20    */
21   protected $themeManager;
22
23   /**
24    * The theme negotiator.
25    *
26    * @var \Drupal\Core\Theme\ThemeNegotiatorInterface
27    */
28   protected $themeNegotiator;
29
30   /**
31    * The active route match.
32    *
33    * @var \Drupal\Core\Routing\RouteMatchInterface
34    */
35   protected $routeMatch;
36
37   /**
38    * Constructs the MenuTestController object.
39    *
40    * @param \Drupal\menu_test\Controller\ThemeManagerInterface $theme_manager
41    *   The theme manager.
42    * @param \Drupal\menu_test\Controller\ThemeNegotiatorInterface $theme_negotiator
43    *   The theme negotiator.
44    * @param \Drupal\menu_test\Controller\RouteMatchInterface $route_match
45    *   The current route match.
46    */
47   public function __construct(ThemeManagerInterface $theme_manager, ThemeNegotiatorInterface $theme_negotiator, RouteMatchInterface $route_match) {
48     $this->themeManager = $theme_manager;
49     $this->themeNegotiator = $theme_negotiator;
50     $this->routeMatch = $route_match;
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public static function create(ContainerInterface $container) {
57     return new static(
58       $container->get('theme.manager'),
59       $container->get('theme.negotiator'),
60       $container->get('current_route_match')
61     );
62   }
63
64   /**
65    * Some known placeholder content which can be used for testing.
66    *
67    * @return string
68    *   A string that can be used for comparison.
69    */
70   public function menuTestCallback() {
71     return ['#markup' => 'This is the menuTestCallback content.'];
72   }
73
74
75   /**
76    * A title callback method for test routes.
77    *
78    * @param array $_title_arguments
79    *   Optional array from the route defaults.
80    * @param string $_title
81    *   Optional _title string from the route defaults.
82    *
83    * @return string
84    *   The route title.
85    */
86   public function titleCallback(array $_title_arguments = [], $_title = '') {
87     $_title_arguments += ['case_number' => '2', 'title' => $_title];
88     return t($_title_arguments['title']) . ' - Case ' . $_title_arguments['case_number'];
89   }
90
91   /**
92    * Page callback: Tests the theme negotiation functionality.
93    *
94    * @param bool $inherited
95    *   TRUE when the requested page is intended to inherit
96    *   the theme of its parent.
97    *
98    * @return string
99    *   A string describing the requested custom theme and actual
100    *   theme being used
101    *   for the current page request.
102    */
103   public function themePage($inherited) {
104     $theme_key = $this->themeManager->getActiveTheme()->getName();
105     // Now we check what the theme negotiator service returns.
106     $active_theme = $this->themeNegotiator
107       ->determineActiveTheme($this->routeMatch);
108     $output = "Active theme: $active_theme. Actual theme: $theme_key.";
109     if ($inherited) {
110       $output .= ' Theme negotiation inheritance is being tested.';
111     }
112     return ['#markup' => $output];
113   }
114
115   /**
116    * A title callback for XSS breadcrumb check.
117    *
118    * @return string
119    */
120   public function breadcrumbTitleCallback() {
121     return '<script>alert(123);</script>';
122   }
123
124 }