Skip to content

Commit

Permalink
Move element index variable prep into template hooks
Browse files Browse the repository at this point in the history
Fixes #16254
  • Loading branch information
brandonkelly committed Dec 2, 2024
1 parent 0a04e9c commit 130fd77
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- Fixed a bug where renaming asset folders could move them to the webroot on Windows. ([#16215](https://github.com/craftcms/cms/issues/16215))
- Fixed a bug where Matrix fields’ content tables weren’t getting renamed properly when applying project config changes. ([#16227](https://github.com/craftcms/cms/issues/16227))
- Fixed a bug where utilities’ `isSelectable()` methods weren’t being respected.
- Fixed an exception that could be thrown when displaying entry indexes, if any `EVENT_INIT` or `EVENT_DEFINE_BEHAVIORS` entry event handlers were calling `getSection()` or `getType()` on the entry. ([#16254](https://github.com/craftcms/cms/issues/16254))

## 4.13.3 - 2024-11-22

Expand Down
12 changes: 1 addition & 11 deletions src/templates/_elements/sources.twig
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
{% apply spaceless %}
{% set keyPrefix = keyPrefix ?? '' %}
{% set isTopLevel = not keyPrefix %}

{% if isTopLevel %}
{% set elementInstance = craft.app.elements.createElement(elementType) %}
{% set baseSortOptions = elementInstance.sortOptions()|map((option, key) => {
label: option.label ?? option,
attr: option.attribute ?? option.orderBy ?? key,
defaultDir: option.defaultDir ?? 'asc',
})|values %}
{% set tableColumns = craft.app.elementSources.getAvailableTableAttributes(elementType) %}
{% endif %}
{% hook 'cp.elements.sources' %}

{% macro sourceLink(key, source, isTopLevel, elementType, baseSortOptions, tableColumns) %}
{{ tag('a', {
Expand Down
11 changes: 2 additions & 9 deletions src/templates/_elements/toolbar.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
"Display as thumbnails",
]) %}

{% set elementInstance = craft.app.elements.createElement(elementType) %}
{% set context = context is defined ? context : 'index' %}
{% set showStatusMenu = (showStatusMenu is defined and showStatusMenu != 'auto' ? showStatusMenu : elementInstance.hasStatuses()) %}
{% set showSiteMenu = (craft.app.getIsMultiSite() ? (showSiteMenu ?? 'auto') : false) %}
{% if showSiteMenu is same as ('auto') %}
{% set showSiteMenu = elementInstance.isLocalized() %}
{% endif %}
{% set idPrefix = "elementtoolbar#{random()}-" %}
{% hook 'cp.elements.toolbar' %}

{% if showStatusMenu or context == 'index' %}
<div>
Expand All @@ -26,7 +19,7 @@
<ul class="padded">
<li><a data-status="" class="sel"><span class="status all"></span>{{ "All"|t('app') }}</a></li>
{% if showStatusMenu %}
{% for status, info in elementInstance.statuses() %}
{% for status, info in elementStatuses %}
{% set label = info.label ?? info %}
{% set color = info.color ?? '' %}
<li><a data-status="{{ status }}"><span class="status {{ status }} {{ color }}"></span>{{ label }}</a></li>
Expand Down
19 changes: 3 additions & 16 deletions src/templates/_layouts/elementindex.twig
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
{% extends '_layouts/cp.twig' %}

{% set elementInstance = craft.app.elements.createElement(elementType) %}
{% set title = title ?? elementInstance.pluralDisplayName() %}
{% set context = 'index' %}

{% if not elementInstance %}
{% exit 404 %}
{% endif %}

{% set sources = craft.app.elementSources.getSources(elementType, 'index', true) %}

{% set showSiteMenu = (craft.app.getIsMultiSite() ? (showSiteMenu ?? 'auto') : false) %}
{% if showSiteMenu is same as ('auto') %}
{% set showSiteMenu = elementInstance.isLocalized() %}
{% endif %}
{% hook 'cp.layouts.elementindex' %}


{% block contextMenu %}
Expand Down Expand Up @@ -64,8 +51,8 @@

{% block initJs %}
Craft.elementIndex = Craft.createElementIndex('{{ elementType|e("js") }}', $('#page-container'), {
elementTypeName: '{{ elementInstance.displayName()|e("js") }}',
elementTypePluralName: '{{ elementInstance.pluralDisplayName()|e("js") }}',
elementTypeName: '{{ elementDisplayName|e("js") }}',
elementTypePluralName: '{{ elementPluralDisplayName|e("js") }}',
context: '{{ context }}',
storageKey: 'elementindex.{{ elementType|e("js") }}',
criteria: Craft.defaultIndexCriteria,
Expand Down
73 changes: 73 additions & 0 deletions src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\web;

use Craft;
use craft\base\ElementInterface;
use craft\events\AssetBundleEvent;
use craft\events\CreateTwigEvent;
use craft\events\RegisterTemplateRootsEvent;
Expand All @@ -26,6 +27,7 @@
use craft\web\twig\GlobalsExtension;
use craft\web\twig\SinglePreloaderExtension;
use craft\web\twig\TemplateLoader;
use Illuminate\Support\Collection;
use LogicException;
use Throwable;
use Twig\Error\LoaderError as TwigLoaderError;
Expand Down Expand Up @@ -361,6 +363,9 @@ public function init(): void
}

// Register the control panel hooks
$this->hook('cp.layouts.elementindex', [$this, '_prepareElementIndexVariables']);
$this->hook('cp.elements.toolbar', [$this, '_prepareElementToolbarVariables']);
$this->hook('cp.elements.sources', [$this, '_prepareElementSourcesVariables']);
$this->hook('cp.elements.element', [$this, '_getCpElementHtml']);
}

Expand Down Expand Up @@ -2383,6 +2388,74 @@ private function _setJsProperty(string $property, array $names): void
$this->registerJs($js, self::POS_HEAD);
}

/** @phpstan-ignore-next-line */
private function _prepareElementIndexVariables(array &$context): ?string
{
/** @var class-string<ElementInterface> $elementType */
$elementType = $context['elementType'];

$context['title'] ??= $elementType::pluralDisplayName();
$context['context'] = 'index';
$context['sources'] = Craft::$app->getElementSources()->getSources($elementType, withDisabled: true);

$context['showSiteMenu'] = Craft::$app->getIsMultiSite() ? ($context['showSiteMenu'] ?? 'auto') : false;
if ($context['showSiteMenu'] === 'auto') {
$context['showSiteMenu'] = $elementType::isLocalized();
}

$context['elementDisplayName'] = $elementType::displayName();
$context['elementPluralDisplayName'] = $elementType::pluralDisplayName();

return null;
}

/** @phpstan-ignore-next-line */
private function _prepareElementToolbarVariables(array &$context): ?string
{
/** @var class-string<ElementInterface> $elementType */
$elementType = $context['elementType'];

$context['context'] ??= 'index';
$context['showStatusMenu'] ??= 'auto';
if ($context['showStatusMenu'] === 'auto') {
$context['showStatusMenu'] = $elementType::hasStatuses();
}
$context['showSiteMenu'] = Craft::$app->getIsMultiSite() ? ($context['showSiteMenu'] ?? 'auto') : false;
if ($context['showSiteMenu'] === 'auto') {
$context['showSiteMenu'] = $elementType::isLocalized();
}
$context['idPrefix'] = sprintf('elementtoolbar%s-', mt_rand());

if ($context['showStatusMenu']) {
$context['elementStatuses'] = $elementType::statuses();
}

return null;
}

/** @phpstan-ignore-next-line */
private function _prepareElementSourcesVariables(array &$context): ?string
{
$context['keyPrefix'] ??= '';
$context['isTopLevel'] = $context['keyPrefix'] === '';

if ($context['isTopLevel']) {
/** @var class-string<ElementInterface> $elementType */
$elementType = $context['elementType'];
$context['baseSortOptions'] = Collection::make($elementType::sortOptions())
->map(fn($option, $key) => [
'label' => $option['label'] ?? $option,
'attr' => $option['attribute'] ?? $option['orderBy'] ?? $key,
'defaultDir' => $option['defaultDir'] ?? 'asc',
])
->values()
->all();
$context['tableColumns'] = Craft::$app->getElementSources()->getAvailableTableAttributes($elementType);
}

return null;
}

/**
* Returns the HTML for an element in the control panel.
*
Expand Down

0 comments on commit 130fd77

Please sign in to comment.