You are here

public function ContextManager::getActiveReactions in Context 8.4

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

Get all active reactions or reactions of a certain type.

Parameters

string $reactionType: Either the reaction class name or the id of the reaction type to get.

Return value

\Drupal\context\Entity\ContextReactionInterface[] An array with all active reactions or reactions of a certain type.

File

src/ContextManager.php, line 270

Class

ContextManager
This is the manager service for the context module.

Namespace

Drupal\context

Code

public function getActiveReactions($reactionType = NULL) {
  $reactions = [];
  foreach ($this
    ->getActiveContexts() as $context) {

    // If no reaction type has been specified then add all reactions and
    // continue to the next context.
    if (is_null($reactionType)) {
      foreach ($context
        ->getReactions() as $reaction) {

        // Only return block reaction if there is a block applied to
        // the current theme.
        if ($reaction instanceof Blocks) {
          $blocks = $reaction
            ->getBlocks();
          $current_theme = $this
            ->getCurrentTheme();
          foreach ($blocks as $block) {
            if (isset($block
              ->getConfiguration()['theme']) && $block
              ->getConfiguration()['theme'] == $current_theme) {
              $reactions[] = $reaction;
              break;
            }
          }
          if (empty($blocks
            ->getConfiguration())) {
            $reactions[] = $reaction;
          }
        }
        else {
          $reactions[] = $reaction;
        }
      }
      continue;
    }
    $contextReactions = $context
      ->getReactions();

    // Filter the reactions based on the reaction type.
    foreach ($contextReactions as $reaction) {
      if (class_exists($reactionType) && $reaction instanceof $reactionType) {
        $reactions[] = $reaction;
        continue;
      }
      if ($reaction
        ->getPluginId() === $reactionType) {
        $reactions[] = $reaction;
        continue;
      }
    }
  }
  return $reactions;
}