LanguageBlock.php in Zircon Profile 8.0
File
core/modules/language/src/Plugin/Block/LanguageBlock.php
View source
<?php
namespace Drupal\language\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Path\PathMatcherInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $languageManager;
protected $pathMatcher;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->pathMatcher = $path_matcher;
}
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'));
}
protected function blockAccess(AccountInterface $account) {
$access = $this->languageManager
->isMultilingual() ? AccessResult::allowed() : AccessResult::forbidden();
return $access
->addCacheTags([
'config:configurable_language_list',
]);
}
public function build() {
$build = array();
$route_name = $this->pathMatcher
->isFrontPage() ? '<front>' : '<current>';
$type = $this
->getDerivativeId();
$links = $this->languageManager
->getLanguageSwitchLinks($type, Url::fromRoute($route_name));
if (isset($links->links)) {
$build = array(
'#theme' => 'links__language_block',
'#links' => $links->links,
'#attributes' => array(
'class' => array(
"language-switcher-{$links->method_id}",
),
),
'#set_active_class' => TRUE,
);
}
return $build;
}
public function getCacheMaxAge() {
return 0;
}
}