From d08b8ed9b94fa8c93eed2b42b975af8c44279a00 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 10 Dec 2024 10:44:07 -0800 Subject: [PATCH] Autosuggest templates --- CHANGELOG-WIP.md | 4 +++- src/fieldlayoutelements/Template.php | 3 ++- src/templates/_includes/forms/autosuggest.twig | 10 +++++----- src/templates/_includes/forms/editableTable.twig | 2 +- src/templates/settings/email/_index.twig | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 6662234f0d0..53be156a029 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -33,6 +33,7 @@ - The Queue Manager utility now shows jobs’ class names. ([#16228](https://github.com/craftcms/cms/pull/16228)) - Improved the wording of field instance action labels. ([#16261](https://github.com/craftcms/cms/discussions/16261)) - Templates rendered for “Template” field layout UI elements can now call control panel template functions like `elementChip()` and `elementCard()`. ([#16267](https://github.com/craftcms/cms/issues/16267)) +- “Template” field layout UI elements now show suggestions for the Template input. - Improved the error output for nested elements when they can’t be resaved via `resave` commands. - `resave` commands’ `--drafts`, `--provisional-drafts`, and `--revisions` options can now be set to `null`, causing elements to be resaved regardless of whether they’re drafts/provisional drafts/revisions. @@ -82,8 +83,9 @@ - `craft\services\Revisions::createRevision()` no longer creates the revision if an `EVENT_BEFORE_CREATE_REVISION` event handler sets `$event->handled` to `true` and at least one revision already exists for the element. ([#16260](https://github.com/craftcms/cms/discussions/16260)) - Deprecated `craft\fields\Color::$presets`. ([#16249](https://github.com/craftcms/cms/pull/16249)) - Deprecated `craft\fields\Link::$showTargetField`. +- `_includes/forms/autosuggest.twig` now supports a `suggestTemplates` variable. - `_includes/forms/colorSelect.twig` now supports `options` and `withBlankOption` variables. -- `_includes/forms/selectize.twig` now supports a `color` property in option data, which can be set to a hex value or a color name. +- `_includes/forms/selectize.twig` now supports a `color` property in option data, which can be set to a hex value or a color name. - Sortable checkbox selects now always display the selected options first on initial render. ### System diff --git a/src/fieldlayoutelements/Template.php b/src/fieldlayoutelements/Template.php index 37dd320571c..4e63b93056b 100644 --- a/src/fieldlayoutelements/Template.php +++ b/src/fieldlayoutelements/Template.php @@ -103,13 +103,14 @@ public function hasSettings() */ protected function settingsHtml(): ?string { - return Cp::textFieldHtml([ + return Cp::autosuggestFieldHtml([ 'label' => Craft::t('app', 'Template'), 'instructions' => Craft::t('app', 'The path to a template file within your `templates/` folder.'), 'tip' => Craft::t('app', 'The template will be rendered with an `element` variable.'), 'class' => 'code', 'id' => 'template', 'name' => 'template', + 'suggestTemplates' => true, 'value' => $this->template, ]); } diff --git a/src/templates/_includes/forms/autosuggest.twig b/src/templates/_includes/forms/autosuggest.twig index 320c8fe64f5..6e795cdfd32 100644 --- a/src/templates/_includes/forms/autosuggest.twig +++ b/src/templates/_includes/forms/autosuggest.twig @@ -1,11 +1,11 @@ {% do view.registerAssetBundle("craft\\web\\assets\\vue\\VueAsset") %} -{% if suggestEnvVars ?? false %} - {% set suggestions = (suggestions ?? [])|merge(craft.cp.getEnvSuggestions( +{% set suggestions = (suggestions ?? []) + |merge((suggestTemplates ?? false) ? craft.cp.getTemplateSuggestions() : []) + |merge((suggestEnvVars ?? false) ? craft.cp.getEnvSuggestions( suggestAliases ?? false, suggestionFilter ?? null - )) %} -{% endif %} + ) : []) %} {%- set id = id ?? "autosuggest#{random()}" %} {%- set containerId = "#{id}-container" %} @@ -50,7 +50,7 @@ new Vue({ query: (value ?? '')|lower, selected: '', filteredOptions: [], - suggestions: suggestions ?? [], + suggestions, id: autosuggestId, inputProps: { class: class|join(' '), diff --git a/src/templates/_includes/forms/editableTable.twig b/src/templates/_includes/forms/editableTable.twig index 7e9d572b401..25042aa7be0 100644 --- a/src/templates/_includes/forms/editableTable.twig +++ b/src/templates/_includes/forms/editableTable.twig @@ -202,7 +202,7 @@ {%- case 'autosuggest' or 'template' -%} {% include "_includes/forms/autosuggest" with { name: cellName, - suggestions: col.type == 'template' ? craft.cp.getTemplateSuggestions() : [], + suggestTemplates: col.type == 'template', suggestEnvVars: col.suggestEnvVars ?? false, suggestAliases: col.suggestAliases ?? false, value: value, diff --git a/src/templates/settings/email/_index.twig b/src/templates/settings/email/_index.twig index 1c7e242c03e..ce118444629 100644 --- a/src/templates/settings/email/_index.twig +++ b/src/templates/settings/email/_index.twig @@ -91,7 +91,7 @@ instructions: "The template Craft CMS will use for HTML emails"|t('app'), id: 'template', name: 'template', - suggestions: craft.cp.getTemplateSuggestions(), + suggestTemplates: true, suggestEnvVars: true, value: settings.template, errors: (freshSettings ? null : settings.getErrors('template'))