Help with Custom Plugin

For Joomla! 5.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Thu Mar 03, 2022 3:29 am

Help with Custom Plugin

Post by joom-rookie » Sat Apr 20, 2024 6:50 am

Hey guys,
to keep this short... I've been working on a simple Custom button for the Editor. All of this code I got and modified from existing plugins.

So far, this works to Add the button to the editor, and Opens a modal window when clicked.

The problem is, the modal window is always empty. None of the $button->content is in the page source. I am at a complete loss here, even tried ChatGPT for help with no luck.

Code below: Can anyone shed some light on this please?

Code: Select all

use Joomla\CMS\Language\Text;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Plugin\CMSPlugin;

jimport( 'joomla.plugin.plugin' );

class PlgEditorsXtdMacrosButton extends CMSPlugin
{
protected $autoloadLanguage = true;
function onDisplay($name)
    {
	$db = JFactory::getDBO();
	$query = $db->getQuery(true);
	$query->select($db->quoteName('template'));
	$query->from($db->quoteName('#__template_styles'));
	$query->where($db->quoteName('client_id') . ' = 0');
	$query->order($db->quoteName('home') . ' DESC');
	$db->setQuery($query);
	$template = $db->loadResult();
		
        $filePath = JPATH_ROOT . '/templates/' . $template . '/html/shortcodes.ini';
		
        $fileContent = file_get_contents($filePath);
		
	preg_match_all('/\[([^\]]+)\]/', $fileContent, $matches);
	$keys = $matches[1];
	$keyList = implode('<br>', $keys);
		
	$button = new CMSObject();
        $button->modal   = true;
        //$button->content = '<div class="macros-list">' . $keyList . '</div>';
        $button->content = '<div class="macros-list"><h1>Static Content</h1></div>';
        $button->text    = Text::_('PLG_EDITORS-XTD_MACROSBUTTON_BUTTON_TEXT');
        $button->name    = $this->_type . '_' . $this->_name;
        $button->icon    = 'file';
        $button->iconSVG = '<svg width="24px" height="24px" viewBox="0 0 100 100"><polygon fill="#1cc3ed" points="89.189,12.197 64.033,12.197 45.632,43.797 27.231,12.197 2.074,12.197 45.571,87.803 45.632,87.693 45.692,87.803"><animate attributeName="fill" values="#1cc3ed;pink;orange;yellow;#1cc3ed" dur="10s" repeatCount="indefinite" /></polygon><polygon fill="#FFFFFF" points="97.926,12.197 72.77,12.197 54.368,43.797 35.967,12.197 10.81,12.197 54.307,87.803 54.368,87.693 54.43,87.803"/></svg>';
		
	$button->options = [
        'height' => '300px',
        'width'  => '800px',
        'bodyHeight'  => '70',
        'modalWidth'  => '80',
        ];
		return $button;
    }
}

-------- Plugin explanation if you want it ------------

I use EasyShortcodes on most websites, and I want to add an Editor button that opens a modal window with the macros available. The user can click any of them and they will be added to the editor window.

I havent got to that last part yet. Im stuck on getting "anything" to show in the modal at all. Even static content doesn't show, and none of it ever appears in the page source.
But $button->text does indeed get put in the Modal Title... so this means it's ok right?

MarkRS
Joomla! Explorer
Joomla! Explorer
Posts: 347
Joined: Thu Oct 29, 2009 8:28 am
Location: UK

Re: Help with Custom Plugin

Post by MarkRS » Sat Apr 20, 2024 10:08 am

If you don't "use" the relevant Factory (don't write "JFactory") then can it be loading the database stuff? I wouldn't have thought so. Although, if that were the case I'd've expected the whole thing to fail, I would correct that first and see if it makes a difference.

Also, although I'm not very familiar with plugins, I'd suggest your "jimport" stuff is not the J5 way.
It's a community, the more we all contribute, the better it will be.

joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Thu Mar 03, 2022 3:29 am

Re: Help with Custom Plugin

Post by joom-rookie » Sun Apr 21, 2024 12:59 am

Hi Mark, thanks for your reply.
This is my first time attempting to write a plugin too. As I mentioned in the initial post, I got this from an existing plugin, PhocaDownload Button Plugin.

It uses jimport('joomla.plugin.plugin') too, and it seems to work fine??

The database stuff... I really dont like it, it was the only way I could get the current frontend template name for the path to the file, because: Factory::getApplication()->getTemplate always returns the Admin template, not the frontend template.

This is something I plan on getting back to, I'm certain there is a better way than going to the database.

The plugin currently works, no errors of any kind displayed. Just the modal window is always empty no matter what I try to pass to: $button->content

I tried everything I could think of, including lots of ChatGPT's suggestions to "inject" the content into the modal window, but nothing worked. I am completely lost with this one.

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2757
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Help with Custom Plugin

Post by ceford » Sun Apr 21, 2024 8:08 am

I don't have an answer - but here are some thoughts:

The CMSObject is deprecated and will be removed in J6. It contains almost no useful functions. And it is not clear what you want it to contain. If you need to use a modal could you use a Bootstrap modal? https://docs.joomla.org/J4.x:Using_Boot ... n_Joomla_4

Could you make the button into a dropdown list with the available items listed as in any other dropdown list? There seem to be plenty of articles on how to create custom buttons for TinyMCE - I have never tried it myself.

joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Thu Mar 03, 2022 3:29 am

Re: Help with Custom Plugin

Post by joom-rookie » Mon Apr 22, 2024 6:48 am

Hey Ceford,
I don't understand what CMSObject is doing, the code I have is from another plugin that does the same thing, that Im trying to modify to do what I need.

A dropdown menu wont be ideal since there are quite a few macros created, 20+ usually.

The modal will contain just text with hyperlink. nothing fancy. The problem is that $button.content is not going to the modal. I've tested the output as a simple echo, and it's just plain text, so the content is generated, just will not go into the modal window.
I did try with a bootstrap modal, but it was empty too, so I switched back to the plugin code I was modifying.

Thanks.

User avatar
ceford
Joomla! Hero
Joomla! Hero
Posts: 2757
Joined: Mon Feb 24, 2014 10:38 pm
Location: Edinburgh, Scotland
Contact:

Re: Help with Custom Plugin

Post by ceford » Mon Apr 22, 2024 7:53 am

Caveat: out of my depth!

The PageBreak plugin has a link to a form that is rendered when the modal opens. So maybe you should look at that plugin and provide a link in your plugin that will render the content you need.

joom-rookie
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Thu Mar 03, 2022 3:29 am

Re: Help with Custom Plugin

Post by joom-rookie » Tue Apr 23, 2024 1:44 am

I had a look at the Page Break button plugin, it loads a joomla component in an iframe, just like most other button plugins I was looking at for ideas.

The problem in my case, is that I need to read the contents of a file, and echo them as clickable links.
So while I can load the required ini file (in a round-about way) inside the modal using an iFrame ($button->link), for my needs this isn't going to be helpful.

But thanks for your suggestion.

For now, I've given up on this because I've actually wasted too much work time on this, and I don't know what I'm doing for the most part, I don't have the knowledge and understanding of the deeper Joomla stuff to get this done myself.

I will leave this post here, in case someone comes across it with an answer, but for now I've given up, until I do some actual work! hahah!

Thanks guys.


Post Reply

Return to “Joomla! 5.x Coding”