Skip to content

Commit

Permalink
Document event: BeforeCompileHeadEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Apr 30, 2023
1 parent b8b35f3 commit b6c1fc4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
4 changes: 3 additions & 1 deletion libraries/src/Document/Renderer/Html/MetasRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Joomla\CMS\Document\Renderer\Html;

use Joomla\CMS\Document\DocumentRenderer;
use Joomla\CMS\Event\Document\BeforeCompileHeadEvent;
use Joomla\CMS\Factory;
use Joomla\CMS\Helper\TagsHelper;
use Joomla\CMS\Uri\Uri;
Expand Down Expand Up @@ -58,7 +59,8 @@ public function render($head, $params = [], $content = null)
}

// Trigger the onBeforeCompileHead event
$app->triggerEvent('onBeforeCompileHead');
$event = new BeforeCompileHeadEvent('onBeforeCompileHead', ['subject' => $this->_doc]);
$app->triggerEvent('onBeforeCompileHead', $event);

// Add Script Options as inline asset
$scriptOptions = $this->_doc->getScriptOptions();
Expand Down
3 changes: 3 additions & 0 deletions libraries/src/Event/CoreEventAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Joomla\CMS\Event;

use Joomla\CMS\Event\Document\BeforeCompileHeadEvent;
use Joomla\CMS\Event\Model\BeforeBatchEvent;
use Joomla\CMS\Event\Plugin\System\Webauthn\Ajax as PlgSystemWebauthnAjax;
use Joomla\CMS\Event\Plugin\System\Webauthn\AjaxChallenge as PlgSystemWebauthnAjaxChallenge;
Expand Down Expand Up @@ -115,6 +116,8 @@ trait CoreEventAware
'onAjaxWebauthnInitcreate' => PlgSystemWebauthnAjaxInitCreate::class,
'onAjaxWebauthnLogin' => PlgSystemWebauthnAjaxLogin::class,
'onAjaxWebauthnSavelabel' => PlgSystemWebauthnAjaxSaveLabel::class,
// Document
'onBeforeCompileHead' => BeforeCompileHeadEvent::class,
];

/**
Expand Down
70 changes: 70 additions & 0 deletions libraries/src/Event/Document/AbstractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\CMS\Event\Document;

use Joomla\CMS\Document\Document;
use Joomla\CMS\Event\AbstractImmutableEvent;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Event class for Document events
*
* @since __DEPLOY_VERSION__
*/
abstract class AbstractEvent extends AbstractImmutableEvent
{
/**
* Constructor.
*
* @param string $name The event name.
* @param array $arguments The event arguments.
*
* @throws \BadMethodCallException
*
* @since __DEPLOY_VERSION__
*/
public function __construct($name, array $arguments = [])
{
if (!\array_key_exists('subject', $arguments)) {
throw new \BadMethodCallException("Argument 'subject' of event {$this->name} is required but has not been provided");
}

parent::__construct($name, $arguments);
}

/**
* Setter for the subject argument.
*
* @param Document $value The value to set
*
* @return Document
*
* @since __DEPLOY_VERSION__
*/
protected function setSubject(Document $value): Document
{
return $value;
}

/**
* Returns associated Document instance.
*
* @return Document
*
* @since __DEPLOY_VERSION__
*/
public function getDocument(): Document
{
return $this->getArgument('subject');
}
}
22 changes: 22 additions & 0 deletions libraries/src/Event/Document/BeforeCompileHeadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Joomla! Content Management System
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\CMS\Event\Document;

// phpcs:disable PSR1.Files.SideEffects
\defined('JPATH_PLATFORM') or die;
// phpcs:enable PSR1.Files.SideEffects

/**
* Event class for onBeforeCompileHead event
*
* @since __DEPLOY_VERSION__
*/
class BeforeCompileHeadEvent extends AbstractEvent
{}

0 comments on commit b6c1fc4

Please sign in to comment.