ContextGroupsManager.php in Context groups 8.2
File
src/ContextGroupsManager.php
View source
<?php
namespace Drupal\context_groups;
use Drupal\context\Entity\Context;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use stdClass;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContextGroupsManager {
protected $themeHandler;
public function __construct(ThemeHandlerInterface $themeHandler) {
$this->themeHandler = $themeHandler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('theme_handler'));
}
public function getParams(Context $context, FormStateInterface $form_state) {
$theme = $this
->getCurrentTheme($form_state);
$groups = $context
->getThirdPartySettings('context_groups');
if (empty($groups)) {
return FALSE;
}
$group_list[''] = t('None');
foreach ($groups as $group_name => $value) {
if ($value['theme'] == $theme) {
$group_list[$group_name] = $value['label'];
$group_data[$group_name] = $value;
$group_data[$group_name]['id'] = $group_name;
}
}
if (!isset($group_data)) {
return FALSE;
}
$params = new stdClass();
$params->groups = $group_data;
$params->group_list = $group_list;
$params->groups_by_region = [];
foreach ($params->groups as $group) {
$params->groups_by_region['region-' . $group['region']][] = $group;
}
return $params;
}
public function getCurrentTheme(FormStateInterface $form_state) {
if (!empty($form_state
->getUserInput()['reactions']['blocks']['theme'])) {
$theme = $form_state
->getUserInput()['reactions']['blocks']['theme'];
}
else {
$theme = $this->themeHandler
->getDefault();
}
return $theme;
}
public function getAllParentsForGroup(array $groups, $group, array $parents = []) {
if (empty($group)) {
return array_reverse($parents);
}
else {
$parents[] = $group;
return $this
->getAllParentsForGroup($groups, $groups[$group]['parent'], $parents);
}
}
}