Language.php in GoogleTagManager 8
File
src/Plugin/Condition/Language.php
View source
<?php
namespace Drupal\google_tag\Plugin\Condition;
use Drupal\google_tag\ConditionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Language extends ConditionBase implements ContainerFactoryPluginInterface {
protected $languageManager;
public function __construct(LanguageManagerInterface $language_manager, array $configuration, $plugin_id, $plugin_definition) {
$this->toggle = 'language_toggle';
$this->list = 'language_list';
$this->singular = 'language';
$this->plural = 'languages';
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->languageManager = $language_manager;
$this->options = $this
->languageOptions();
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($container
->get('language_manager'), $configuration, $plugin_id, $plugin_definition);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
if (!$this->languageManager
->isMultilingual()) {
$form['language_list'] = [
'#type' => 'value',
'#default_value' => $this->configuration['language_list'],
];
}
return $form;
}
public function summary() {
$languages = $this->languageManager
->getLanguages(LanguageInterface::STATE_ALL);
$selected = $this->configuration['language_list'];
$this->values = array_reduce($languages, function (&$names, $language) use ($selected) {
if (!empty($selected[$language
->getId()])) {
$names[$language
->getId()] = $language
->getName();
}
return $names;
}, []);
return parent::summary();
}
public function languageOptions() {
$options = [];
if ($this->languageManager
->isMultilingual()) {
$languages = $this->languageManager
->getLanguages();
foreach ($languages as $language) {
$options[$language
->getId()] = $language
->getName();
}
}
return $options;
}
public function contextToEvaluate() {
return $this
->getContextValue('language')
->getId();
}
}