diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index caf176d9..6ddc87db 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -13,13 +13,13 @@ use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; +use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter; class Configuration implements ConfigurationInterface { public function getConfigTreeBuilder() { - $cmfRoutingAvailable = interface_exists('Symfony\Cmf\Component\Routing\RouteObjectInterface'); + $cmfRoutingAvailable = class_exists('Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter'); $treeBuilder = new TreeBuilder(); $treeBuilder->root('cmf_menu') @@ -53,8 +53,8 @@ public function getConfigTreeBuilder() ->scalarNode('content_url_generator')->defaultValue('router')->end() ->booleanNode('allow_empty_items')->defaultFalse()->end() - ->scalarNode('content_key')->defaultValue($cmfRoutingAvailable ? RouteObjectInterface::CONTENT_OBJECT : '')->end() - ->scalarNode('route_name_key')->defaultValue($cmfRoutingAvailable ? RouteObjectInterface::ROUTE_NAME : '')->end() + ->scalarNode('content_key')->defaultValue($cmfRoutingAvailable ? DynamicRouter::CONTENT_KEY : '')->end() + ->scalarNode('route_name_key')->defaultValue($cmfRoutingAvailable ? DynamicRouter::ROUTE_KEY : '')->end() ->arrayNode('voters') ->children() diff --git a/Templating/MenuHelper.php b/Templating/MenuHelper.php index 962a7b72..5a68d979 100644 --- a/Templating/MenuHelper.php +++ b/Templating/MenuHelper.php @@ -18,7 +18,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Templating\Helper\Helper; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; +use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter; use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNode; use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface; @@ -51,7 +51,7 @@ class MenuHelper extends Helper * @param string $routeNameKey The name of the request attribute holding * the name of the current route */ - public function __construct(ManagerRegistry $managerRegistry, FactoryInterface $menuFactory, $contentObjectKey = RouteObjectInterface::CONTENT_OBJECT, $routeNameKey = RouteObjectInterface::ROUTE_NAME) + public function __construct(ManagerRegistry $managerRegistry, FactoryInterface $menuFactory, $contentObjectKey = DynamicRouter::CONTENT_KEY, $routeNameKey = DynamicRouter::ROUTE_KEY) { $this->managerRegistry = $managerRegistry; $this->menuFactory = $menuFactory; @@ -79,7 +79,7 @@ public function setManagerName($managerName) * * @return array An array with breadcrumb items (each item has the following keys: label, uri, item) */ - public function getBreadcrumbArray(NodeInterface $node, $includeMenuRoot = true) + public function getBreadcrumbsArray(NodeInterface $node, $includeMenuRoot = true) { $item = $this->menuFactory->createItem($node->getName(), $node->getOptions()); @@ -97,7 +97,7 @@ public function getBreadcrumbArray(NodeInterface $node, $includeMenuRoot = true) return $includeMenuRoot ? $breadcrumbs : array(); } - return array_merge($this->getBreadcrumbArray($parent, $includeMenuRoot), $breadcrumbs); + return array_merge($this->getBreadcrumbsArray($parent, $includeMenuRoot), $breadcrumbs); } /** @@ -127,8 +127,8 @@ public function getCurrentItem(Request $request) * It uses some special Request attributes that are managed by * the CmfRoutingBundle: * - * * RouteObjectInterface::CONTENT_OBJECT to match a menu node by the refering content - * * RouteObjectInterface::ROUTE_NAME to match a menu node by the refering route name + * * DynamicRouter::CONTENT_KEY to match a menu node by the refering content + * * DynamicRouter::ROUTE_KEY to match a menu node by the refering route name * * @return NodeInterface|null */ @@ -141,7 +141,11 @@ public function getCurrentNode(Request $request) $content = $request->attributes->get($this->contentObjectKey); if ($content instanceof MenuNodeReferrersInterface) { - return $this->filterByLinkType(new ArrayCollection($content->getMenuNodes()), 'content'); + $node = $this->filterByLinkType(new ArrayCollection($content->getMenuNodes()), 'content'); + + if ($node) { + return $node; + } } } diff --git a/Tests/Functional/Templating/MenuHelperTest.php b/Tests/Functional/Templating/MenuHelperTest.php index 632316e5..2029cb4c 100644 --- a/Tests/Functional/Templating/MenuHelperTest.php +++ b/Tests/Functional/Templating/MenuHelperTest.php @@ -13,7 +13,7 @@ use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface; use Symfony\Cmf\Bundle\MenuBundle\Templating\MenuHelper; -use Symfony\Cmf\Component\Routing\RouteObjectInterface; +use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter; use Symfony\Cmf\Component\Testing\Functional\BaseTestCase; use Knp\Menu\NodeInterface; @@ -32,13 +32,13 @@ protected function setUp() } /** - * @dataProvider provideGetBreadcrumbArrayData + * @dataProvider provideGetBreadcrumbsArrayData */ - public function testGetBreadcrumbArray($includeMenuRoot) + public function testGetBreadcrumbsArray($includeMenuRoot) { $currentNode = $this->db('PHPCR')->getOm()->find(null, '/test/menus/test-menu/item-2/sub-item-2'); - $breadcrumbs = $this->helper->getBreadcrumbArray($currentNode, $includeMenuRoot); + $breadcrumbs = $this->helper->getBreadcrumbsArray($currentNode, $includeMenuRoot); // simplify the returned breadcrumb array $breadcrumbs = array_map(function ($breadcrumb) { @@ -56,7 +56,7 @@ public function testGetBreadcrumbArray($includeMenuRoot) $this->assertEquals($expectedBreadcrumbs, $breadcrumbs); } - public function provideGetBreadcrumbArrayData() + public function provideGetBreadcrumbsArrayData() { return array('menu root included' => array(true), 'menu route excluded' => array(false)); } @@ -67,9 +67,9 @@ public function provideGetBreadcrumbArrayData() public function testGetCurrentNodeWithRoute($routeName, $nodeName) { $attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag'); - $attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(false); - $attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(true); - $attributes->get(RouteObjectInterface::ROUTE_NAME)->willReturn($routeName); + $attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(false); + $attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(true); + $attributes->get(DynamicRouter::ROUTE_KEY)->willReturn($routeName); $request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); $request->attributes = $attributes->reveal(); @@ -93,9 +93,9 @@ public function testGetCurrentNodeWithContent() $content->addMenuNode($this->db('PHPCR')->getOm()->find(null, '/test/menus/test-menu/item-1')); $attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag'); - $attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(true); - $attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(true); - $attributes->get(RouteObjectInterface::CONTENT_OBJECT)->willReturn($content); + $attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(true); + $attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(true); + $attributes->get(DynamicRouter::CONTENT_KEY)->willReturn($content); $request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); $request->attributes = $attributes->reveal(); @@ -108,8 +108,8 @@ public function testGetCurrentNodeWithContent() public function testGetCurrentNodeWithoutMatch() { $attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag'); - $attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(false); - $attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(false); + $attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(false); + $attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(false); $request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); $request->attributes = $attributes->reveal(); @@ -120,9 +120,9 @@ public function testGetCurrentNodeWithoutMatch() public function testGetCurrentItemWithMatch() { $attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag'); - $attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(false); - $attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(true); - $attributes->get(RouteObjectInterface::ROUTE_NAME)->willReturn('link_test_route_with_params'); + $attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(false); + $attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(true); + $attributes->get(DynamicRouter::ROUTE_KEY)->willReturn('link_test_route_with_params'); $request = $this->prophesize('Symfony\Component\HttpFoundation\Request'); $request->attributes = $attributes->reveal(); diff --git a/Twig/MenuExtension.php b/Twig/MenuExtension.php index 85a9b5f1..919d1688 100644 --- a/Twig/MenuExtension.php +++ b/Twig/MenuExtension.php @@ -33,6 +33,7 @@ public function getFunctions() return array( new \Twig_SimpleFunction('cmf_menu_get_breadcrumbs_array', array($this->helper, 'getBreadcrumbsArray')), new \Twig_SimpleFunction('cmf_menu_get_current_item', array($this->helper, 'getCurrentItem')), + new \Twig_SimpleFunction('cmf_menu_get_current_node', array($this->helper, 'getCurrentNode')), ); }