Skip to content

Commit

Permalink
Bad formatting of concrete event handling example
Browse files Browse the repository at this point in the history
Close gh-33
  • Loading branch information
nikosdion committed May 22, 2024
1 parent 583ab9e commit cede9f6
Showing 1 changed file with 45 additions and 71 deletions.
116 changes: 45 additions & 71 deletions sections/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1817,91 +1817,65 @@ namespace Acme\Plugin\System\Example;

defined('_JEXEC') || die;

use Joomla\CMS\Event\Result\ResultAwareInterface;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Content\Administrator\Table\ArticleTable;
use Joomla\Event\Event;
use Joomla\Event\SubscriberInterface;

class ExamplePlugin extends CMSPlugin implements SubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'onExampleFoobar' => 'handleFoobar',
];
}

public function handleFoobar(Event $event)
{
// If using a concrete event, do it the simple way
if ($event instanceof \Acme\Component\Example\Administrator\Event\Foobar) {
/** @var ArticleTable $article */
$article = $event->getArgument('article');
/** @var integer $count */
$count = $event->getArgument('count', 10);<?php
namespace Acme\Plugin\System\Example;
public static function getSubscribedEvents(): array
{
return [
'onExampleFoobar' => 'handleFoobar',
];
}

defined('_JEXEC') || die;
public function handleFoobar(Event $event)
{
// If using a concrete event, do it the simple way
if ($event instanceof \Acme\Component\Example\Administrator\Event\Foobar) {
/** @var ArticleTable $article */
$article = $event->getArgument('article');
/** @var integer $count */
$count = $event->getArgument('count', 10);
// If using a generic event, do it the hard way
} else {
[$article, $count] = $event->getArguments();

if (!$article instanceof ArticleTable) {
throw new \InvalidArgumentException('The article argument to the onExampleFoobar event must be an ArticleTable');
}

use Joomla\CMS\Event\Result\ResultAwareInterface;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\Component\Content\Administrator\Table\ArticleTable;
use Joomla\Event\Event;
use Joomla\Event\SubscriberInterface;
if (!is_int($count) || $count <= 0) {
throw new \InvalidArgumentException('The count argument to the onExampleFoobar event must be a positive integer');
}
}

class ExamplePlugin extends CMSPlugin implements SubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
'onExampleFoobar' => 'handleFoobar',
];
}
// Do something...
$myResult = [
'foobar' => $article->title,
'foobaz' => $count
];

public function handleFoobar(Event $event)
{
// If using a concrete event, do it the simple way
if ($event instanceof \Acme\Component\Example\Administrator\Event\Foobar) {
/** @var ArticleTable $article */
$article = $event->getArgument('article');
/** @var integer $count */
$count = $event->getArgument('count', 10);
// If using a generic event, do it the hard way
} else {
[$article, $count] = $event->getArguments();

if (!$article instanceof ArticleTable) {
throw new \InvalidArgumentException('The article argument to the onExampleFoobar event must be an ArticleTable');
}

if (!is_int($count) || $count <= 0) {
throw new \InvalidArgumentException('The count argument to the onExampleFoobar event must be a positive integer');
}
}
// Return the result
$this->setResult($myResult);
}

// Do something...
$myResult = [
'foobar' => $article->title,
'foobaz' => $count
];
private function setResult(Event $event, $value): void
{
if ($event instanceof ResultAwareInterface) {
$event->addResult($value);

// Return the result
$this->setResult($myResult);
}

private function setResult(Event $event, $value): void
{
if ($event instanceof ResultAwareInterface) {
$event->addResult($value);

return;
}
return;
}

$result = $event->getArgument('result', []) ?: [];
$result = is_array($result) ? $result : [];
$result[] = $value;
$event->setArgument('result', $result);
}
$result = $event->getArgument('result', []) ?: [];
$result = is_array($result) ? $result : [];
$result[] = $value;
$event->setArgument('result', $result);
}
}</programlisting>

<para>In the example code above the setResult method can be reused by all of your event handlers, at any return
Expand Down

0 comments on commit cede9f6

Please sign in to comment.