From 4674631114063e1b65970da7a473f41560b79d08 Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 1 Aug 2024 11:11:59 +0200 Subject: [PATCH] OP-463: Block templates --- src/Form/Type/BlockType.php | 4 ++++ .../TemplateBlockAutocompleteChoiceType.php | 19 +++++++++++++++++ src/Form/Type/TemplateType.php | 1 + .../assets/admin/js/bitbag/bitbag-template.js | 14 ++++++------- .../config/routing/admin/template.yml | 21 ++++++++++++++++--- .../views/Block/Crud/_form.html.twig | 2 ++ src/Resources/views/Form/theme.html.twig | 6 +++++- src/Resources/views/Page/Crud/_form.html.twig | 14 +------------ src/Resources/views/Template/form.html.twig | 13 ++++++++++++ 9 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 src/Form/Type/TemplateBlockAutocompleteChoiceType.php create mode 100644 src/Resources/views/Template/form.html.twig diff --git a/src/Form/Type/BlockType.php b/src/Form/Type/BlockType.php index d2472dc3..6713a45c 100755 --- a/src/Form/Type/BlockType.php +++ b/src/Form/Type/BlockType.php @@ -72,6 +72,10 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'multiple' => true, 'help' => 'bitbag_sylius_cms_plugin.ui.display_for_taxons.help', ]) + ->add('template', TemplateBlockAutocompleteChoiceType::class, [ + 'label' => false, + 'mapped' => false, + ]) ; } diff --git a/src/Form/Type/TemplateBlockAutocompleteChoiceType.php b/src/Form/Type/TemplateBlockAutocompleteChoiceType.php new file mode 100644 index 00000000..c0737319 --- /dev/null +++ b/src/Form/Type/TemplateBlockAutocompleteChoiceType.php @@ -0,0 +1,19 @@ + 'bitbag_sylius_cms_plugin.ui.type', 'choices' => [ 'bitbag_sylius_cms_plugin.ui.page' => 'page', + 'bitbag_sylius_cms_plugin.ui.block' => 'block', ], ]) ->add('contentElements', CollectionType::class, [ diff --git a/src/Resources/assets/admin/js/bitbag/bitbag-template.js b/src/Resources/assets/admin/js/bitbag/bitbag-template.js index aeea6ac5..6ca7984b 100644 --- a/src/Resources/assets/admin/js/bitbag/bitbag-template.js +++ b/src/Resources/assets/admin/js/bitbag/bitbag-template.js @@ -9,19 +9,20 @@ export class HandleTemplate { $(document).ready(() => { const cmsLoadTemplate = $('[data-bb-cms-load-template]'); const cmsPageTemplate = $('#bitbag_sylius_cms_plugin_page_template'); + const cmsBlockTemplate = $('#bitbag_sylius_cms_plugin_block_template'); cmsLoadTemplate.on('click', function (e) { e.preventDefault(); - if (!cmsPageTemplate.val()) { + if (!cmsPageTemplate.val() && !cmsBlockTemplate.val()) { return; } $('#load-template-confirmation-modal').modal('show'); }); - $('#load-template-confirmation-button').on('click', function (e) { - const templateId = cmsPageTemplate.val(); + $('#load-template-confirmation-button').on('click', function () { + const templateId = cmsPageTemplate.val() ?? cmsBlockTemplate.val(); if (!templateId) { return; } @@ -34,9 +35,6 @@ export class HandleTemplate { $.ajax({ url: endpointUrl, type: 'GET', - headers: { - 'X-Requested-With': 'XMLHttpRequest' - }, success: function(data) { if (data.status === 'success') { $('[id^="bitbag_sylius_cms_plugin_"][id$="contentElements"]') @@ -47,7 +45,9 @@ export class HandleTemplate { $('[data-form-collection="add"]').trigger('click'); }); - const elements = $('[id^="bitbag_sylius_cms_plugin_page_contentElements_"][id$="_type"]'); + const elements = $('[id^="bitbag_sylius_cms_plugin_"][id*="_contentElements_"][id$="_type"]').filter(function() { + return /_page_|_block_/.test(this.id); + }); $.each(data.content, function (index, element) { elements.eq(index).val(element.type); diff --git a/src/Resources/config/routing/admin/template.yml b/src/Resources/config/routing/admin/template.yml index 129cd1bb..9013de97 100755 --- a/src/Resources/config/routing/admin/template.yml +++ b/src/Resources/config/routing/admin/template.yml @@ -16,7 +16,7 @@ bitbag_sylius_cms_plugin_admin_template: type: sylius.resource bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase: - path: /ajax/templates/search + path: /ajax/templates/page/search methods: [GET] defaults: _format: json @@ -30,7 +30,22 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase: phrase: $phrase type: page -bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id: +bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase: + path: /ajax/templates/block/search + methods: [GET] + defaults: + _format: json + _controller: bitbag_sylius_cms_plugin.controller.template::indexAction + _sylius: + serialization_groups: [Autocomplete] + permission: true + repository: + method: findTemplatesByNamePart + arguments: + phrase: $phrase + type: block + +bitbag_sylius_cms_plugin_admin_ajax_template_by_id: path: /ajax/templates/id methods: [GET] defaults: @@ -43,7 +58,7 @@ bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id: method: find arguments: [id: $id] -bitbag_sylius_cms_plugin_admin_ajax_template_page_content_by_id: +bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id: path: /ajax/templates/content/{id} methods: [GET] defaults: diff --git a/src/Resources/views/Block/Crud/_form.html.twig b/src/Resources/views/Block/Crud/_form.html.twig index d53d42d7..e2905386 100755 --- a/src/Resources/views/Block/Crud/_form.html.twig +++ b/src/Resources/views/Block/Crud/_form.html.twig @@ -1,5 +1,6 @@ {% from '@BitBagSyliusCmsPlugin/Macro/translationForm.html.twig' import translationForm %} {% form_theme form '@BitBagSyliusCmsPlugin/Form/theme.html.twig' %} +{% include '@BitBagSyliusCmsPlugin/Modal/_loadTemplateConfirmation.html.twig' %}
@@ -22,6 +23,7 @@

{{ 'bitbag_sylius_cms_plugin.ui.content_elements.title'|trans }}

+ {% include '@BitBagSyliusCmsPlugin/Template/form.html.twig' with {ajax_url: path('bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id', {'id': 'REPLACE_ID'}) } %}
{{ form_row(form.contentElements) }} diff --git a/src/Resources/views/Form/theme.html.twig b/src/Resources/views/Form/theme.html.twig index 4f31f175..1a33ad39 100755 --- a/src/Resources/views/Form/theme.html.twig +++ b/src/Resources/views/Form/theme.html.twig @@ -13,7 +13,11 @@ {% endblock %} {% block bitbag_template_page_autocomplete_choice_row %} - {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_id')}) }} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_page_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_by_id')}) }} +{% endblock %} + +{% block bitbag_template_block_autocomplete_choice_row %} + {{ form_row(form, {'remote_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_block_by_name_phrase'), 'load_edit_url': path('bitbag_sylius_cms_plugin_admin_ajax_template_by_id')}) }} {% endblock %} {% block bitbag_media_autocomplete_choice_row %} diff --git a/src/Resources/views/Page/Crud/_form.html.twig b/src/Resources/views/Page/Crud/_form.html.twig index cc75f04c..97fb6a55 100755 --- a/src/Resources/views/Page/Crud/_form.html.twig +++ b/src/Resources/views/Page/Crud/_form.html.twig @@ -24,19 +24,7 @@

{{ 'bitbag_sylius_cms_plugin.ui.content_elements.title'|trans }}

-
-
{{ 'bitbag_sylius_cms_plugin.ui.use_page_template'|trans }}
-
-
- {{ form_row(form.template) }} -
- -
-
+ {% include '@BitBagSyliusCmsPlugin/Template/form.html.twig' with {ajax_url: path('bitbag_sylius_cms_plugin_admin_ajax_template_content_by_id', {'id': 'REPLACE_ID'}) } %}
{{ form_row(form.contentElements) }} diff --git a/src/Resources/views/Template/form.html.twig b/src/Resources/views/Template/form.html.twig new file mode 100644 index 00000000..c53272e2 --- /dev/null +++ b/src/Resources/views/Template/form.html.twig @@ -0,0 +1,13 @@ +
+
{{ 'bitbag_sylius_cms_plugin.ui.use_page_template'|trans }}
+
+
+ {{ form_row(form.template) }} +
+ +
+