Skip to content

Commit 8cd998c

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 a787fe0 commit 8cd998c

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
@@ -29,6 +29,7 @@
2929
use craft\events\RegisterElementSourcesEvent;
3030
use craft\events\RegisterElementTableAttributesEvent;
3131
use craft\events\RegisterPreviewTargetsEvent;
32+
use craft\events\RenderElementsEvent;
3233
use craft\events\SetElementRouteEvent;
3334
use craft\events\SetElementTableAttributeHtmlEvent;
3435
use craft\helpers\ArrayHelper;
@@ -187,6 +188,11 @@ abstract class Element extends Component implements ElementInterface
187188
*/
188189
const EVENT_REGISTER_HTML_ATTRIBUTES = 'registerHtmlAttributes';
189190

191+
/**
192+
* @event RenderElementsEvent The event that is triggered when rendering elements
193+
*/
194+
const EVENT_RENDER_ELEMENTS = 'renderElements';
195+
190196
/**
191197
* @event SetElementRouteEvent The event that is triggered when defining the route that should be used when this element’s URL is requested
192198
*
@@ -574,17 +580,19 @@ protected static function defineSearchableAttributes(): array
574580
*/
575581
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes): string
576582
{
583+
$source = ElementHelper::findSource(static::class, $sourceKey, $context);
584+
577585
$variables = [
578586
'viewMode' => $viewState['mode'],
579587
'context' => $context,
580588
'disabledElementIds' => $disabledElementIds,
581589
'collapsedElementIds' => Craft::$app->getRequest()->getParam('collapsedElementIds'),
582590
'showCheckboxes' => $showCheckboxes,
591+
'source' => $source
583592
];
584593

585594
// Special case for sorting by structure
586595
if (isset($viewState['order']) && $viewState['order'] === 'structure') {
587-
$source = ElementHelper::findSource(static::class, $sourceKey, $context);
588596

589597
if (isset($source['structureId'])) {
590598
$elementQuery->orderBy(['lft' => SORT_ASC]);
@@ -621,7 +629,14 @@ public static function indexHtml(ElementQueryInterface $elementQuery, array $dis
621629

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

624-
return Craft::$app->getView()->renderTemplate($template, $variables);
632+
// Give plugins a chance to modify the element rendering
633+
$event = new RenderElementsEvent([
634+
'variables' => $variables,
635+
'template' => $template
636+
]);
637+
Event::trigger(static::class, self::EVENT_RENDER_ELEMENTS, $event);
638+
639+
return Craft::$app->getView()->renderTemplate($event->template, $event->variables);
625640
}
626641

627642
/**

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)