diff --git a/src/bundle/Resources/public/js/scripts/admin.contenttype.edit.js b/src/bundle/Resources/public/js/scripts/admin.contenttype.edit.js index 16c01cbb48..a88491860a 100644 --- a/src/bundle/Resources/public/js/scripts/admin.contenttype.edit.js +++ b/src/bundle/Resources/public/js/scripts/admin.contenttype.edit.js @@ -147,6 +147,11 @@ detail: { nodes: [fieldNode] }, }), ); + doc.body.dispatchEvent( + new CustomEvent('ibexa-autogenerate-identifier:init', { + detail: { fieldNode, shouldAutogenerateValue: true }, + }), + ); }; const insertFieldDefinitionNode = (fieldNode, { targetContainer, draggedItemPosition }) => { const fieldDefinitionNode = createFieldDefinitionNode(fieldNode, { targetContainer, draggedItemPosition }); diff --git a/src/bundle/Resources/public/js/scripts/autogenerate.identifier.js b/src/bundle/Resources/public/js/scripts/autogenerate.identifier.js index 40339cf92b..d87895c89b 100644 --- a/src/bundle/Resources/public/js/scripts/autogenerate.identifier.js +++ b/src/bundle/Resources/public/js/scripts/autogenerate.identifier.js @@ -1,14 +1,32 @@ (function (doc, ibexa) { const sourceInputs = doc.querySelectorAll('[data-autogenerate-identifier-target-selector]'); - sourceInputs.forEach((sourceInput) => { - const { autogenerateIdentifierTargetSelector } = sourceInput.dataset; - const targetInput = doc.querySelector(autogenerateIdentifierTargetSelector); - const identifierAutogenerator = new ibexa.core.SlugValueInputAutogenerator({ - sourceInput, - targetInput, + const initAutogenerator = (elements, shouldAutogenerateValue) => { + elements.forEach((sourceInput) => { + const { autogenerateIdentifierTargetSelector } = sourceInput.dataset; + const targetInput = doc.querySelector(autogenerateIdentifierTargetSelector); + const identifierAutogenerator = new ibexa.core.SlugValueInputAutogenerator({ + sourceInput, + targetInput, + shouldAutogenerateValue: shouldAutogenerateValue || !targetInput.value, + }); + + identifierAutogenerator.init(); }); + }; + const attachListeners = () => { + doc.body.addEventListener( + 'ibexa-autogenerate-identifier:init', + (event) => { + const { fieldNode, shouldAutogenerateValue } = event.detail; + const sourceFields = fieldNode.querySelectorAll('[data-autogenerate-identifier-target-selector]'); + + initAutogenerator(sourceFields, shouldAutogenerateValue); + }, + false, + ); + }; - identifierAutogenerator.init(); - }); + initAutogenerator(sourceInputs); + attachListeners(); })(document, window.ibexa); diff --git a/src/bundle/Resources/public/js/scripts/core/slug.value.input.autogenerator.js b/src/bundle/Resources/public/js/scripts/core/slug.value.input.autogenerator.js index 6ad399febb..5026768d41 100644 --- a/src/bundle/Resources/public/js/scripts/core/slug.value.input.autogenerator.js +++ b/src/bundle/Resources/public/js/scripts/core/slug.value.input.autogenerator.js @@ -4,7 +4,7 @@ this.sourceInput = config.sourceInput; this.targetInput = config.targetInput; this.whitespaceTextReplacer = config.whitespaceTextReplacer || '_'; - this.shouldAutogenerateValue = !this.targetInput.value; + this.shouldAutogenerateValue = config.shouldAutogenerateValue || !this.targetInput.value; } slugify(text) { diff --git a/src/bundle/Resources/views/themes/admin/content_type/create.html.twig b/src/bundle/Resources/views/themes/admin/content_type/create.html.twig index 24ec499a88..dd98a3e6eb 100644 --- a/src/bundle/Resources/views/themes/admin/content_type/create.html.twig +++ b/src/bundle/Resources/views/themes/admin/content_type/create.html.twig @@ -51,8 +51,11 @@ {% block left_column %} {% set form_field_options = { row_attr: { class: 'ibexa-form-field' } } %} - {{ form_row(form.name, form_field_options) }} - {{ form_row(form.identifier, form_field_options) }} + {{ form_row(form.name, form_field_options|merge({ + attr: { 'data-autogenerate-identifier-target-selector': '#ibexa-edit-content-type-identifier' } } + )) }} + {{ form_row(form.identifier, form_field_options|merge({ + id: 'ibexa-edit-content-type-identifier' } ) ) }} {{ form_row(form.description, form_field_options) }} {{ form_row(form.nameSchema, form_field_options) }} {{ form_row(form.urlAliasSchema, form_field_options) }} diff --git a/src/bundle/Resources/views/themes/admin/content_type/edit.html.twig b/src/bundle/Resources/views/themes/admin/content_type/edit.html.twig index 6621787597..ee1c92af27 100644 --- a/src/bundle/Resources/views/themes/admin/content_type/edit.html.twig +++ b/src/bundle/Resources/views/themes/admin/content_type/edit.html.twig @@ -57,8 +57,11 @@ {% block left_column %} {% set form_field_options = { row_attr: { class: 'ibexa-form-field' } } %} - {{ form_row(form.name, form_field_options) }} - {{ form_row(form.identifier, form_field_options) }} + {{ form_row(form.name, form_field_options|merge({ + attr: { 'data-autogenerate-identifier-target-selector': '#ibexa-edit-content-type-identifier' } } + )) }} + {{ form_row(form.identifier, form_field_options|merge({ + id: 'ibexa-edit-content-type-identifier' } ) ) }} {{ form_row(form.description, form_field_options) }} {{ form_row(form.nameSchema, form_field_options) }} {{ form_row(form.urlAliasSchema, form_field_options) }} diff --git a/src/bundle/Resources/views/themes/admin/content_type/field_definition.html.twig b/src/bundle/Resources/views/themes/admin/content_type/field_definition.html.twig index 8eda7f03bf..eebac98857 100644 --- a/src/bundle/Resources/views/themes/admin/content_type/field_definition.html.twig +++ b/src/bundle/Resources/views/themes/admin/content_type/field_definition.html.twig @@ -28,8 +28,12 @@ {% if field_definition.enabled is defined %} {{ form_row(field_definition.enabled) }} {% endif %} - {{ form_row(field_definition.name) }} - {{ form_row(field_definition.identifier) }} + {{ form_row(field_definition.name, { + attr: { 'data-autogenerate-identifier-target-selector': '#ibexa-ct-identifier-' ~ field_definition.vars.value.identifier } + }) }} + {{ form_row(field_definition.identifier, { + id: 'ibexa-ct-identifier-' ~ field_definition.vars.value.identifier + }) }} {{ form_row(field_definition.description) }} {{ form_row(field_definition.isRequired) }} {{ form_row(field_definition.isSearchable) }} diff --git a/src/bundle/Resources/views/themes/admin/content_type/part/field_definition_form.html.twig b/src/bundle/Resources/views/themes/admin/content_type/part/field_definition_form.html.twig index 68c6c48860..ac31c17a8a 100644 --- a/src/bundle/Resources/views/themes/admin/content_type/part/field_definition_form.html.twig +++ b/src/bundle/Resources/views/themes/admin/content_type/part/field_definition_form.html.twig @@ -29,8 +29,12 @@ } -%} {%- block body_content -%}
- {{ form_row(form.name) }} - {{ form_row(form.identifier) }} + {{ form_row(form.name, { + attr: { 'data-autogenerate-identifier-target-selector': '#ibexa-ct-identifier-' ~ form.identifier.vars.value } + }) }} + {{ form_row(form.identifier, { + id: 'ibexa-ct-identifier-' ~ form.identifier.vars.value + }) }} {{ form_row(form.description) }} {{ form_row(form.isRequired) }} {{ form_row(form.isSearchable) }}