2008-12-29

Symfony 1.2 & menu generation

If you need menu subsystem in your symfony application you can:
- make hardcore in template & layout (-);
- write own subsystem (+);
- or use plugin :)

Previously - in Symfony v.1.0 I have used sfMenuGeneratorPlugin.
As for me - it's a good plugin - I like it, but ...
- last release at 07/02/2008;
- it's compatible with symfony 1.0 (we have a newer - 1.2.1 );
- it depends on prototype.js (I like jQuery :)
- unfortunately - it doesn't support i18n.

So - let's use it - as we need.
1. Download plugin.
2. unpack it and put MenuGeneratorHelper.php to

/symfony_project_root
/lib
/helper
MenuGeneratorHelper.php

3. some changes to helper

define("__MG_CONFIG",'sf_menu_generator_');
// if you don't use prototype - you can remove this definitions
//define("__MG_JSCLASS",'sfMenuGeneratorPluginShortcuts');
//define("__MG_JSOBJECT",'sfMenuGeneratorPluginShortcuts_object');

// add own definition - it will mark active menu item
define("__MG_SELECTED_ITEM",'navSelectedMenu');


if you don't use prototype - you can remove this method & links to it

/*
function mg_add_shortcut($menuItem) {
//...
}
*/


add marker for selected menu item and fix link assembling for usage i18n -> __()

function mg_parse_menuitem($node,$module,$reverse)
{
//...

$requestObj = sfContext::getInstance()->getRequest();
$routeObj = $requestObj->getAttribute("sf_route");

foreach($menuItem as $key=>$value)
{
if(substr($key,0,5) == 'html_')
{
$arrKey = substr($key,5);
$arrValue = $value;
$menuItemUrl = url_for($menuItem['link']);

// remove /frontend_dev.php/ from URL
if(sfContext::getInstance()->getConfiguration()->isDebug()) {
$appConfiguration = sfContext::getInstance()->getConfiguration();
$pattern = "/".$appConfiguration->getApplication()."_".$appConfiguration->getEnvironment().".php";
$menuItemUrl = ereg_replace($pattern, "", $menuItemUrl);
}

if($requestObj->getPathInfo() == $menuItemUrl) {
$htmlattrs[] = "id=\"".__MG_SELECTED_ITEM."\"";
}

$htmlattrs[] = $arrKey."=\"$arrValue\"";
}
if(substr($key,0,2) == 'a_')
{
$aattrs[] = substr($key,2)."=\"$value\"";
}
}
$prepend = '';

return $prepend.content_tag('li',
mg_menuitem($app_items).
link_to(
(isset($menuItem['text'])?__($menuItem['text']):''),
(isset($menuItem['link'])?$menuItem['link']:'#'),
implode(' ',$aattrs)
), implode(' ',$htmlattrs)
);
}


4. Add helper to standard_helpers list (settings.yml)

all:
.settings:
standard_helpers: [Partial, Cache, I18N, Form, MenuGenerator]


5. Clean up

symfony cc


that's all )

plugin configuration here.

1 comment:

Unknown said...

Hi,

I read your mod to the symfony menu generator plugin. All I can really tell from it that you changed MenuGeneratorHelper.php after installing the plugin? It seems you suggest some manual unpacking but why not install it in the first place?

Then, it is not clear to me exactly where you changed the code in mg_parse_menuitem.

Any chance you could send me the modified version or clarify your comments?

Thanks a lot in advance.