You are here

public function BlockCollection::getAllByRegion in Context 8

Same name and namespace in other branches
  1. 8.4 src/Reaction/Blocks/BlockCollection.php \Drupal\context\Reaction\Blocks\BlockCollection::getAllByRegion()
  2. 8.0 src/Reaction/Blocks/BlockCollection.php \Drupal\context\Reaction\Blocks\BlockCollection::getAllByRegion()

Returns all blocks keyed by their region. Base code from the ctools block plugin collection.

Parameters

string $theme: The theme to get blocks for.

Return value

BlockPluginInterface[] An associative array keyed by region, containing an associative array of block plugins.

File

src/Reaction/Blocks/BlockCollection.php, line 30

Class

BlockCollection

Namespace

Drupal\context\Reaction\Blocks

Code

public function getAllByRegion($theme) {
  $region_assignments = [];

  /** @var BlockPluginInterface[] $this */
  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) {

    // @todo Determine the reason this needs error suppression.
    @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;
}