Fixes #20551: Support quick-add form prefix in automatic slug generation

The slug generation logic in `reslug.ts` looks for form fields using hard-coded ID selectors like `#id_slug` and `#id_name`. In quick-add modals, Django applies a `quickadd` prefix to form fields (introduced in #20542), resulting in IDs like `#id_quickadd-slug` and `#id_quickadd-name`. The logic couldn't find these prefixed fields, so automatic slug generation failed silently in quick-add modals. This fix updates the field selectors to try both unprefixed and prefixed patterns using the nullish coalescing operator (`??`), checking for the standard field ID first and falling back to the quickadd-prefixed ID if the standard one isn't found.
This commit is contained in:
Jason Novinger
2025-10-19 16:37:25 -05:00
parent a1aaf465ac
commit 4e5db958c4
3 changed files with 13 additions and 6 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -23,10 +23,17 @@ export function initReslug(): void {
for (const slugButton of getElements<HTMLButtonElement>('button#reslug')) {
const form = slugButton.form;
if (form == null) continue;
const slugField = form.querySelector('#id_slug') as HTMLInputElement;
// Try without prefix first, fallback to quickadd prefix for quick-add modals
const slugField = (form.querySelector('#id_slug') ??
form.querySelector('#id_quickadd-slug')) as HTMLInputElement;
if (slugField == null) continue;
const sourceId = slugField.getAttribute('slug-source');
const sourceField = form.querySelector(`#id_${sourceId}`) as HTMLInputElement;
// Try both patterns for source field as well
const sourceField = (form.querySelector(`#id_${sourceId}`) ??
form.querySelector(`#id_quickadd-${sourceId}`)) as HTMLInputElement;
const slugLengthAttr = slugField.getAttribute('maxlength');
let slugLength = 50;