diff --git a/page_manager.services.yml b/page_manager.services.yml index 3c2df6c..4d7db13 100644 --- a/page_manager.services.yml +++ b/page_manager.services.yml @@ -9,8 +9,8 @@ services: arguments: ['@router.route_provider', '@request_stack'] tags: - { name: 'event_subscriber' } - page_manager.static_context: - class: Drupal\page_manager\StaticContext + page_manager.context_mapper: + class: Drupal\page_manager\ContextMapper arguments: ['@entity.repository'] page_manager.page_manager_routes: class: Drupal\page_manager\Routing\PageManagerRoutes diff --git a/src/ContextMapper.php b/src/ContextMapper.php new file mode 100644 index 0000000..101bf2f --- /dev/null +++ b/src/ContextMapper.php @@ -0,0 +1,55 @@ +entityRepository = $entity_repository; + } + + /** + * {@inheritdoc} + */ + public function getContextValues(array $context_configurations) { + $contexts = []; + foreach ($context_configurations as $name => $context_configuration) { + $context_definition = new ContextDefinition($context_configuration['type'], $context_configuration['label']); + if (strpos($context_configuration['type'], 'entity:') === 0) { + $context = new EntityLazyLoadContext($context_definition, $this->entityRepository, $context_configuration['value']); + } + else { + $context = new Context($context_definition, $context_configuration['value']); + } + $contexts[$name] = $context; + } + return $contexts; + } + +} diff --git a/src/StaticContextInterface.php b/src/ContextMapperInterface.php similarity index 59% rename from src/StaticContextInterface.php rename to src/ContextMapperInterface.php index 28259e4..9f76cc1 100644 --- a/src/StaticContextInterface.php +++ b/src/ContextMapperInterface.php @@ -2,15 +2,15 @@ /** * @file - * Contains \Drupal\page_manager\StaticContextInterface. + * Contains \Drupal\page_manager\ContextMapperInterface. */ namespace Drupal\page_manager; /** - * Gathers static context. + * Provides an interface for mapping context configurations to context objects. */ -interface StaticContextInterface { +interface ContextMapperInterface { /** * Gathers the static context values. @@ -21,6 +21,6 @@ * @return \Drupal\Component\Plugin\Context\ContextInterface[] * An array of set context values, keyed by context name. */ - public function getStaticContextValues(array $static_context_configurations); + public function getContextValues(array $static_context_configurations); } diff --git a/src/Entity/PageVariant.php b/src/Entity/PageVariant.php index b99f772..bf5d9ee 100644 --- a/src/Entity/PageVariant.php +++ b/src/Entity/PageVariant.php @@ -254,8 +254,11 @@ public function getPage() { * {@inheritdoc} */ public function getContexts() { - $this->contexts = $this->getStaticContextService()->getStaticContextValues($this->getStaticContexts()); - return array_merge($this->getPage()->getContexts(), $this->contexts); + if (!$this->contexts) { + $contexts = $this->getContextMapper()->getContextValues($this->getStaticContexts()); + $this->contexts = array_merge($this->getPage()->getContexts(), $contexts); + } + return $this->contexts; } /** @@ -399,12 +402,12 @@ protected function getConditionManager() { } /** - * @todo Rename. + * Wraps the context mapper. * - * @return \Drupal\page_manager\StaticContextInterface + * @return \Drupal\page_manager\ContextMapperInterface */ - protected function getStaticContextService() { - return \Drupal::service('page_manager.static_context'); + protected function getContextMapper() { + return \Drupal::service('page_manager.context_mapper'); } } diff --git a/src/StaticContext.php b/src/StaticContext.php deleted file mode 100644 index b8f1721..0000000 --- a/src/StaticContext.php +++ /dev/null @@ -1,49 +0,0 @@ -entityRepository = $entity_repository; - } - - /** - * {@inheritdoc} - */ - public function getStaticContextValues(array $static_context_configurations) { - $contexts = []; - foreach ($static_context_configurations as $name => $static_context) { - // @todo Do not always assume these are entities. - $context = new EntityLazyLoadContext(new ContextDefinition($static_context['type'], $static_context['label']), $this->entityRepository, $static_context['value']); - $contexts[$name] = $context; - } - return $contexts; - } - -} diff --git a/tests/src/Unit/ContextMapperTest.php b/tests/src/Unit/ContextMapperTest.php new file mode 100644 index 0000000..fd8067c --- /dev/null +++ b/tests/src/Unit/ContextMapperTest.php @@ -0,0 +1,107 @@ +typedDataManager = $this->prophesize(TypedDataManager::class); + $this->entityRepository = $this->prophesize(EntityRepositoryInterface::class); + $this->staticContext = new ContextMapper($this->entityRepository->reveal()); + + $container = new ContainerBuilder(); + $container->set('typed_data_manager', $this->typedDataManager->reveal()); + \Drupal::setContainer($container); + } + + /** + * @covers ::getContextValues + */ + public function testGetContextValues() { + $input = []; + $actual = $this->staticContext->getContextValues($input); + $this->assertEquals([], $actual); + } + + /** + * @covers ::getContextValues + */ + public function testGetContextValuesContext() { + $data_definition = DataDefinition::createFromDataType('integer'); + $typed_data = IntegerData::createInstance($data_definition); + $this->typedDataManager->createDataDefinition('integer')->willReturn($data_definition); + $this->typedDataManager->getDefaultConstraints($data_definition)->willReturn([]); + $this->typedDataManager->create($data_definition, 5)->willReturn($typed_data); + + $input = [ + 'foo' => [ + 'label' => 'Foo', + 'type' => 'integer', + 'value' => 5, + ], + ]; + $expected = new Context(new ContextDefinition('integer', 'Foo'), 5); + $actual = $this->staticContext->getContextValues($input)['foo']; + $this->assertEquals($expected, $actual); + } + + /** + * @covers ::getContextValues + */ + public function testGetContextValuesEntityContext() { + $input = [ + 'foo' => [ + 'label' => 'Foo', + 'type' => 'entity:node', + 'value' => 'the_node_uuid', + ], + ]; + $expected = new EntityLazyLoadContext(new ContextDefinition('entity:node', 'Foo'), $this->entityRepository->reveal(), 'the_node_uuid'); + $actual = $this->staticContext->getContextValues($input)['foo']; + $this->assertEquals($expected, $actual); + } + +}