1

I try to hide a menu item programmatically, I found this publication that explains the process as follows

  1. Create custom html module
  2. Install and enable the Sourcerer plugin
  3. In the module editor use a code similar to this

enter image description here

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

5
  • 1
    I installed the free version of Sourcerer on my Joomla 3.9.13 instance and had problems too. I couldn't get the CSS aspects listed on their main page working, even though I used their "Copy selection as plain text" mechanism to copy and paste code. I did manage to get echo "test" working, but at one point all the other modules at the same template position disappeared! So it might be worth it raising an issue on their forum. But if I were you, I'd just write and install my own little module which did what you want it to do. Commented Jan 19, 2020 at 11:28
  • I updated the question
    – user615274
    Commented Jan 20, 2020 at 4:48
  • 1
    In your addStyleDeclaration call you need to pass $item instead of $style, and item116 should I think be item-116. But rather than relying on the classes which Joomla outputs (and so could theoretically change), I think it would be better to set a Link Class on the menuitem (under Link Type tab in admin) and use that class to select the <a> tag. You might need to do { display:none !important }. Commented Jan 20, 2020 at 12:11
  • Thanks for the comment and the observation of $ item. It worked for me to refer to a class that I defined in the menu item as you have suggested. Finally works as expected
    – user615274
    Commented Jan 20, 2020 at 14:52
  • @user615274 how do we progress this page to a resolution? What remains unfixed? Commented Sep 8, 2020 at 9:16

1 Answer 1

4
  1. From https://www.regularlabs.com/extensions/sourcerer/tutorial:

Sourcerer already creates the $document (or $doc) object for you when it is needed. So you do not have to initialize this yourself.

    {source}
    <?php
        if (true) {
            $style = '.item-135 {display: none;}';
            $doc->addStyleDeclaration($style);
        }
    ?>
    {/source}
  1. I would also urge you to remove many of those extra spaces so that you never have any accidental code breakages.
    Opening <?php tag must not have a space in it. https://3v4l.org/rPd0Y versus https://3v4l.org/QYRVt

  2. Also from the FAQ:

Make sure you are using the WYSIWYG view of your editor.

  1. Also from the FAQ:

You can set certain security settings in the Sourcerer plugin. This way you can prevent certain user group levels and certain content types to not allow the use of Sourcerer. Please look in the html output source of you site to see if you have a Sourcerer comment that tells you this is the case.

  1. Also in the FAQ:

If the php tags are getting stripped out, try double bracket syntax (your attempt was missing the ?). Here's how that would look:

    {source}
    [[?php
        if (true) {
            $style = '.item-135 {display: none;}';
            $doc->addStyleDeclaration($style);
        }
    ?]]
    {/source}
  1. Your simplest test rules out a php error, but for other researchers: errors can be discovered by viewing the generated source.
6
  • Opening <?php tag must not have a space in it. 3v4l.org/rPd0Y versus 3v4l.org/QYRVt Commented Jan 15, 2020 at 3:27
  • thanks for answering, sadly still not working, code like this {source}<?php echo 'hello'; ?>{/ source} is also not shown in the position assigned to the custom html module. If on the other hand I try html content or javascript the text is shown in the position.
    – user615274
    Commented Jan 16, 2020 at 18:51
  • Please stop adding unnecessary spaces to your code -- I am trying to eliminate potential causes. Why did you write {/ source}? I have updated my answer to include a few more potential solutions. Commented Jan 16, 2020 at 21:08
  • English is not my first language, I use google translate to write these messages and usually add spaces. I try to correct them but sometimes they escape me. I can tell you that it is not a matter of spaces
    – user615274
    Commented Jan 17, 2020 at 2:58
  • Can you confirm that items 3, 4, and 5 in my answer did not resolve your issue? Commented Jan 17, 2020 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.