diff --git a/src/base/Element.php b/src/base/Element.php index 28c8f44d94a..b314f7f7bc9 100644 --- a/src/base/Element.php +++ b/src/base/Element.php @@ -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; @@ -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 * @@ -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'], @@ -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); } /** diff --git a/src/base/ElementInterface.php b/src/base/ElementInterface.php index c1a1007cd57..f5570e65a08 100644 --- a/src/base/ElementInterface.php +++ b/src/base/ElementInterface.php @@ -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. diff --git a/src/controllers/ElementIndexesController.php b/src/controllers/ElementIndexesController.php index 591366d9ee9..d54c061c13b 100644 --- a/src/controllers/ElementIndexesController.php +++ b/src/controllers/ElementIndexesController.php @@ -530,7 +530,8 @@ protected function elementResponseData(bool $includeContainer, bool $includeActi $this->sourceKey, $this->context, $includeContainer, - $showCheckboxes + $showCheckboxes, + $this->source ); $responseData['headHtml'] = $view->getHeadHtml(); diff --git a/src/events/RenderElementsEvent.php b/src/events/RenderElementsEvent.php new file mode 100644 index 00000000000..2baa20aeaf5 --- /dev/null +++ b/src/events/RenderElementsEvent.php @@ -0,0 +1,31 @@ + + * @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; +}