The easiest way to modify the field editor for a specific field(s) is through the Field Editor cascade (Control Panel → Info → Advanced → Cascades → Field Editor tab). Here’s a working package; I’ll walk you through the steps below. duplicate field values.json (1.1 KB)
First, add a new config tiddler whose text field contains filter that captures both your chosen field names and the template that should be used to display them. In my demo above, I called this $:/config/FieldEditorFilters/duplicate
.
caption +[match<currentField>] :then[[$:/my/ui/EditTemplate/fieldEditor/duplicate]]
<!-- ^ as many space-separated field names as you want ^ your modified display template for these field names -->
We’ll need to make sure this filter appears before $:/config/FieldEditorFilters/default
in the cascade, so either drag your new filter tiddler before $:/config/FieldEditorFilters/default
in the $:/tags/FieldEditorFilter
tag-pill or give it the field value list-before: $:/config/FieldEditorFilters/default
.
To make the display template $:/my/ui/EditTemplate/fieldEditor/duplicate
, I started out by cloning $:/core/ui/EditTemplate/fieldEditor/default
and modified it as follows:
\function currentValue() [<storyTiddler>get<currentField>]
<$edit-text
tiddler=<<currentTiddler>>
field=<<currentField>>
tag="input"
default=""
class="tc-edit-texteditor tc-edit-fieldeditor"
placeholder={{$:/language/EditTemplate/Fields/Add/Value/Placeholder}}
tabindex={{$:/config/EditTabIndex}}
cancelPopups="yes"
/>
<% if [has<currentField>] -[<currentTiddler>] -[{!!draft.of}] :filter[get<currentField>match<currentValue>] +[format:titlelist[]join[ ]] %>
<small>
"<<currentTiddler>>" shares its <<currentField>> value with <$list filter="[enlist<condition>]" join=", "><$link/></$list>
</small>
<% endif %>
I added everything after the $edit-text
widget.
- The
$edit-text
duplicates the default field editor.
- The conditional
<% if ... %>
displays its contents only if there are one or more tiddlers (not counting the current draft or the tiddler it belongs to) that have an identical value for the current field.
- If you want to check for duplicates in shadows as well as standard tiddlers, replace
[has<currentField>]
with [all[shadows+tiddlers]has<currentField>]
.
-
+[format:titlelist[]join[ ]]
stores any resulting titles as a single space-separated string, accessible inside the conditional as <<condition>>
.
-
<$list filter="[enlist<condition>]" join=", "><$link/></$list>
converts the title list stored in <<condition>>
to a comma-separated list of links to the relevant titles.
- You can modify the styling of this warning text however you want — just make sure it all stays inside the conditional.
Alternately, if you want all user-added fields to warn for duplicate values, you can skip the first step (where we created $:/config/FieldEditorFilters/duplicate
) and simply replace the contents of $:/config/FieldEditorFilters/default
with
caption +[match<currentField>] :then[[$:/my/ui/EditTemplate/fieldEditor/duplicate]]
Or go a step further and overwrite the contents of $:/core/ui/EditTemplate/fieldEditor/default
itself with the modified template above.
Since our new field template deviates from the standard field editor only when duplicate field values exist, you could theoretically make it your new global default. But you may not want these warnings for all your fields… and since the filter used in the conditional is checking for matching field values on all your tiddlers, it may be a little slow in a wiki with lots of tiddlers/lots of fields.