Joomla Hacks
My notes while learning about Joomla extension development.
Google Maps API Key Storage
Extension Type: Component
Add the field parameters to the component configuration and update the respective language files. For example,
/administrator/components/com_mycomponent/config.xml
<!-- insert after existing fields -->
<field name="googleapik" type="text" size="40"
label="COM_MYCOMPONENT_FIELD_GOOGLEAPIK_LABEL"
description="COM_MYCOMPONENT_FIELD_GOOGLEAPIK_DESC" />
Update the edit.php file containing the form that needs to retrieve the api key from the database. For example,
/administrator/components/com_mycomponent/views/mycomponent/tmpl/edit.php
<?php
defined('_JEXEC') or die;
// get google api key from component config
jimport('joomla.application.component.helper');
$googleapik = JComponentHelper::getParams('com_mycomponent')->get('googleapik');
?>
Put the API key in a JavaScript object below the form and before the JavaScript application that will use it.
<script type="text/javascript">
// global app namespace
var com_mycomponent = com_mycomponent || {};
// define global app vars here
com_mycomponent.googleapik = '<?php echo $googleapik; ?>';
</script>
Extension Type: Plugin
Add a config node for the the field parameters to the plugin manifest file and update the respective language files. For example,
/plugins/content/myplugin/myplugin.xml
<config>
<fields name="params">
<fieldset name="basic">
<field
name="googleapik"
type="text"
label="PLG_CONTENT_MYPLUGIN_FIELD_GOOGLEAPIK_LABEL"
description="PLG_CONTENT_MYPLUGIN_FIELD_GOOGLEAPIK_DESCRIPTION"
/>
</fieldset>
</fields>
</config>
Update the template override file containing the form that needs to retrieve the api key from the database. For example, add this php code before the form markup.
/administrator/templates/isis/html/com_content/article/edit.php
$plg_content_myplugin = JPluginHelper::getPlugin('content', 'myplugin');
$plg_content_myplugin_params = new JRegistry($plg_content_myplugin->params);
$googleapik = $plg_content_myplugin_params->get('googleapik');