View source
<?php
namespace Drupal\context_groups\Plugin\DisplayVariant;
use Drupal\context\ContextManager;
use Drupal\context_groups\ContextGroupsManager;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\context\Plugin\DisplayVariant\ContextBlockPageVariant;
class ContextGroupsBlockPageVariant extends ContextBlockPageVariant {
protected $contextGroupsManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextManager $contextManager, ContextGroupsManager $contextGroupsManager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $contextManager);
$this->contextGroupsManager = $contextGroupsManager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('context.manager'), $container
->get('context_groups.manager'));
}
public function build() {
$build = parent::build();
$rendered_context_groups = $this
->getNonEmptyContextGroups($build);
$contexts = $this->contextManager
->getActiveContexts();
foreach ($contexts as $context) {
$groups = $context
->getThirdPartySettings('context_groups');
foreach ($groups as $key => $data) {
if (!in_array($key, $rendered_context_groups)) {
continue;
}
if (empty($data['parent'])) {
$old_data = isset($build[$data['region']][$key]) ? $build[$data['region']][$key] : [];
$new_data = [
'#theme' => 'context_groups',
'#attributes' => [
'class' => [
'context-groups context-groups-' . $key . ' ' . $data['class'],
],
],
'#weight' => $data['weight'],
'#context_id' => $context
->getName(),
'#context_group' => $key,
];
$group_content = array_merge_recursive($old_data, $new_data);
$build[$data['region']][$key] = $group_content;
}
else {
$build_parent =& $build[$data['region']];
foreach ($data['all_parents'] as $value) {
$build_parent =& $build_parent[$value];
}
$old_data = isset($build_parent[$key]) ? $build_parent[$key] : [];
$new_data = [
'#theme' => 'context_groups',
'#attributes' => [
'class' => [
'context-groups context-groups-' . $key . ' ' . $data['class'],
],
],
'#weight' => $data['weight'],
'#context_id' => $context
->getName(),
'#context_group' => $key,
];
$merge = array_merge_recursive($old_data, $new_data);
$build_parent[$key] = $merge;
}
}
}
foreach (Element::children($build) as $region_name) {
foreach ($build[$region_name] as $key => $block) {
if (isset($block['#type']) && $block['#type'] == 'container') {
continue;
}
if (!empty($block['#configuration']['all_parents'])) {
$build_parent =& $build[$region_name];
foreach ($block['#configuration']['all_parents'] as $value) {
$build_parent =& $build_parent[$value];
}
$build_parent[$key] = $block;
unset($build[$region_name][$key]);
}
}
}
return $build;
}
private function getNonEmptyContextGroups(array $build) {
$not_empty_context_groups = [];
foreach (Element::children($build) as $region_name) {
foreach ($build[$region_name] as $block) {
if (!empty($block['#configuration']['all_parents'])) {
$not_empty_context_groups = array_unique(array_merge($block['#configuration']['all_parents'], $not_empty_context_groups));
}
}
}
return $not_empty_context_groups;
}
}