Created
May 20, 2014 04:36
-
-
Save EclipseGc/fbf56101c18530869e10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/block_page.services.yml b/block_page.services.yml | |
index 8df43da..da46708 100644 | |
--- a/block_page.services.yml | |
+++ b/block_page.services.yml | |
@@ -12,3 +12,6 @@ services: | |
plugin.manager.page_variant: | |
class: Drupal\block_page\Plugin\PageVariantManager | |
parent: default_plugin_manager | |
+ context.handler: | |
+ class: Drupal\block_page\ContextHandler | |
+ arguments: ['@typed_data_manager'] | |
diff --git a/src/ContextHandler.php b/src/ContextHandler.php | |
new file mode 100644 | |
index 0000000..3b04290 | |
--- /dev/null | |
+++ b/src/ContextHandler.php | |
@@ -0,0 +1,109 @@ | |
+<?php | |
+/** | |
+* @file | |
+* Contains \Drupal\block_page\ContextHandler.php. | |
+*/ | |
+ | |
+namespace Drupal\block_page; | |
+ | |
+use Drupal\Component\Plugin\PluginManagerInterface; | |
+use Drupal\Core\TypedData\DataDefinitionInterface; | |
+use Drupal\Core\TypedData\DataDefinition; | |
+use Drupal\Core\TypedData\TypedDataManager; | |
+ | |
+class ContextHandler { | |
+ | |
+ /** | |
+ * @var \Drupal\Core\TypedData\TypedDataManager | |
+ */ | |
+ protected $typed_data; | |
+ | |
+ public function __construct(TypedDataManager $typed_data) { | |
+ $this->typed_data = $typed_data; | |
+ } | |
+ | |
+ public function checkRequirements(array $contexts, array $requirements) { | |
+ $results = array(); | |
+ foreach ($requirements as $name => $requirement) { | |
+ if (!$requirement instanceof DataDefinitionInterface) { | |
+ // @todo make a custom exception class. | |
+ throw new \Exception(sprintf('The %s requirement must be an instance of DataDefinitionInterface.', $name)); | |
+ } | |
+ if ($requirement->isRequired()) { | |
+ foreach ($contexts as $context_object) { | |
+ $context = $context_object->getContextDefinition(); | |
+ if (!$context instanceof DataDefinitionInterface) { | |
+ // @todo make a custom exception class. | |
+ throw new \Exception('An illegal context was passed, all contexts must be an instance of DataDefinitionInterface.'); | |
+ } | |
+ if ($requirement->getDataType() == $context->getDataType()) { | |
+ foreach ($requirement->getConstraints() as $constraint_name => $constraint) { | |
+ if ($context->getConstraint($constraint_name) != $constraint) { | |
+ continue 2; | |
+ } | |
+ } | |
+ } | |
+ $results[$name] = TRUE; | |
+ } | |
+ } | |
+ else { | |
+ $results[$name] = TRUE; | |
+ } | |
+ } | |
+ return count($requirements) == count($results); | |
+ } | |
+ | |
+ public function getAvailablePlugins(array $contexts, PluginManagerInterface $manager) { | |
+ $plugins = $manager->getDefinitions(); | |
+ $available_plugins = array(); | |
+ foreach ($plugins as $plugin_id => $plugin) { | |
+ if (isset($plugin['context'])) { | |
+ $plugin_contexts = $plugin['context']; | |
+ $requirements = array(); | |
+ foreach ($plugin_contexts as $context_id => $plugin_context) { | |
+ $definition = $this->typed_data->getDefinition($plugin_context['type']); | |
+ $definition['type'] = $plugin_context['type']; | |
+ if (isset($plugin_context['constraints'])) { | |
+ if (!isset($definition['constraints'])) { | |
+ $definition['constraints'] = $plugin_context['constraints']; | |
+ } | |
+ else { | |
+ $definition['constraints'] += $plugin_context['constraints']; | |
+ } | |
+ } | |
+ if (!isset($definition['required'])) { | |
+ $definition['required'] = TRUE; | |
+ } | |
+ $requirements[$context_id] = new DataDefinition($definition); | |
+ } | |
+ if ($this->checkRequirements($contexts, $requirements)) { | |
+ $available_plugins[$plugin_id] = $plugin; | |
+ } | |
+ } | |
+ else { | |
+ $available_plugins[$plugin_id] = $plugin; | |
+ } | |
+ } | |
+ return $available_plugins; | |
+ } | |
+ | |
+ public function getValidContexts(array $contexts, DataDefinitionInterface $definition) { | |
+ $valid = array(); | |
+ foreach ($contexts as $id => $context) { | |
+ if (!$context instanceof DataDefinitionInterface) { | |
+ // @todo make a custom exception class. | |
+ throw new \Exception('An illegal context was passed, all contexts must be an instance of DataDefinitionInterface.'); | |
+ } | |
+ if ($definition->getDataType() == $context->getDataType()) { | |
+ foreach ($definition->getConstraints() as $constraint_name => $constraint) { | |
+ if ($context->getConstraint($constraint_name) != $constraint) { | |
+ continue 2; | |
+ } | |
+ } | |
+ } | |
+ $valid[$id] = $context; | |
+ } | |
+ return $valid; | |
+ } | |
+ | |
+} | |
diff --git a/src/Controller/BlockPageController.php b/src/Controller/BlockPageController.php | |
index 072b458..7548911 100644 | |
--- a/src/Controller/BlockPageController.php | |
+++ b/src/Controller/BlockPageController.php | |
@@ -95,7 +95,9 @@ class BlockPageController extends ControllerBase { | |
'#links' => array(), | |
); | |
$condition_manager = \Drupal::service('plugin.manager.condition'); | |
- foreach ($condition_manager->getDefinitions() as $access_id => $access_condition) { | |
+ $context_handler = \Drupal::service('context.handler'); | |
+ $available_plugins = $context_handler->getAvailablePlugins($block_page->getContextValues(), $condition_manager); | |
+ foreach ($available_plugins as $access_id => $access_condition) { | |
$build['#links'][$access_id] = array( | |
'title' => $access_condition['label'], | |
'route_name' => 'block_page.access_condition_add', | |
diff --git a/src/EventSubscriber/CurrentUserContext.php b/src/EventSubscriber/CurrentUserContext.php | |
index 26db991..cec3773 100644 | |
--- a/src/EventSubscriber/CurrentUserContext.php | |
+++ b/src/EventSubscriber/CurrentUserContext.php | |
@@ -9,6 +9,7 @@ namespace Drupal\block_page\EventSubscriber; | |
use Drupal\block_page\Event\BlockPageContextEvent; | |
use Drupal\Core\Entity\EntityManagerInterface; | |
+use Drupal\Core\Plugin\Context\Context; | |
use Drupal\Core\Session\AccountProxyInterface; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
@@ -52,7 +53,9 @@ class CurrentUserContext implements EventSubscriberInterface { | |
*/ | |
public function onBlockPageContext(BlockPageContextEvent $event) { | |
$current_user = $this->userStorage->load($this->accountProxy->getAccount()->id()); | |
- $event->getBlockPage()->setContextValue('current_user', $current_user); | |
+ $context = new Context(['type' => 'entity', 'constraints' => ['EntityType' => 'user']]); | |
+ $context->setContextValue($current_user); | |
+ $event->getBlockPage()->setContextValue('current_user', $context); | |
} | |
/** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment