Version 1
[yaffs-website] / web / core / modules / system / tests / modules / condition_test / src / Plugin / Condition / OptionalContextCondition.php
diff --git a/web/core/modules/system/tests/modules/condition_test/src/Plugin/Condition/OptionalContextCondition.php b/web/core/modules/system/tests/modules/condition_test/src/Plugin/Condition/OptionalContextCondition.php
new file mode 100644 (file)
index 0000000..061261c
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace Drupal\condition_test\Plugin\Condition;
+
+use Drupal\Core\Condition\ConditionPluginBase;
+
+/**
+ * Provides a condition with an optional node context.
+ *
+ * The context type entity:node is used since that would allow to also use this
+ * for web tests with the node route context.
+ *
+ * @Condition(
+ *   id = "condition_test_optional_context",
+ *   label = @Translation("Optional context"),
+ *   context = {
+ *     "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = FALSE),
+ *   }
+ * )
+ */
+class OptionalContextCondition extends ConditionPluginBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function evaluate() {
+    // Grant access if no context value is given.
+    return !$this->getContextValue('node');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function summary() {
+    return $this->t('Context with optional context.');
+  }
+
+}