You are here

public function ContextManager::getContextsByGroup in Context 8.0

Same name and namespace in other branches
  1. 8.4 src/ContextManager.php \Drupal\context\ContextManager::getContextsByGroup()
  2. 8 src/ContextManager.php \Drupal\context\ContextManager::getContextsByGroup()

Get all contexts sorted by their group and sorted by their weight inside of each group.

Return value

array

File

src/ContextManager.php, line 118

Class

ContextManager
This is the manager service for the context module and should not be confused with the built in contexts in Drupal.

Namespace

Drupal\context

Code

public function getContextsByGroup() {
  $contexts = $this
    ->getContexts();
  $groups = [];

  // Sort the contexts by their weight before grouping them.
  uasort($contexts, [
    $this,
    'sortContextsByWeight',
  ]);

  // Add each context to their respective groups.
  foreach ($contexts as $context_id => $context) {
    $group = $context
      ->getGroup();
    if ($group === Context::CONTEXT_GROUP_NONE) {
      $group = 'not_grouped';
    }
    $groups[$group][$context_id] = $context;
  }
  return $groups;
}