Skip to content

Instantly share code, notes, and snippets.

@sanjeevshrestha
Created April 9, 2012 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanjeevshrestha/2342760 to your computer and use it in GitHub Desktop.
Save sanjeevshrestha/2342760 to your computer and use it in GitHub Desktop.
Joomla 2.5.x! com_menu menutypes model changes
/**
* Changes to menutypes.php method 'getTypeOptionsFromXML'
* Why? Currently the menutype does not allow to insert other many variables from metadata.xml file. This makes is rigid if any developer wants to add a variable to the menu url. Yes there is request field type but it does not server the purpose. I don't want to confuse people and also i want to use the same name for multiple state vars.
* How? Allow devs to insert variables as an attribute in menu option item as shown below
* e.g.
* <metadata>
* <menu>
* <options var="view">
* <option value="lists" name="Lists" msg="Listing" />
* <option value="messages" name="Messages" role='message.pms.exec' msg="Messages" />
* <option value="regions" name="Regions" country_id='110' msg="Regions" />
* <option value="search" name="Search" msg="Search Listing" />
* </options>
* </menu>
* </metadata>
*
*/
protected function getTypeOptionsFromXML($file, $component)
{
// Initialise variables.
$options = array();
// Attempt to load the xml file.
if (!$xml = simplexml_load_file($file)) {
return false;
}
// Look for the first menu node off of the root node.
if (!$menu = $xml->xpath('menu[1]')) {
return false;
}
else {
$menu = $menu[0];
}
// If we have no options to parse, just add the base component to the list of options.
if (!empty($menu['options']) && $menu['options'] == 'none')
{
// Create the menu option for the component.
$o = new JObject;
$o->title = (string) $menu['name'];
$o->description = (string) $menu['msg'];
$o->request = array('option' => $component);
$options[] = $o;
return $options;
}
// Look for the first options node off of the menu node.
if (!$optionsNode = $menu->xpath('options[1]')) {
return false;
}
else {
$optionsNode = $optionsNode[0];
}
// Make sure the options node has children.
if (!$children = $optionsNode->children()) {
return false;
}
else {
// Process each child as an option.
foreach ($children as $child)
{
if ($child->getName() == 'option') {
// Create the menu option for the component.
$o = new JObject;
$o->title = (string) $child['name'];
unset( $child[ 'name' ] ); //Unset the name variable
$o->description = (string) $child['msg'];
unset( $child[ 'msg' ] ); //Unset Msg variable
$request = array('option'=> $component,(string)$optionsNode[ 'var' ] => (string)$child[ 'value' ]);
unset( $child[ 'value' ] ); //Unset Child Value
$attributes = $child->attributes();
foreach ( $attributes as $attrib=> $val )
{
$attrib=(string)$attrib;
$request[ $attrib ] = (string)$val;
}
$o->request = $request;
$options[] = $o;
}
elseif ($child->getName() == 'default') {
// Create the menu option for the component.
$o = new JObject;
$o->title = (string) $child['name'];
$o->description = (string) $child['msg'];
$o->request = array('option' => $component);
$options[] = $o;
}
}
}
return $options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment