Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Annotation / ContextDefinition.php
index 043975295348b6e20592361d52c54fea3f3524dc..9e28d5a6ed696914077becd33cc1c65ba7b60bac 100644 (file)
@@ -116,10 +116,36 @@ class ContextDefinition extends Plugin {
     if (isset($values['class']) && !in_array('Drupal\Core\Plugin\Context\ContextDefinitionInterface', class_implements($values['class']))) {
       throw new \Exception('ContextDefinition class must implement \Drupal\Core\Plugin\Context\ContextDefinitionInterface.');
     }
-    $class = isset($values['class']) ? $values['class'] : 'Drupal\Core\Plugin\Context\ContextDefinition';
+
+    $class = $this->getDefinitionClass($values);
     $this->definition = new $class($values['value'], $values['label'], $values['required'], $values['multiple'], $values['description'], $values['default_value']);
   }
 
+  /**
+   * Determines the context definition class to use.
+   *
+   * If the annotation specifies a specific context definition class, we use
+   * that. Otherwise, we use \Drupal\Core\Plugin\Context\EntityContextDefinition
+   * if the data type starts with 'entity:', since it contains specialized logic
+   * specific to entities. Otherwise, we fall back to the generic
+   * \Drupal\Core\Plugin\Context\ContextDefinition class.
+   *
+   * @param array $values
+   *   The annotation values.
+   *
+   * @return string
+   *   The fully-qualified name of the context definition class.
+   */
+  protected function getDefinitionClass(array $values) {
+    if (isset($values['class'])) {
+      return $values['class'];
+    }
+    if (strpos($values['value'], 'entity:') === 0) {
+      return 'Drupal\Core\Plugin\Context\EntityContextDefinition';
+    }
+    return 'Drupal\Core\Plugin\Context\ContextDefinition';
+  }
+
   /**
    * Returns the value of an annotation.
    *