Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Plugin\Condition;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Condition\ConditionPluginBase;
|
Chris@0
|
6 use Drupal\Core\Extension\ThemeHandlerInterface;
|
Chris@0
|
7 use Drupal\Core\Form\FormStateInterface;
|
Chris@0
|
8 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
Chris@0
|
9 use Drupal\Core\Theme\ThemeManagerInterface;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Provides a 'Current Theme' condition.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @Condition(
|
Chris@0
|
16 * id = "current_theme",
|
Chris@0
|
17 * label = @Translation("Current Theme"),
|
Chris@0
|
18 * )
|
Chris@0
|
19 */
|
Chris@0
|
20 class CurrentThemeCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface {
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The theme manager.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @var \Drupal\Core\Theme\ThemeManagerInterface
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $themeManager;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The theme handler.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var \Drupal\Core\Extension\ThemeHandlerInterface
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $themeHandler;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Constructs a CurrentThemeCondition condition plugin.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @param array $configuration
|
Chris@0
|
40 * A configuration array containing information about the plugin instance.
|
Chris@0
|
41 * @param string $plugin_id
|
Chris@0
|
42 * The plugin_id for the plugin instance.
|
Chris@0
|
43 * @param mixed $plugin_definition
|
Chris@0
|
44 * The plugin implementation definition.
|
Chris@0
|
45 * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
|
Chris@0
|
46 * The theme manager.
|
Chris@0
|
47 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
|
Chris@0
|
48 * The theme handler.
|
Chris@0
|
49 */
|
Chris@0
|
50 public function __construct(array $configuration, $plugin_id, $plugin_definition, ThemeManagerInterface $theme_manager, ThemeHandlerInterface $theme_handler) {
|
Chris@0
|
51 parent::__construct($configuration, $plugin_id, $plugin_definition);
|
Chris@0
|
52 $this->themeManager = $theme_manager;
|
Chris@0
|
53 $this->themeHandler = $theme_handler;
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * {@inheritdoc}
|
Chris@0
|
58 */
|
Chris@0
|
59 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
Chris@0
|
60 return new static(
|
Chris@0
|
61 $configuration,
|
Chris@0
|
62 $plugin_id,
|
Chris@0
|
63 $plugin_definition,
|
Chris@0
|
64 $container->get('theme.manager'),
|
Chris@0
|
65 $container->get('theme_handler')
|
Chris@0
|
66 );
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * {@inheritdoc}
|
Chris@0
|
71 */
|
Chris@0
|
72 public function defaultConfiguration() {
|
Chris@0
|
73 return ['theme' => ''] + parent::defaultConfiguration();
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * {@inheritdoc}
|
Chris@0
|
78 */
|
Chris@0
|
79 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
Chris@0
|
80 $form['theme'] = [
|
Chris@0
|
81 '#type' => 'select',
|
Chris@0
|
82 '#title' => $this->t('Theme'),
|
Chris@0
|
83 '#default_value' => $this->configuration['theme'],
|
Chris@0
|
84 '#options' => array_map(function ($theme_info) {
|
Chris@0
|
85 return $theme_info->info['name'];
|
Chris@0
|
86 }, $this->themeHandler->listInfo()),
|
Chris@0
|
87 ];
|
Chris@0
|
88 return parent::buildConfigurationForm($form, $form_state);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * {@inheritdoc}
|
Chris@0
|
93 */
|
Chris@0
|
94 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
Chris@0
|
95 $this->configuration['theme'] = $form_state->getValue('theme');
|
Chris@0
|
96 parent::submitConfigurationForm($form, $form_state);
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 /**
|
Chris@0
|
100 * {@inheritdoc}
|
Chris@0
|
101 */
|
Chris@0
|
102 public function evaluate() {
|
Chris@0
|
103 if (!$this->configuration['theme']) {
|
Chris@0
|
104 return TRUE;
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 return $this->themeManager->getActiveTheme()->getName() == $this->configuration['theme'];
|
Chris@0
|
108 }
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * {@inheritdoc}
|
Chris@0
|
112 */
|
Chris@0
|
113 public function summary() {
|
Chris@0
|
114 if ($this->isNegated()) {
|
Chris@0
|
115 return $this->t('The current theme is not @theme', ['@theme' => $this->configuration['theme']]);
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return $this->t('The current theme is @theme', ['@theme' => $this->configuration['theme']]);
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 /**
|
Chris@0
|
122 * {@inheritdoc}
|
Chris@0
|
123 */
|
Chris@0
|
124 public function getCacheContexts() {
|
Chris@0
|
125 $contexts = parent::getCacheContexts();
|
Chris@0
|
126 $contexts[] = 'theme';
|
Chris@0
|
127 return $contexts;
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 }
|