Skip to content

Commit

Permalink
render badge from menu item extras
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilkka Pale committed Mar 23, 2016
1 parent d46e2ac commit 998baa9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Factory/FlatMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function buildOptions(array $options) {
public function buildItem(ItemInterface $item, array $options) {
// Set extra parameters

$params = ['icon', 'rate_rights', 'headline', 'hide_navbar'];
$params = ['icon', 'rate_rights', 'headline', 'hide_navbar', 'badge'];

foreach($params as $param) {
if(isset($options[$param])) {
Expand All @@ -45,4 +45,4 @@ public function buildItem(ItemInterface $item, array $options) {
}
}

}
}
4 changes: 1 addition & 3 deletions src/FlatMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ public function addChild($child, array $options = array()) {

return $child;
}


}
}
37 changes: 23 additions & 14 deletions src/Renderer/FlatMenuRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ public function render(ItemInterface $item, array $options = array()) {
public function renderBacklink(ItemInterface $item, $options = array()) {
$options = array_merge($this->defaultOptions, $options);

if(empty($options['headline'])) {
if (empty($options['headline'])) {
return null;
}


if(!empty($options['label'])) {
if (!empty($options['label'])) {
$item->getParent()->setLabel($options['label']);
}

if(!empty($options['icon'])) {
if (!empty($options['icon'])) {
$item->getParent()->setExtra('ext:icon', $options['icon']);
}

if(!empty($options['headline'])) {
if (!empty($options['headline'])) {
$item->setExtra('ext:headline', $options['headline']);
}

Expand All @@ -93,7 +92,8 @@ public function renderBacklink(ItemInterface $item, $options = array()) {

protected function getExtendedOptions(ItemInterface $item) {
$options = [];
foreach ($item->getExtras() as $key => $value) {
$extras = $item->getExtras();
foreach ($extras as $key => $value) {
if (substr($key, 0, 4) === 'ext:') {
$options[substr($key, 4)] = $value;
}
Expand Down Expand Up @@ -286,23 +286,32 @@ protected function renderSpanElement(ItemInterface $item, array $options) {
}

protected function renderLabel(ItemInterface $item, array $options) {
$icon = '';
$extendedOtions = $this->getExtendedOptions($item);
$label = '';
$extendedOptions = $this->getExtendedOptions($item);
// icon
if (!empty($extendedOtions['icon'])) {
$icon = $this->format(
'<img src="' . $options['icon_base_path'] . $extendedOtions['icon'] . $options['icon_ext'] . '" />',
if (!empty($extendedOptions['icon'])) {
$label .= $this->format(
'<img src="' . $options['icon_base_path'] . $extendedOptions['icon'] . $options['icon_ext'] . '" />',
'img',
$item->getLevel(),
$options
);
}

if (!empty($extendedOptions['badge'])) {
$label .= sprintf(' <span style="margin-left:10px" class="label label-important pull-right">%s</span>',
$this->escape($extendedOptions['badge'])
);
}

if ($options['allow_safe_labels'] && $item->getExtra('safe_label', false)) {
return $icon . $item->getLabel();
$label .= $item->getLabel();
} else {
$label .= $this->escape($item->getLabel());
}

return $icon . $this->escape($item->getLabel());

return $label;
}

/**
Expand Down Expand Up @@ -336,4 +345,4 @@ protected function format($html, $type, $level, array $options) {

return str_repeat(' ', $spacing) . $html . "\n";
}
}
}

0 comments on commit 998baa9

Please sign in to comment.