Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use craft\events\RegisterElementTableAttributesEvent;
use craft\events\RegisterPreviewTargetsEvent;
use craft\events\SetEagerLoadedElementsEvent;
use craft\events\RenderElementsEvent;
use craft\events\SetElementRouteEvent;
use craft\events\SetElementTableAttributeHtmlEvent;
use craft\fieldlayoutelements\BaseField;
Expand Down Expand Up @@ -237,6 +238,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 @@ -691,17 +697,19 @@ protected static function defineSearchableAttributes(): array
*/
public static function indexHtml(ElementQueryInterface $elementQuery, array $disabledElementIds = null, array $viewState, string $sourceKey = null, string $context = null, bool $includeContainer, bool $showCheckboxes): string
{
$source = ElementHelper::findSource(static::class, $sourceKey, $context);

$variables = [
'viewMode' => $viewState['mode'],
'context' => $context,
'disabledElementIds' => $disabledElementIds,
'collapsedElementIds' => Craft::$app->getRequest()->getParam('collapsedElementIds'),
'showCheckboxes' => $showCheckboxes,
'source' => $source
];

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

if (isset($source['structureId'])) {
$elementQuery->orderBy(['lft' => SORT_ASC]);
Expand Down Expand Up @@ -738,7 +746,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
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;
}