Skip to content

Commit cdcd8af

Browse files
authored
refactor editor-xtd plugin Weblink class
1 parent 3d926c7 commit cdcd8af

1 file changed

Lines changed: 48 additions & 34 deletions

File tree

  • src/plugins/editors-xtd/weblink/src/Extension

src/plugins/editors-xtd/weblink/src/Extension/Weblink.php

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @package Joomla.Plugin
55
* @subpackage Editors-xtd.weblink
66
*
7-
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7+
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
88
* @license GNU General Public License version 2 or later; see LICENSE.txt
99
*/
1010

@@ -14,19 +14,19 @@
1414
\defined('_JEXEC') or die;
1515
// phpcs:enable PSR1.Files.SideEffects
1616

17-
use Joomla\CMS\Application\CMSApplicationInterface;
17+
use Joomla\CMS\Editor\Button\Button;
18+
use Joomla\CMS\Event\Editor\EditorButtonsSetupEvent;
1819
use Joomla\CMS\Language\Text;
1920
use Joomla\CMS\Plugin\CMSPlugin;
2021
use Joomla\CMS\Session\Session;
21-
use Joomla\Database\DatabaseInterface;
22-
use Joomla\Event\DispatcherInterface;
22+
use Joomla\Event\SubscriberInterface;
2323

2424
/**
2525
* Editor Web Link button
2626
*
2727
* @since 5.1.0
2828
*/
29-
final class Weblink extends CMSPlugin
29+
final class Weblink extends CMSPlugin implements SubscriberInterface
3030
{
3131
/**
3232
* Load the language file on instantiation.
@@ -37,19 +37,39 @@ final class Weblink extends CMSPlugin
3737
protected $autoloadLanguage = true;
3838

3939
/**
40-
* Constructor
40+
* Returns an array of events this subscriber will listen to.
4141
*
42-
* @param DispatcherInterface $dispatcher
43-
* @param array $config
44-
* @param DatabaseInterface $database
42+
* @return array
43+
*
44+
* @since 5.2.0
4545
*/
46-
public function __construct(DispatcherInterface $dispatcher, array $config, CMSApplicationInterface $application)
46+
public static function getSubscribedEvents(): array
4747
{
48-
parent::__construct($dispatcher, $config);
49-
50-
$this->setApplication($application);
48+
return ['onEditorButtonsSetup' => 'onEditorButtonsSetup'];
5149
}
5250

51+
/**
52+
* @param EditorButtonsSetupEvent $event
53+
* @return void
54+
*
55+
* @since 5.2.0
56+
*/
57+
public function onEditorButtonsSetup(EditorButtonsSetupEvent $event): void
58+
{
59+
$subject = $event->getButtonsRegistry();
60+
$disabled = $event->getDisabledButtons();
61+
62+
if (\in_array($this->_name, $disabled)) {
63+
return;
64+
}
65+
66+
$button = $this->onDisplay($event->getEditorId());
67+
68+
if ($button) {
69+
$subject->add($button);
70+
}
71+
}
72+
5373
/**
5474
* Display the button
5575
*
@@ -72,27 +92,21 @@ public function onDisplay($name): ?object
7292
$link = 'index.php?option=com_weblinks&view=weblinks&layout=modal&tmpl=component&'
7393
. Session::getFormToken() . '=1&editor=' . $name;
7494

75-
$button = new \stdClass();
76-
$button->modal = true;
77-
$button->link = $link;
78-
$button->text = Text::_('PLG_EDITORS-XTD_WEBLINK_BUTTON_WEBLINK');
79-
$button->name = $this->_type . '_' . $this->_name;
80-
$button->icon = 'globe';
81-
// phpcs:disable Generic.Files.LineLength
82-
$button->iconSVG = '<svg xmlns="http://www.w3.org/2000/svg" width="24 height="24" fill="currentColor" class="bi bi-globe" viewBox="0 0 16 16">
95+
$button = new Button(
96+
$this->_name,
97+
[
98+
'action' => 'modal',
99+
'link' => $link,
100+
'text' => Text::_('PLG_EDITORS-XTD_WEBLINK_BUTTON_WEBLINK'),
101+
'icon' => 'globe',
102+
'iconSVG' => '<svg xmlns="http://www.w3.org/2000/svg" width="24 height="24" fill="currentColor" class="bi bi-globe" viewBox="0 0 16 16">
83103
<path d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm7.5-6.923c-.67.204-1.335.82-1.887 1.855A7.97 7.97 0 0 0 5.145 4H7.5V1.077zM4.09 4a9.267 9.267 0 0 1 .64-1.539 6.7 6.7 0 0 1 .597-.933A7.025 7.025 0 0 0 2.255 4H4.09zm-.582 3.5c.03-.877.138-1.718.312-2.5H1.674a6.958 6.958 0 0 0-.656 2.5h2.49zM4.847 5a12.5 12.5 0 0 0-.338 2.5H7.5V5H4.847zM8.5 5v2.5h2.99a12.495 12.495 0 0 0-.337-2.5H8.5zM4.51 8.5a12.5 12.5 0 0 0 .337 2.5H7.5V8.5H4.51zm3.99 0V11h2.653c.187-.765.306-1.608.338-2.5H8.5zM5.145 12c.138.386.295.744.468 1.068.552 1.035 1.218 1.65 1.887 1.855V12H5.145zm.182 2.472a6.696 6.696 0 0 1-.597-.933A9.268 9.268 0 0 1 4.09 12H2.255a7.024 7.024 0 0 0 3.072 2.472zM3.82 11a13.652 13.652 0 0 1-.312-2.5h-2.49c.062.89.291 1.733.656 2.5H3.82zm6.853 3.472A7.024 7.024 0 0 0 13.745 12H11.91a9.27 9.27 0 0 1-.64 1.539 6.688 6.688 0 0 1-.597.933zM8.5 12v2.923c.67-.204 1.335-.82 1.887-1.855.173-.324.33-.682.468-1.068H8.5zm3.68-1h2.146c.365-.767.594-1.61.656-2.5h-2.49a13.65 13.65 0 0 1-.312 2.5zm2.802-3.5a6.959 6.959 0 0 0-.656-2.5H12.18c.174.782.282 1.623.312 2.5h2.49zM11.27 2.461c.247.464.462.98.64 1.539h1.835a7.024 7.024 0 0 0-3.072-2.472c.218.284.418.598.597.933zM10.855 4a7.966 7.966 0 0 0-.468-1.068C9.835 1.897 9.17 1.282 8.5 1.077V4h2.355z"/>
84-
</svg>';
85-
// phpcs:enable Generic.Files.LineLength
86-
$button->options = [
87-
'height' => '300px',
88-
'width' => '800px',
89-
'bodyHeight' => '70',
90-
'modalWidth' => '80',
91-
];
92-
93-
return $button;
94-
}
95-
96-
return null; // return null if conditions are not met
104+
</svg>',
105+
// This is whole Plugin name, it is needed for keeping backward compatibility
106+
'name' => $this->_type . '_' . $this->_name,
107+
]
108+
);
109+
return $button;
110+
}
97111
}
98112
}

0 commit comments

Comments
 (0)