3 namespace Drupal\system\Plugin\Condition;
5 use Drupal\Core\Condition\ConditionPluginBase;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9 use Drupal\Core\Theme\ThemeManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Provides a 'Current Theme' condition.
16 * id = "current_theme",
17 * label = @Translation("Current Theme"),
20 class CurrentThemeCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface {
25 * @var \Drupal\Core\Theme\ThemeManagerInterface
27 protected $themeManager;
32 * @var \Drupal\Core\Extension\ThemeHandlerInterface
34 protected $themeHandler;
37 * Constructs a CurrentThemeCondition condition plugin.
39 * @param array $configuration
40 * A configuration array containing information about the plugin instance.
41 * @param string $plugin_id
42 * The plugin_id for the plugin instance.
43 * @param mixed $plugin_definition
44 * The plugin implementation definition.
45 * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
47 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
50 public function __construct(array $configuration, $plugin_id, $plugin_definition, ThemeManagerInterface $theme_manager, ThemeHandlerInterface $theme_handler) {
51 parent::__construct($configuration, $plugin_id, $plugin_definition);
52 $this->themeManager = $theme_manager;
53 $this->themeHandler = $theme_handler;
59 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64 $container->get('theme.manager'),
65 $container->get('theme_handler')
72 public function defaultConfiguration() {
73 return ['theme' => ''] + parent::defaultConfiguration();
79 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
82 '#title' => $this->t('Theme'),
83 '#default_value' => $this->configuration['theme'],
84 '#options' => array_map(function ($theme_info) {
85 return $theme_info->info['name'];
86 }, $this->themeHandler->listInfo()),
88 return parent::buildConfigurationForm($form, $form_state);
94 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
95 $this->configuration['theme'] = $form_state->getValue('theme');
96 parent::submitConfigurationForm($form, $form_state);
102 public function evaluate() {
103 if (!$this->configuration['theme']) {
107 return $this->themeManager->getActiveTheme()->getName() == $this->configuration['theme'];
113 public function summary() {
114 if ($this->isNegated()) {
115 return $this->t('The current theme is not @theme', ['@theme' => $this->configuration['theme']]);
118 return $this->t('The current theme is @theme', ['@theme' => $this->configuration['theme']]);
124 public function getCacheContexts() {
125 $contexts = parent::getCacheContexts();
126 $contexts[] = 'theme';