Skip to content

Commit

Permalink
additional breadcrumb and blocks support in twig renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Jun 27, 2012
1 parent cdf3f13 commit a2e4a2c
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 74 deletions.
6 changes: 0 additions & 6 deletions Menu/MenuFactory.php
Expand Up @@ -229,10 +229,6 @@ public function createItem($name, array $options = array(), array & $itemList =

$routeParameters = array_merge($options['routeParameters'], $this->defaultrouteParameters, $this->routeParameters[$name]);

if (!$routeParameters && isset($options['routeParameters'])) {
$routeParameters = $options['routeParameters'];
}

$itemOids = array();

if (!empty($options['secureParams'])) {
Expand All @@ -250,8 +246,6 @@ public function createItem($name, array $options = array(), array & $itemList =
}
}



$extra = array(
'type' => $options['type'],
'translateDomain' => $options['translateDomain'],
Expand Down
32 changes: 0 additions & 32 deletions Menu/MenuItem.php
Expand Up @@ -34,36 +34,4 @@ public function addChild($child, array $options = array())

return $child;
}

/**
* {@inheritdoc}
* @param null $subItem
* @return array
*/
public function getBreadcrumbsArray($subItem = null)
{
$breadcrumbs = array();
$obj = $this;

if ($subItem) {
if (!is_array($subItem)) {
$subItem = array((string) $subItem => null);
}
$subItem = array_reverse($subItem);
foreach ($subItem as $key => $value) {
if (is_numeric($key)) {
$key = $value;
$value = null;
}
$breadcrumbs[(string) $key] = $value;
}
}

do {
$name = $obj->getName();
$breadcrumbs[$name] = $obj;
} while ($obj = $obj->getParent());

return array_reverse($breadcrumbs, true);
}
}
1 change: 0 additions & 1 deletion Menu/MenuOptions.php
Expand Up @@ -11,7 +11,6 @@ class MenuOptions
public function __construct(array $options)
{
$this->options = $options;

}

