context-8.x-4.x-dev/src/Plugin/ContextReaction/Regions.php

src/Plugin/ContextReaction/Regions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
 
namespace Drupal\context\Plugin\ContextReaction;
 
use Drupal\block\BlockRepositoryInterface;
use Drupal\context\ContextReactionPluginBase;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
 
/**
 * Provides a content reaction that will let you disable regions.
 *
 * @ContextReaction(
 *   id = "regions",
 *   label = @Translation("Regions")
 * )
 */
class Regions extends ContextReactionPluginBase implements ContainerFactoryPluginInterface {
 
  /**
   * An array of regions to be disabled with this reaction.
   *
   * @var array
   */
  protected $regions = [];
 
  /**
   * The theme manager.
   *
   * @var \Drupal\Core\Theme\ThemeManagerInterface
   */
  protected $themeManager;
 
  /**
   * The handler of the available themes.
   *
   * @var \Drupal\Core\Extension\ThemeHandlerInterface
   */
  protected $themeHandler;
 
  /**
   * {@inheritdoc}
   */
  public function __construct(
    array $configuration,
    $pluginId,
    $pluginDefinition,
    ThemeManagerInterface $themeManager,
    ThemeHandlerInterface $themeHandler
  ) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->themeManager = $themeManager;
    $this->themeHandler = $themeHandler;
  }
 
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static(
      $configuration,
      $pluginId,
      $pluginDefinition,
      $container->get('theme.manager'),
      $container->get('theme_handler')
    );
  }
 
  /**
   * {@inheritdoc}
   */
  public function summary() {
    return $this->t('Lets you remove regions from selected theme.');
  }
 
  /**
   * Executes the plugin.
   */
  public function execute() {
    // TODO: Implement execute() method.
  }
 
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
 
    $themes = $this->themeHandler->listInfo();
    $default_theme = $this->themeHandler->getDefault();
 
    // Build configuration form for each installed theme.
    foreach ($themes as $theme_id => $theme) {
      if ($theme_id == $default_theme) {
        $title = $this->t('Disable Regions in %theme (Default)', [
          '%theme' => $theme->info['name'],
        ]);
      }
      else {
        $title = $this->t('Disable Regions in %theme', [
          '%theme' => $theme->info['name'],
        ]);
      }
 
      $form[$theme_id] = [
        '#type' => 'details',
        '#title' => $title,
        '#weight' => 5,
        '#open' => FALSE,
      ];
 
      // Get regions of the theme.
      $regions = $this->getSystemRegionList($theme_id);
 
      // Get disabled regions.
      $disabled_regions = $this->getDisabledRegions();
 
      $form[$theme_id]['regions'] = [
        '#type' => 'checkboxes',
        '#options' => $regions,
        '#title' => $this->t('Disable the following'),
        '#default_value' => $disabled_regions[$theme_id] ?? [],
      ];
    }
    return $form;
  }
 
  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $themes = $form_state->getValues();
    if (is_array($themes)) {
      foreach ($themes as $theme_name => $region) {
        $disabled_regions = array_keys(array_filter($region['regions']));
        if (!empty($disabled_regions)) {
          $configuration['regions'][$theme_name] = $disabled_regions;
          $configuration += $this->getConfiguration();
        }
        else {
          $configuration['regions'][$theme_name] = [];
          $configuration += $this->getConfiguration();
        }
        $this->setConfiguration($configuration);
      }
    }
  }
 
  /**
   * Wraps system_region_list().
   *
   * @param string $theme
   *   The theme to get a list of regions for.
   * @param string $show
   *   What type of regions that should be returned, defaults to all regions.
   *
   * @return array
   *   The list of available regions from a specified theme.
   *
   * @todo This could be moved to a service since we use it in a couple of places.
   */
  protected function getSystemRegionList($theme, $show = BlockRepositoryInterface::REGIONS_ALL) {
    return system_region_list($theme, $show);
  }
 
  /**
   * Get disabled regions.
   */
  protected function getDisabledRegions() {
    $configurations = $this->getConfiguration();
    return $configurations['regions'] ?? [];
  }
 
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc