Skip to content

Commit

Permalink
Merge branch '4.x' of https://github.com/craftcms/cms into 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Dec 2, 2024
2 parents 4a76bc6 + 130fd77 commit d4ae5d8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 44 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 an error that could occur when duplicating an element with an Assets field that had a dynamic subpath. ([#16214](https://github.com/craftcms/cms/issues/16214))
- 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 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 `getType()` on the entry. ([#16254](https://github.com/craftcms/cms/issues/16254))
- Fixed a bug where element slideouts had Save buttons even if the user didn’t have permission to save the element. ([#16205](https://github.com/craftcms/cms/pull/16205))
- Fixed a bug where pagination wasn’t working properly on the Entry Types index page when searching. ([#16204](https://github.com/craftcms/cms/issues/16204))
- Fixed an error that could occur when saving an element with an invalid Link field value. ([#16212](https://github.com/craftcms/cms/issues/16212))
Expand Down
19 changes: 1 addition & 18 deletions src/templates/_elements/sources.twig
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
{% apply spaceless %}
{% set keyPrefix = keyPrefix ?? '' %}
{% set isTopLevel = not keyPrefix %}

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

{% if viewModes is not defined %}
{% if elementInstance is not defined %}
{% set elementInstance = craft.app.elements.createElement(elementType) %}
{% endif %}
{% set viewModes = elementInstance.indexViewModes() %}
{% endif %}
{% hook 'cp.elements.sources' %}

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

{% set elementInstance = craft.app.elements.createElement(elementType) %}
{% set context = context is defined ? context : 'index' %}
{% set isAdministrative = context in ['index', 'embedded-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 isAdministrative %}
<div>
Expand All @@ -27,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 ?? '' %}
{% set color = color is instance of ('craft\\enums\\Color') ? color.value : color %}
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' %}

{% if showSiteMenu %}
{% if selectableSites is not defined %}
Expand Down Expand Up @@ -90,8 +77,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
77 changes: 77 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 @@ -27,6 +28,7 @@
use craft\web\twig\SafeHtml;
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 @@ -363,6 +365,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, '_elementChipHtml']);
}

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

private function _prepareElementIndexVariables(array &$context): null
{
/** @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;
}

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

$context['context'] ??= 'index';
$context['isAdministrative'] = match ($context['context']) {
'index', 'embedded-index' => true,
default => false,
};
$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;
}

private function _prepareElementSourcesVariables(array &$context): null
{
/** @var class-string<ElementInterface> $elementType */
$elementType = $context['elementType'];

$context['keyPrefix'] ??= '';
$context['isTopLevel'] = $context['keyPrefix'] === '';

if ($context['isTopLevel']) {
$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);
}

$context['viewModes'] ??= $elementType::indexViewModes();

return null;
}

/**
* Renders an element’s chip HTML.
*
Expand Down

0 comments on commit d4ae5d8

Please sign in to comment.