At the time of Joomla! custom component development, we need to get a component’s parameters for use in different places like in the component itself, modules, and plugins.
Access component parameters from inside own component
To access component parameters in the component itself, I use Factory::getApplication() like the below:
use Joomla\CMS\Factory;
$app = Factory::getApplication();
$params = $app->getParams();
$param = $params->get('show_create_date');
Access component parameters from another component
use Joomla\CMS\Component\ComponentHelper; $content_params = ComponentHelper::getParams( 'com_content' ); $show_date = $content_params->get( 'show_create_date' );
Or even shortcut:
$show_date = ComponentHelper::getParams('com_content')->get('show_create_date');
Get the Module Parameter from the template’s index.php file
use Joomla\CMS\Component\ComponentHelper;
use Joomla\Registry\Registry;
$module = ModuleHelper::getModule('mod_headlineticker');
$headLineParams = new Registry($module->params);
$tickercount = (int) $headLineParams['count'];
Get the plugin’s parameter anywhere inside Joomla
To access a plugin’s parameter (here, it is “relatedarticles” which is a “content” plugin) anywhere inside Joomla 3, load the particular plugin’s parameters in a variable named “$plugin” using PluginHelper::getPlugin function as an array. Later, this array-type variable is boxed in JRegistry class and uses the “get” function to access each and every parameter of the Registry (former JRegistry) class.
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\Registry\Registry;
// Get plugin 'relatedarticles' of type 'content'
$plugin = PluginHelper::getPlugin('content', 'relatedarticles');
// Check if plugin is enabled
if ($plugin) {
// Get plugin params
$pluginParams = new Registry($plugin->params);
$catids = $pluginParams->get('catids');
}
To access Current Menu parameters
use Joomla\CMS\Factory;
$app = Factory::getApplication();
// Get active menu
$currentMenuItem = $app->getMenu()->getActive();
// Get params for active menu
$params = $currentMenuItem->params;
// Access param you want
$yourParameter = $params->get('yourParameter');
To access Menu parameters from a known Itemid
use Joomla\CMS\Factory;
$app = Factory::getApplication();
// Get Itemid from URL
$input = Factory::getApplication()->input;
$itemId = $input->get->get('Itemid', '0', 'INT');
// Get menu from Itemid
$menuItem = $app->getMenu()->getItem($itemId);
// Get params for menuItem
$params = $menuItem->params;
// Access param you want
$yourParameter = $params->get('yourParameter');
Template parameters from inside a template
$param = $this->params->get('paramName', defaultValue);
Template parameters from outside a template
use Joomla\CMS\Factory;
$app = Factory::getApplication('site');
$template = $app->getTemplate(true);
$param = $template->params->get('paramName', defaultValue);
Leave Your Comment