From 8323f374a46c38b22cd21b5ddfe654266c4401aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 3 Aug 2022 20:28:27 +0200 Subject: [PATCH] Implement attachment CollectionType using stimulus --- .../attachmenttype_change_controller.js | 29 +++++ src/Form/AttachmentFormType.php | 12 +- src/Form/Type/StructuralEntityType.php | 4 +- templates/AdminPages/_attachments.html.twig | 116 +----------------- .../Form/extendedBootstrap4_layout.html.twig | 2 +- templates/Parts/edit/_attachments.html.twig | 111 +---------------- .../Parts/edit/edit_form_styles.html.twig | 70 +++++++++++ .../components/attachments.macro.html.twig | 20 +++ .../collection_type.macro.html.twig | 2 +- translations/messages.en.xlf | 6 + 10 files changed, 141 insertions(+), 231 deletions(-) create mode 100644 assets/controllers/elements/attachmenttype_change_controller.js create mode 100644 templates/components/attachments.macro.html.twig diff --git a/assets/controllers/elements/attachmenttype_change_controller.js b/assets/controllers/elements/attachmenttype_change_controller.js new file mode 100644 index 00000000..d96e70f3 --- /dev/null +++ b/assets/controllers/elements/attachmenttype_change_controller.js @@ -0,0 +1,29 @@ +import {Controller} from "@hotwired/stimulus"; + +/** + * This controller synchronizes the filetype filters of the file input type with our selected attachment type + */ +export default class extends Controller +{ + _selectInput; + _fileInput; + + connect() { + //Find the select input for our attachment form + this._selectInput = this.element.querySelector('select'); + //Find the file input for our attachment form + this._fileInput = this.element.querySelector('input[type="file"]'); + + this._selectInput.addEventListener('change', this.updateAllowedFiletypes.bind(this)); + + //Update file file on load + this.updateAllowedFiletypes(); + } + + updateAllowedFiletypes() { + let selected_option = this._selectInput.options[this._selectInput.selectedIndex]; + let filetype_filter = selected_option.dataset.filetype_filter; + //Apply filetype filter to file input + this._fileInput.setAttribute('accept', filetype_filter); + } +} \ No newline at end of file diff --git a/src/Form/AttachmentFormType.php b/src/Form/AttachmentFormType.php index 018337c9..1b332fbf 100644 --- a/src/Form/AttachmentFormType.php +++ b/src/Form/AttachmentFormType.php @@ -95,9 +95,6 @@ class AttachmentFormType extends AbstractType 'label' => 'attachment.edit.attachment_type', 'class' => AttachmentType::class, 'disable_not_selectable' => true, - 'attr' => [ - 'class' => 'attachment_type_selector', - ], ]); $builder->add('showInTable', CheckboxType::class, [ @@ -139,9 +136,9 @@ class AttachmentFormType extends AbstractType 'mapped' => false, 'required' => false, 'attr' => [ - 'class' => 'file', + /*'class' => 'file', 'data-show-preview' => 'false', - 'data-show-upload' => 'false', + 'data-show-upload' => 'false',*/ ], 'constraints' => [ //new AllowedFileExtension(), @@ -186,4 +183,9 @@ class AttachmentFormType extends AbstractType 'allow_builtins' => true, ]); } + + public function getBlockPrefix() + { + return 'attachment'; + } } diff --git a/src/Form/Type/StructuralEntityType.php b/src/Form/Type/StructuralEntityType.php index b5cd793e..60911e4d 100644 --- a/src/Form/Type/StructuralEntityType.php +++ b/src/Form/Type/StructuralEntityType.php @@ -114,9 +114,11 @@ class StructuralEntityType extends AbstractType $resolver->setDefault('empty_message', null); + $resolver->setDefault('controller', 'elements--selectpicker'); + $resolver->setDefault('attr', static function (Options $options) { $tmp = [ - 'data-controller' => 'elements--selectpicker', + 'data-controller' => $options['controller'], 'data-live-search' => true, 'title' => 'selectpicker.nothing_selected', ]; diff --git a/templates/AdminPages/_attachments.html.twig b/templates/AdminPages/_attachments.html.twig index 95ab8a10..f14b02d4 100644 --- a/templates/AdminPages/_attachments.html.twig +++ b/templates/AdminPages/_attachments.html.twig @@ -1,115 +1,3 @@ -{% set delete_btn %} - -{% endset %} +{% import "components/attachments.macro.html.twig" as attachments %} -{#{{ form_row(form.master_picture_attachment) }} #} - - - - {% for attachment in form.attachments %} - - - - {% endfor %} - -
-
- {{ form_widget(attachment) }} -
-
- {{ delete_btn }} - - {% set attach = attachment.vars.value %} - {# @var attach \App\Entity\Attachments\Attachment #} - - - {% if attachment_manager.fileExisting(attach) %} - {% if not attach.external %} -

-
- - {{ attach.filename }} - -
- - {{ attachment_manager.humanFileSize(attach) }} - -
- {% else %} -

-
- - {% trans %}attachment.external{% endtrans %} - -
- {% endif %} - {% if attach.secure and not is_granted('show_private', attach) %} - {# Leave blank #} - {% elseif attach.picture %} - - {% trans %}attachment.preview.alt{% endtrans %} - - {% else %} - {% trans %}attachment.view{% endtrans %} - {% endif %} - {% else %} -

-
- - {% trans %}attachment.file_not_found{% endtrans %} - -
- {% endif %} - - {% if attach.secure %} -
- - {% trans %}attachment.secure{% endtrans %} - -
- {% endif %} -
-
- - - - \ No newline at end of file +{{ attachments.attachment_edit_list(form.attachments) }} \ No newline at end of file diff --git a/templates/Form/extendedBootstrap4_layout.html.twig b/templates/Form/extendedBootstrap4_layout.html.twig index aec3a427..dbac4b10 100644 --- a/templates/Form/extendedBootstrap4_layout.html.twig +++ b/templates/Form/extendedBootstrap4_layout.html.twig @@ -115,7 +115,7 @@ {%- block choice_widget_collapsed -%} {# Only add the BS5 form-select class if we dont use bootstrap-selectpicker #} - {% if attr["data-controller"] is defined and attr["data-controller"] != "elements--selectpicker" %} + {% if attr["data-controller"] is defined and attr["data-controller"] not in ["elements--selectpicker"] %} {%- set attr = attr|merge({class: (attr.class|default('') ~ ' form-select')|trim}) -%} {% else %} {# If it is an selectpicker add form-control class to fill whole width #} diff --git a/templates/Parts/edit/_attachments.html.twig b/templates/Parts/edit/_attachments.html.twig index 5c7588c2..ccd0c3eb 100644 --- a/templates/Parts/edit/_attachments.html.twig +++ b/templates/Parts/edit/_attachments.html.twig @@ -1,112 +1,5 @@ -{% set delete_btn %} - -{% endset %} +{% import "components/attachments.macro.html.twig" as attachments %} {{ form_row(form.master_picture_attachment) }} - - - {% for attachment in form.attachments %} - - - - - {% endfor %} - -
- {{ form_widget(attachment) }} - - {{ delete_btn }} - - {% set attach = attachment.vars.value %} - - {% if attachment_manager.fileExisting(attach) %} - {% if not attach.external %} -

-
- - {{ attach.filename }} - -
- - {{ attachment_manager.humanFileSize(attach) }} - -
- {% else %} -

-
- - {% trans %}attachment.external{% endtrans %} - -
- {% endif %} - - {% if attach.secure and not is_granted('show_private', attach) %} - {# Leave blank #} - {% elseif attach.picture %} - - {% trans %}attachment.preview.alt{% endtrans %} - - {% else %} - {% trans %}attachment.view{% endtrans %} - {% endif %} - {% else %} -

-
- - {% trans %}attachment.file_not_found{% endtrans %} - -
- {% endif %} - - {% if attach.secure %} -
- - {% trans %}attachment.secure{% endtrans %} - -
- {% endif %} - -
- - - - \ No newline at end of file +{{ attachments.attachment_edit_list(form.attachments) }} \ No newline at end of file diff --git a/templates/Parts/edit/edit_form_styles.html.twig b/templates/Parts/edit/edit_form_styles.html.twig index 050470c1..cf15c809 100644 --- a/templates/Parts/edit/edit_form_styles.html.twig +++ b/templates/Parts/edit/edit_form_styles.html.twig @@ -101,4 +101,74 @@ {{ form_errors(form) }} +{% endblock %} + +{% block attachment_widget %} + {% import 'components/collection_type.macro.html.twig' as collection %} + + {% dump(form) %} + + + + {{ form_widget(form) }} + + + + + {% set attach = form.vars.value %} + + {% if attach is not null %} + {% if attachment_manager.fileExisting(attach) %} + {% if not attach.external %} +

+
+ + {{ attach.filename }} + +
+ + {{ attachment_manager.humanFileSize(attach) }} + +
+ {% else %} +

+
+ + {% trans %}attachment.external{% endtrans %} + +
+ {% endif %} + + {% if attach.secure and not is_granted('show_private', attach) %} + {# Leave blank #} + {% elseif attach.picture %} + + {% trans %}attachment.preview.alt{% endtrans %} + + {% else %} + {% trans %}attachment.view{% endtrans %} + {% endif %} + {% else %} +

+
+ + {% trans %}attachment.file_not_found{% endtrans %} + +
+ {% endif %} + + {% if attach.secure %} +
+ + {% trans %}attachment.secure{% endtrans %} + +
+ {% endif %} + {% endif %} + + + {% endblock %} \ No newline at end of file diff --git a/templates/components/attachments.macro.html.twig b/templates/components/attachments.macro.html.twig new file mode 100644 index 00000000..7194c5ad --- /dev/null +++ b/templates/components/attachments.macro.html.twig @@ -0,0 +1,20 @@ +{# Renders a editable list of all attachments. form is the Attachment CollectionType #} +{% macro attachment_edit_list(form, part_mode = false) %} + {% form_theme form with ['Parts/edit/edit_form_styles.html.twig'] %} + + {% import 'components/collection_type.macro.html.twig' as collection %} +
+ + + {% for attachment in form %} + {{ form_widget(attachment) }} + {% endfor %} + +
+ + +
+{% endmacro %} \ No newline at end of file diff --git a/templates/components/collection_type.macro.html.twig b/templates/components/collection_type.macro.html.twig index fa186b2e..c895987d 100644 --- a/templates/components/collection_type.macro.html.twig +++ b/templates/components/collection_type.macro.html.twig @@ -1,6 +1,6 @@ {% macro controller(form, deleteMessage) %} {{ stimulus_controller('elements/collection_type', { - 'deleteMessage': 'parameter.delete.confirm'|trans, + 'deleteMessage': deleteMessage|trans, 'prototype': form_widget(form.vars.prototype)|e('html_attr') }) }} {% endmacro %} diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 185a2097..04ccc941 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -9351,5 +9351,11 @@ Element 3 Actions finished successfully. + + + attachment.edit.delete.confirm + Do you really want to delete this attachment? + +