annotate core/modules/language/src/Plugin/Block/LanguageBlock.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\language\Plugin\Block;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessResult;
Chris@0 6 use Drupal\Core\Block\BlockBase;
Chris@0 7 use Drupal\Core\Path\PathMatcherInterface;
Chris@0 8 use Drupal\Core\Session\AccountInterface;
Chris@0 9 use Drupal\Core\Language\LanguageManagerInterface;
Chris@0 10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Chris@0 11 use Drupal\Core\Url;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Provides a 'Language switcher' block.
Chris@0 16 *
Chris@0 17 * @Block(
Chris@0 18 * id = "language_block",
Chris@0 19 * admin_label = @Translation("Language switcher"),
Chris@0 20 * category = @Translation("System"),
Chris@0 21 * deriver = "Drupal\language\Plugin\Derivative\LanguageBlock"
Chris@0 22 * )
Chris@0 23 */
Chris@0 24 class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The language manager.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Language\LanguageManagerInterface
Chris@0 30 */
Chris@0 31 protected $languageManager;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The path matcher.
Chris@0 35 *
Chris@0 36 * @var \Drupal\Core\Path\PathMatcherInterface
Chris@0 37 */
Chris@0 38 protected $pathMatcher;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Constructs an LanguageBlock object.
Chris@0 42 *
Chris@0 43 * @param array $configuration
Chris@0 44 * A configuration array containing information about the plugin instance.
Chris@0 45 * @param string $plugin_id
Chris@0 46 * The plugin_id for the plugin instance.
Chris@0 47 * @param mixed $plugin_definition
Chris@0 48 * The plugin implementation definition.
Chris@0 49 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
Chris@0 50 * The language manager.
Chris@0 51 * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
Chris@0 52 * The path matcher.
Chris@0 53 */
Chris@0 54 public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, PathMatcherInterface $path_matcher) {
Chris@0 55 parent::__construct($configuration, $plugin_id, $plugin_definition);
Chris@0 56 $this->languageManager = $language_manager;
Chris@0 57 $this->pathMatcher = $path_matcher;
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * {@inheritdoc}
Chris@0 62 */
Chris@0 63 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
Chris@0 64 return new static(
Chris@0 65 $configuration,
Chris@0 66 $plugin_id,
Chris@0 67 $plugin_definition,
Chris@0 68 $container->get('language_manager'),
Chris@0 69 $container->get('path.matcher')
Chris@0 70 );
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * {@inheritdoc}
Chris@0 75 */
Chris@0 76 protected function blockAccess(AccountInterface $account) {
Chris@0 77 $access = $this->languageManager->isMultilingual() ? AccessResult::allowed() : AccessResult::forbidden();
Chris@0 78 return $access->addCacheTags(['config:configurable_language_list']);
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 public function build() {
Chris@0 85 $build = [];
Chris@0 86 $route_name = $this->pathMatcher->isFrontPage() ? '<front>' : '<current>';
Chris@0 87 $type = $this->getDerivativeId();
Chris@0 88 $links = $this->languageManager->getLanguageSwitchLinks($type, Url::fromRoute($route_name));
Chris@0 89
Chris@0 90 if (isset($links->links)) {
Chris@0 91 $build = [
Chris@0 92 '#theme' => 'links__language_block',
Chris@0 93 '#links' => $links->links,
Chris@0 94 '#attributes' => [
Chris@0 95 'class' => [
Chris@0 96 "language-switcher-{$links->method_id}",
Chris@0 97 ],
Chris@0 98 ],
Chris@0 99 '#set_active_class' => TRUE,
Chris@0 100 ];
Chris@0 101 }
Chris@0 102 return $build;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * {@inheritdoc}
Chris@0 107 *
Chris@0 108 * @todo Make cacheable in https://www.drupal.org/node/2232375.
Chris@0 109 */
Chris@0 110 public function getCacheMaxAge() {
Chris@0 111 return 0;
Chris@0 112 }
Chris@0 113
Chris@0 114 }