Skip to content

Commit

Permalink
Added new RenderElementsEvent
Browse files Browse the repository at this point in the history
The new event allows plugin / module authors to override the template rendering for the elements index container.
  • Loading branch information
leevigraham committed May 14, 2020
1 parent 413a667 commit c266759
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use craft\events\RegisterElementSourcesEvent;
use craft\events\RegisterElementTableAttributesEvent;
use craft\events\RegisterPreviewTargetsEvent;
use craft\events\RenderElementsEvent;
use craft\events\SetElementRouteEvent;
use craft\events\SetElementTableAttributeHtmlEvent;
use craft\helpers\ArrayHelper;
Expand Down Expand Up @@ -187,6 +188,11 @@ abstract class Element extends Component implements ElementInterface
*/
const EVENT_REGISTER_HTML_ATTRIBUTES = 'registerHtmlAttributes';

/**
* @event RenderElementsEvent The event that is triggered when rendering elements
*/
const EVENT_RENDER_ELEMENTS = 'renderElements';

/**
* @event SetElementRouteEvent The event that is triggered when defining the route that should be used when this element’s URL is requested
*
Expand Down Expand Up @@ -572,7 +578,7 @@ protected static function defineSearchableAttributes(): array
/**
* @inheritdoc
*/
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes): string
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes, array $source = null): string
{
$variables = [
'viewMode' => $viewState['mode'],
Expand Down Expand Up @@ -621,7 +627,14 @@ public static function indexHtml(ElementQueryInterface $elementQuery, array $dis

$template = '_elements/' . $viewState['mode'] . 'view/' . ($includeContainer ? 'container' : 'elements');

return Craft::$app->getView()->renderTemplate($template, $variables);
// Give plugins a chance to modify the element rendering
$event = new RenderElementsEvent([
'variables' => $variables,
'template' => $template
]);
Event::trigger(static::class, self::EVENT_RENDER_ELEMENTS, $event);

return Craft::$app->getView()->renderTemplate($event->template, $event->variables);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,10 @@ public static function searchableAttributes(): array;
* @param string|null $context
* @param bool $includeContainer
* @param bool $showCheckboxes
* @param array|null $source
* @return string The element index HTML
*/
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes): string;
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes, array $source = null): string;

/**
* Returns the sort options for the element type.
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/ElementIndexesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ protected function elementResponseData(bool $includeContainer, bool $includeActi
$this->sourceKey,
$this->context,
$includeContainer,
$showCheckboxes
$showCheckboxes,
$this->source
);

$responseData['headHtml'] = $view->getHeadHtml();
Expand Down
31 changes: 31 additions & 0 deletions src/events/RenderElementsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\events;

use craft\base\ElementActionInterface;
use craft\elements\db\ElementQueryInterface;
use yii\base\Event;

/**
* Render Element event class.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class RenderElementsEvent extends Event
{
/**
* @var string the template to render
*/
public $template;

/**
* @var array the variables passed to the template
*/
public $variables;
}

0 comments on commit c266759

Please sign in to comment.