/**
Expand Down
66 changes: 66 additions & 0 deletions Menu/Renderer/BreadcrumbTwigRenderer.php
@@ -0,0 +1,66 @@
<?php
namespace Millwright\MenuBundle\Menu\Renderer;

use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\MatcherInterface;

/**
* Millwright menu twig renderer
*/
class BreadcrumbTwigRenderer extends TwigRenderer
{
/**
* @param \Twig_Environment $environment
* @param string $template
* @param MatcherInterface $matcher
* @param array $defaultOptions
*/
public function __construct(
\Twig_Environment $environment,
$template,
MatcherInterface $matcher,
array $defaultOptions = array())
{
$defaultOptions = array_merge(array(
'block' => 'breadcrumb'
), $defaultOptions);
parent::__construct($environment, $template, $matcher, $defaultOptions);
}

/**
* {@inheritDoc}
*/
protected function getData(ItemInterface $item)
{
$currentItem = $this->getCurrentItem($item->getRoot());
$data = $currentItem->getBreadcrumbsArray();
if(isset($data[0]) && empty($data[0]['uri'])) {
array_shift($data);
}

return $data;
}

/**
* Get current item
*
* @param ItemInterface $item
*
* @return ItemInterface|null
*/
private function getCurrentItem(ItemInterface $item)
{
foreach ($item->getChildren() as $child) {
if ($this->matcher->isCurrent($child)) {
return $child;
} else {
$child = $this->getCurrentItem($child);
if ($child) {
return $child;
}
}
}

return null;
}
}
49 changes: 19 additions & 30 deletions Menu/Renderer/TwigRenderer.php
Expand Up @@ -10,25 +10,26 @@
*/
class TwigRenderer implements RendererInterface
{
/**
* @var \Twig_Environment
*/
private $environment;
private $defaultOptions;
private $matcher;
protected $environment;
protected $defaultOptions;
protected $matcher;

/**
* @param \Twig_Environment $environment
* @param string $template
* @param MatcherInterface $matcher
* @param array $defaultOptions
*/
public function __construct(\Twig_Environment $environment, $template, MatcherInterface $matcher,
public function __construct(
\Twig_Environment $environment,
$template,
MatcherInterface $matcher,
array $defaultOptions = array())
{
$this->environment = $environment;
$this->matcher = $matcher;
$this->defaultOptions = array_merge(array(
'block' => 'root',
'depth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
Expand Down Expand Up @@ -58,15 +59,13 @@ public function render(ItemInterface $item, array $options = array())
$template = $this->environment->loadTemplate($template);
}

$block = isset($options['block']) ? $options['block'] : 'root';
//@todo remove breadcrumb into config
if ($block == 'breadcrumb') {
$data = $this->getCurrentItem($item->getRoot())->getBreadcrumbsArray();
} else {
$data = $item;
}
$data = $this->getData($item);

$html = $template->renderBlock($block, array('item' => $data, 'options' => $options, 'matcher' => $this->matcher));
$html = $template->renderBlock($options['block'], array(
'item' => $data,
'options' => $options,
'matcher' => $this->matcher
));

if (!empty($options['clear_matcher'])) {
$this->matcher->clear();
Expand All @@ -76,25 +75,15 @@ public function render(ItemInterface $item, array $options = array())
}

/**
* Get current item
* Get data for template by menu item
* used for breadcrumb
*
* @param ItemInterface $item
*
* @return ItemInterface|null
* @return \Knp\Menu\ItemInterface
*/
protected function getCurrentItem(ItemInterface $item)
protected function getData(ItemInterface $item)
{
foreach ($item->getChildren() as $child) {
if ($this->matcher->isCurrent($child)) {
return $child;
} else {
$child = $this->getCurrentItem($child);
if ($child) {
return $child;
}
}
}

return null;
return $item;
}
}
12 changes: 11 additions & 1 deletion Resources/config/services.xml
Expand Up @@ -11,8 +11,9 @@

<parameter key="millwright_menu.helper.class">Millwright\MenuBundle\Twig\Helper</parameter>
<parameter key="millwright_menu.twig.extension.class">Millwright\MenuBundle\Twig\MenuExtension</parameter>
<parameter key="millwright_menu.renderer.twig.template">MillwrightMenuBundle:Default:knp_menu.html.twig</parameter>
<parameter key="millwright_menu.renderer.twig.template">%knp_menu.renderer.twig.template%</parameter>
<parameter key="millwright_menu.renderer.twig.class">Millwright\MenuBundle\Menu\Renderer\TwigRenderer</parameter>
<parameter key="millwright_menu.renderer.twig.breadcrumb.class">Millwright\MenuBundle\Menu\Renderer\BreadcrumbTwigRenderer</parameter>
<parameter key="millwright_menu.templating.helper.class">Millwright\MenuBundle\Templating\Helper\MenuHelper</parameter>

<parameter key="millwright_menu.options.class">Millwright\MenuBundle\Menu\MenuOptions</parameter>
Expand Down Expand Up @@ -46,6 +47,15 @@
<argument type="service" id="knp_menu.matcher" />
<argument>%knp_menu.renderer.twig.options%</argument>
</service>

<service id="millwright_menu.renderer.breadcrumb.twig" class="%millwright_menu.renderer.twig.breadcrumb.class%">
<tag name="knp_menu.renderer" alias="millwright_breadcrumb_renderer" />
<argument type="service" id="twig" />
<argument>%millwright_menu.renderer.twig.template%</argument>
<argument type="service" id="knp_menu.matcher" />
<argument>%knp_menu.renderer.twig.options%</argument>
</service>

<service id="millwright_menu.factory" class="%millwright_menu.factory.class%">
<argument type="service" id="router" />
<argument type="service" id="security.context" />
Expand Down
6 changes: 2 additions & 4 deletions Resources/views/Default/knp_menu.html.twig
Expand Up @@ -7,15 +7,13 @@
{% block breadcrumb %}
<ul class="breadcrumb">
{% for element in item %}
<li>
{{ block('breadcrumb_element') }}
</li>
<li>{{ block('breadcrumb_element') }}</li>
{% endfor %}
</ul>
{% endblock breadcrumb %}

{% block breadcrumb_element %}
{% set item = element %}
{% set item = element.item %}
{%- if item.uri is not empty and (not matcher.isCurrent(item)) %}
{{ block('linkElement') }}
<span class="divider">/</span>
Expand Down

0 comments on commit a2e4a2c

Please sign in to comment.