Skip to content

Commit 8a97cd5

Browse files
committed
Added new RenderElementsEvent
The new event allows plugin / module authors to override the template rendering for the elements index container.
1 parent 00c3c96 commit 8a97cd5

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/base/Element.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use craft\events\RegisterElementTableAttributesEvent;
3434
use craft\events\RegisterPreviewTargetsEvent;
3535
use craft\events\SetEagerLoadedElementsEvent;
36+
use craft\events\RenderElementsEvent;
3637
use craft\events\SetElementRouteEvent;
3738
use craft\events\SetElementTableAttributeHtmlEvent;
3839
use craft\fieldlayoutelements\BaseField;
@@ -237,6 +238,11 @@ abstract class Element extends Component implements ElementInterface
237238
*/
238239
const EVENT_REGISTER_HTML_ATTRIBUTES = 'registerHtmlAttributes';
239240

241+
/**
242+
* @event RenderElementsEvent The event that is triggered when rendering elements
243+
*/
244+
const EVENT_RENDER_ELEMENTS = 'renderElements';
245+
240246
/**
241247
* @event SetElementRouteEvent The event that is triggered when defining the route that should be used when this element’s URL is requested
242248
*
@@ -691,17 +697,19 @@ protected static function defineSearchableAttributes(): array
691697
*/
692698
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes): string
693699
{
700+
$source = ElementHelper::findSource(static::class, $sourceKey, $context);
701+
694702
$variables = [
695703
'viewMode' => $viewState['mode'],
696704
'context' => $context,
697705
'disabledElementIds' => $disabledElementIds,
698706
'collapsedElementIds' => Craft::$app->getRequest()->getParam('collapsedElementIds'),
699707
'showCheckboxes' => $showCheckboxes,
708+
'source' => $source
700709
];
701710

702711
// Special case for sorting by structure
703712
if (isset($viewState['order']) && $viewState['order'] === 'structure') {
704-
$source = ElementHelper::findSource(static::class, $sourceKey, $context);
705713

706714
if (isset($source['structureId'])) {
707715
$elementQuery->orderBy(['lft' => SORT_ASC]);
@@ -738,7 +746,14 @@ public static function indexHtml(ElementQueryInterface $elementQuery, array $dis
738746

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

741-
return Craft::$app->getView()->renderTemplate($template, $variables);
749+
// Give plugins a chance to modify the element rendering
750+
$event = new RenderElementsEvent([
751+
'variables' => $variables,
752+
'template' => $template
753+
]);
754+
Event::trigger(static::class, self::EVENT_RENDER_ELEMENTS, $event);
755+
756+
return Craft::$app->getView()->renderTemplate($event->template, $event->variables);
742757
}
743758

744759
/**

src/events/RenderElementsEvent.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* @link https://craftcms.com/
4+
* @copyright Copyright (c) Pixel & Tonic, Inc.
5+
* @license https://craftcms.github.io/license/
6+
*/
7+
8+
namespace craft\events;
9+
10+
use craft\base\ElementActionInterface;
11+
use craft\elements\db\ElementQueryInterface;
12+
use yii\base\Event;
13+
14+
/**
15+
* Render Element event class.
16+
*
17+
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
18+
* @since 3.0.0
19+
*/
20+
class RenderElementsEvent extends Event
21+
{
22+
/**
23+
* @var string the template to render
24+
*/
25+
public $template;
26+
27+
/**
28+
* @var array the variables passed to the template
29+
*/
30+
public $variables;
31+
}

0 commit comments

Comments
 (0)