Mercurial > hg > isophonics-drupal-site
comparison core/modules/system/src/Plugin/Condition/CurrentThemeCondition.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\system\Plugin\Condition; | |
4 | |
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; | |
11 | |
12 /** | |
13 * Provides a 'Current Theme' condition. | |
14 * | |
15 * @Condition( | |
16 * id = "current_theme", | |
17 * label = @Translation("Current Theme"), | |
18 * ) | |
19 */ | |
20 class CurrentThemeCondition extends ConditionPluginBase implements ContainerFactoryPluginInterface { | |
21 | |
22 /** | |
23 * The theme manager. | |
24 * | |
25 * @var \Drupal\Core\Theme\ThemeManagerInterface | |
26 */ | |
27 protected $themeManager; | |
28 | |
29 /** | |
30 * The theme handler. | |
31 * | |
32 * @var \Drupal\Core\Extension\ThemeHandlerInterface | |
33 */ | |
34 protected $themeHandler; | |
35 | |
36 /** | |
37 * Constructs a CurrentThemeCondition condition plugin. | |
38 * | |
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 | |
46 * The theme manager. | |
47 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler | |
48 * The theme handler. | |
49 */ | |
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; | |
54 } | |
55 | |
56 /** | |
57 * {@inheritdoc} | |
58 */ | |
59 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
60 return new static( | |
61 $configuration, | |
62 $plugin_id, | |
63 $plugin_definition, | |
64 $container->get('theme.manager'), | |
65 $container->get('theme_handler') | |
66 ); | |
67 } | |
68 | |
69 /** | |
70 * {@inheritdoc} | |
71 */ | |
72 public function defaultConfiguration() { | |
73 return ['theme' => ''] + parent::defaultConfiguration(); | |
74 } | |
75 | |
76 /** | |
77 * {@inheritdoc} | |
78 */ | |
79 public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
80 $form['theme'] = [ | |
81 '#type' => 'select', | |
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()), | |
87 ]; | |
88 return parent::buildConfigurationForm($form, $form_state); | |
89 } | |
90 | |
91 /** | |
92 * {@inheritdoc} | |
93 */ | |
94 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
95 $this->configuration['theme'] = $form_state->getValue('theme'); | |
96 parent::submitConfigurationForm($form, $form_state); | |
97 } | |
98 | |
99 /** | |
100 * {@inheritdoc} | |
101 */ | |
102 public function evaluate() { | |
103 if (!$this->configuration['theme']) { | |
104 return TRUE; | |
105 } | |
106 | |
107 return $this->themeManager->getActiveTheme()->getName() == $this->configuration['theme']; | |
108 } | |
109 | |
110 /** | |
111 * {@inheritdoc} | |
112 */ | |
113 public function summary() { | |
114 if ($this->isNegated()) { | |
115 return $this->t('The current theme is not @theme', ['@theme' => $this->configuration['theme']]); | |
116 } | |
117 | |
118 return $this->t('The current theme is @theme', ['@theme' => $this->configuration['theme']]); | |
119 } | |
120 | |
121 /** | |
122 * {@inheritdoc} | |
123 */ | |
124 public function getCacheContexts() { | |
125 $contexts = parent::getCacheContexts(); | |
126 $contexts[] = 'theme'; | |
127 return $contexts; | |
128 } | |
129 | |
130 } |