LanguageSwitchMenuLink.php in Cheeseburger Menu 5.0.x
File
src/Plugin/Menu/LanguageSwitchMenuLink.php
View source
<?php
namespace Drupal\cheeseburger_menu\Plugin\Menu;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class LanguageSwitchMenuLink extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected $languageManager;
protected $pathMatcher;
protected $entityTypeManager;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher, EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->pathMatcher = $path_matcher;
$this->entityTypeManager = $entity_type_manager;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('language_manager'), $container
->get('path.matcher'), $container
->get('entity_type.manager'), $container
->get('current_route_match'));
}
public function getTitle() {
$language_menu = $this->entityTypeManager
->getStorage('menu')
->load('language');
$langcode = $this
->getPluginDefinition()['options']['langcode'];
return $language_menu
->getThirdPartySetting('cheeseburger_menu', 'use_langcode', TRUE) ? $langcode : $this->languageManager
->getLanguageName($langcode);
}
public function getUrlObject($title_attribute = TRUE) {
if ($this->pathMatcher
->isFrontPage()) {
$url = Url::fromRoute('<front>');
}
else {
$current_route = $this->routeMatch
->getRouteObject();
$options = $current_route
->getOptions();
$url = Url::fromRoute($this->routeMatch
->getRouteName(), $this->routeMatch
->getRawParameters()
->all(), $options);
}
$language = $this->languageManager
->getLanguage($this
->getPluginDefinition()['options']['langcode']);
return $url
->setOption('language', $language);
}
public function getDescription() {
return $this
->t('Menu link to current page on certain language');
}
public function updateLink(array $new_definition_values, $persist) {
return $new_definition_values;
}
public function getCacheContexts() {
return [
'url.path',
];
}
public function getCacheTags() {
return [
'config:system.menu.language',
];
}
}