MenuExampleController.php
View source
<?php
namespace Drupal\menu_example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\examples\Utility\DescriptionTemplateTrait;
class MenuExampleController extends ControllerBase {
use DescriptionTemplateTrait;
protected function getModuleName() {
return 'menu_example';
}
public function basicInstructions() {
return [
$this->description(),
];
}
public function alternateMenu() {
return [
'#markup' => $this->t('This will be in the Main menu instead of the default Tools menu'),
];
}
public function permissioned() {
$url = Url::fromRoute('examples.menu_example.permissioned_controlled');
return [
'#markup' => $this->t('A menu item that requires the "access protected menu example" permission is at @link', [
'@link' => Link::createFromRoute($url->getInternalPath(), $url->getRouteName())
->toString(),
]),
];
}
public function permissionedControlled() {
return [
'#markup' => $this->t('This menu entry will not show and the page will not be accessible without the "access protected menu example" permission to current user.'),
];
}
public function customAccess() {
$url = Url::fromRoute('examples.menu_example.custom_access_page');
return [
'#markup' => $this->t('A menu item that requires the user to posess a role of "authenticated" is at @link', [
'@link' => Link::createFromRoute($url->getInternalPath(), $url->getRouteName())
->toString(),
]),
];
}
public function customAccessPage() {
return [
'#markup' => $this->t('This menu entry will not be visible and access will result
in a 403 error unless the user has the "authenticated" role. This is
accomplished with a custom access check plugin.'),
];
}
public function routeOnly() {
$url = Url::fromRoute('examples.menu_example.route_only.callback');
return [
'#markup' => $this->t('A menu entry with no menu link is at @link', [
'@link' => Link::createFromRoute($url->getInternalPath(), $url->getRouteName())
->toString(),
]),
];
}
public function routeOnlyCallback() {
return [
'#markup' => $this->t('The route entry has no corresponding menu links entry, so it provides a route without a menu link, but it is the same in every other way to the simplest example.'),
];
}
public function tabsPage($path, $title) {
$secondary = substr_count($path, '/') > 2 ? 'secondary ' : '';
return [
'#markup' => $this->t('This is the @secondary tab "@tabname" in the "basic tabs" example.', [
'@secondary' => $secondary,
'@tabname' => $title,
]),
];
}
public function urlArgument($arg1, $arg2) {
$url_single = Url::fromRoute('examples.menu_example.use_url_arguments', [
'arg1' => 'one',
]);
$url_double = Url::fromRoute('examples.menu_example.use_url_arguments', [
'arg1' => 'one',
'arg2' => 'two',
]);
$markup = $this->t('This page demonstrates using arguments in the url. For example, access it with @link_single for single argument or @link_double for two arguments in URL', [
'@link_single' => Link::createFromRoute($url_single->getInternalPath(), $url_single->getRouteName(), $url_single->getRouteParameters())
->toString(),
'@link_double' => Link::createFromRoute($url_double->getInternalPath(), $url_double->getRouteName(), $url_double->getRouteParameters())
->toString(),
]);
if (!empty($arg1)) {
$markup .= '<div>' . $this->t('Argument 1 = @arg', [
'@arg' => $arg1,
]) . '</div>';
}
if (!empty($arg2)) {
$markup .= '<div>' . $this->t('Argument 2 = @arg', [
'@arg' => $arg2,
]) . '</div>';
}
return [
'#markup' => $markup,
];
}
public function titleCallbackContent() {
return [
'#markup' => $this->t('The title of this page is dynamically changed by the title callback for this route defined in menu_example.routing.yml.'),
];
}
public function titleCallback() {
return [
'#markup' => $this->t('The new title is your username: @name', [
'@name' => $this->currentUser()
->getDisplayName(),
]),
];
}
public function placeholderArgs() {
$url = Url::fromRoute('examples.menu_example.placeholder_argument.display', [
'arg' => 3343,
]);
return [
'#markup' => $this->t('Demonstrate placeholders by visiting @link', [
'@link' => Link::createFromRoute($url->getInternalPath(), $url->getRouteName(), $url->getRouteParameters())
->toString(),
]),
];
}
public function placeholderArgsDisplay($arg) {
return [
'#markup' => $arg,
];
}
public function pathOverride() {
return [
'#markup' => $this->t('This menu item was created strictly to allow the RouteSubscriber class to have something to operate on. menu_example.routing.yml defined the path as examples/menu-example/menu-original-path. The alterRoutes() changes it to /examples/menu-example/menu-altered-path. You can try navigating to both paths and see what happens!'),
];
}
}