jeffchannell.com

Menuitem Select in a Custom Component View

Posted in Joomla!
2009-11-07 04:55:18 +0000 UTC

I was doing some restructuring to my soon-to-be-released Joomla component, JMyLife, and wanted to have a slect item in the configuration view in order to allow the admin to select a page to direct users to as a "sign up" page. I originally had this item as part of the global view parameters, but I was having issues with Menu Items overriding the global values on specific views. I started digging through the Joomla API documents, expecting to find a simple JHTML statement I could use to produce this form element, but couldn't. I ended up with a solution that works, though I wish it were a bit easier.

First, I needed to get the class that generates the menu item select, which was done by including it in my view.html.php:

include_once(JPATH_ROOT.DS.'libraries'.DS.'joomla'.DS.'html'.DS.'parameter'.DS.'element'.DS.'menuitem.php');

Also in my view, I added a dummy class (more about this later):

class JmylifeViewConfigurationMenuDummy
{
	function get( $a )
	{
		return '';
	}
}

Finally, I moved into my view's template. The database I created to hold the configuration also stores "type", which dictates what gets rendered. If a row is of type "menuitem" it gets rendered like so:

$_menuXML = new JSimpleXMLElement( '_menuxmlnode', array( 'type' => 'menuitem', 'state' => '1', 'default' => '', 'name' => $row->key ) );
$_menuItem = new JElementMenuItem();
$_menuItem->_parent = new JmylifeViewConfigurationMenuDummy();
echo $_menuItem->fetchElement( '', $row->value, $_menuXML, $row->key );

First, I created a new JSimpleXMLElement that I could pass to the JElementMenuItem class, as the function I was needing to use expects an XML node as one of it's arguments. I then created a new instance of JElementMenuItem, $_menuItem, and assigned my dummy class to $_menuItem->_parent in order to bypass the check on $this->_parent on line 40 of libraries/joomla/html/parameter/element/menuitem.php (I told you I needed that later!). Finally, I pass my key, value, and XML node to fetchElement, which returns a JHTML::_('select.genericlist') that I can echo to the user. Success!

Special thanks to ian_mac in #joomla for pointing me in the right direction!