From a89588756f87d4c246736b4c8fa7705fbd896e67 Mon Sep 17 00:00:00 2001
From: Paul Maddern <pobster@25159.no-reply.drupal.org>
Date: Wed, 28 Feb 2018 19:10:33 +0000
Subject: [PATCH] Issue #2948830 by pobster: Provide context any condition.

---
 src/Plugin/Condition/ContextAny.php | 122 ++++++++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 src/Plugin/Condition/ContextAny.php

diff --git a/src/Plugin/Condition/ContextAny.php b/src/Plugin/Condition/ContextAny.php
new file mode 100644
index 0000000..c458337
--- /dev/null
+++ b/src/Plugin/Condition/ContextAny.php
@@ -0,0 +1,122 @@
+<?php
+namespace Drupal\context\Plugin\Condition;
+
+use Drupal\context\ContextManager;
+use Drupal\Core\Condition\ConditionPluginBase;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Provides a 'Context (any)' condition.
+ *
+ * @Condition(
+ *   id = "context",
+ *   label = @Translation("Context (any)"),
+ *   context = {
+ *     "context" = @ContextDefinition("context", label = @Translation("Context (any)"))
+ *   }
+ * )
+ */
+class ContextAny extends ConditionPluginBase implements ContainerFactoryPluginInterface {
+
+  /**
+   * @var \Drupal\context\ContextManager
+   */
+  private $contextManager;
+
+  /**
+   * Constructs a ContextAny condition plugin.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin_id for the plugin instance.
+   * @param array $plugin_definition
+   *   The plugin implementation definition.
+   * @param \Drupal\context\ContextManager $context_manager
+   *   A context manager for checking the current active contexts.
+   */
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextManager $context_manager) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->contextManager = $context_manager;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      $container->get('context.manager'));
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function defaultConfiguration() {
+    return ['values' => ''] + parent::defaultConfiguration();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+    $form = parent::buildConfigurationForm($form, $form_state);
+    unset($form['negate']);
+    $form['values'] = [
+      '#type' => 'textarea',
+      '#title' => $this->t('Context (any)'),
+      '#description' => $this->t('Set this context on the basis of other active contexts. Put each context on a separate line. The condition will pass if <em>any</em> of the contexts are active. You can use the <code>*</code> character (asterisk) as a wildcard and the <code>~</code> character (tilde) to prevent this context from activating if the listed context is active. Other contexts which use context conditions can not be used to exclude this context from activating.'),
+      '#default_value' => $this->configuration['values'],
+    ];
+    return $form;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
+    $this->configuration['values'] = $form_state->getValue('values');
+    parent::submitConfigurationForm($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function summary() {
+    $contexts = array_map('trim', explode("\n", $this->configuration['values']));
+    $contexts = implode(', ', $contexts);
+    return $this->t('Return true on the basis of other active contexts: @contexts', ['@contexts' => $contexts]);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function evaluate() {
+    $contexts = $negated_contexts = [];
+
+    $values = array_map('trim', explode("\n", $this->configuration['values']));
+    foreach ($values as $key) {
+      if (substr($key, 0, 1) == "~") {
+        $negated_contexts[] = substr($key, 1);
+      }
+      elseif (!empty($key)) {
+        $contexts[] = $key;
+      }
+    }
+
+    foreach ($this->contextManager->getActiveContexts() as $context) {
+      if (in_array($context->getName(), $contexts)) {
+        return TRUE;
+      }
+      elseif (!in_array($context->getName(), $negated_contexts)) {
+        return TRUE;
+      }
+    }
+    return FALSE;
+  }
+
+}
-- 
2.14.3 (Apple Git-98)

