View source
<?php
namespace Drupal\context\Plugin\ContextReaction;
use Drupal\context\ContextReactionPluginBase;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Regions extends ContextReactionPluginBase implements ContainerFactoryPluginInterface {
protected $regions = [];
protected $themeManager;
protected $themeHandler;
function __construct(array $configuration, $pluginId, $pluginDefinition, ThemeManagerInterface $themeManager, ThemeHandlerInterface $themeHandler) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->themeManager = $themeManager;
$this->themeHandler = $themeHandler;
}
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('theme.manager'), $container
->get('theme_handler'));
}
public function summary() {
return $this
->t('Lets you remove regions from selected theme.');
}
public function execute() {
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$themes = $this->themeHandler
->listInfo();
$default_theme = $this->themeHandler
->getDefault();
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,
];
$regions = $this
->getSystemRegionList($theme_id);
$disabled_regions = $this
->getDisabledRegions();
$form[$theme_id]['regions'] = [
'#type' => 'checkboxes',
'#options' => $regions,
'#title' => $this
->t('Disable the following'),
'#default_value' => isset($disabled_regions[$theme_id]) ? $disabled_regions[$theme_id] : [],
];
}
return $form;
}
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);
}
}
}
protected function getSystemRegionList($theme, $show = REGIONS_ALL) {
return system_region_list($theme, $show);
}
protected function getDisabledRegions() {
$configurations = $this
->getConfiguration();
return isset($configurations['regions']) ? $configurations['regions'] : [];
}
}