BlockCollection.php in Context 8
File
src/Reaction/Blocks/BlockCollection.php
View source
<?php
namespace Drupal\context\Reaction\Blocks;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
class BlockCollection extends DefaultLazyPluginCollection {
public function &get($instance_id) {
return parent::get($instance_id);
}
public function getAllByRegion($theme) {
$region_assignments = [];
foreach ($this as $block_id => $block) {
$configuration = $block
->getConfiguration();
if ($configuration['theme'] !== $theme) {
continue;
}
$region = isset($configuration['region']) ? $configuration['region'] : NULL;
$region_assignments[$region][$block_id] = $block;
}
foreach ($region_assignments as $region => $region_assignment) {
@uasort($region_assignment, function (BlockPluginInterface $a, BlockPluginInterface $b) {
$a_config = $a
->getConfiguration();
$a_weight = isset($a_config['weight']) ? $a_config['weight'] : 0;
$b_config = $b
->getConfiguration();
$b_weight = isset($b_config['weight']) ? $b_config['weight'] : 0;
if ($a_weight == $b_weight) {
return strcmp($a
->label(), $b
->label());
}
return $a_weight > $b_weight ? 1 : -1;
});
$region_assignments[$region] = $region_assignment;
}
return $region_assignments;
}
}