I try to hide a menu item programmatically, I found this publication that explains the process as follows
- Create custom html module
- Install and enable the Sourcerer plugin
- In the module editor use a code similar to this
I have done the steps, but I have not achieved the code written in php to be executed, in fact when trying a simple {source} <?php echo 'test'; ?> {/source}
or {source}[[php echo 'test'; ?]]{/source}
the code is not executed, nor is it shown as a comment. It just disappears. If I disable Sourcerer the code is shown as text. I tested the same code in a new instance of joomla and it works properly. Going back to the joomla instance of the project where I work, I verify that the Sourcerer plugin is enabled, verify that the configuration of executing html, css, js and php code is enabled. I have tested with different editors. Unfortunately the same result.
As a final comment I can verify that using Sourcerer, the code written in html and javascript is executed properly. In fact, I temporarily hide the menu using an ajax call to a complement controller method. This works, but for a moment the menu item is displayed and I want to avoid this and do it with php
Update 1
Following Robbie Jackson recommendation I have created a module with logic to hide the menu item, the module has the position debug. For some reason and although the CSS is added into the style tag the menu item remains visible. The same happens if I try to inject a style sheet
mod_name.php
<?php
defined('_JEXEC') or die;
require_once dirname(__FILE__) . '/helper.php';
$currentUser = modName::hideMenuItem();
Using addStyleDeclaration
class ModNameHelper
{
public static function hideMenuItem()
{
$accountType = JFactory::getUser()->getParam('account_type');
if ($accountType !== 'agency') {
$style = 'li.item116 { display: none; }';
JFactory::getDocument()->addStyleDeclaration($style);
}
}
}
Using addStyleSheet
helper.php
class ModNameHelper
{
public static function hideMenuItem()
{
$accountType = JFactory::getUser()->getParam('account_type');
if ($accountType !== 'agency') {
JFactory::getDocument()->addStyleSheet(JURI::root() . 'modules/mod_name/mod_name.css', ["version" => "auto"]);
}
}
}
In the example where I use addStyleDeclaration
in head in the style
tag it is added li.item116 { display: none; }
and in the example using addStyleSheet
the style sheet is injected but in both cases the menu item remains visible.
Any ideas? Thanks in advance
I will appreciate your help