Skip to content

Commit

Permalink
OP-550 - Admin panel forms fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JanPalen committed Oct 9, 2024
1 parent 814841e commit 87c52cd
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/Form/Extension/ProductTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace BitBag\SyliusProductBundlePlugin\Form\Extension;

use BitBag\SyliusProductBundlePlugin\Form\Type\ProductBundleType;
use Sylius\Bundle\ProductBundle\Form\Type\ProductType;
use Sylius\Bundle\AdminBundle\Form\Type\ProductType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\Valid;
Expand Down
11 changes: 11 additions & 0 deletions src/Form/Type/ProductBundleItemType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceAutocompleteChoiceType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\UX\Autocomplete\Form\AsEntityAutocompleteField;
use Symfony\UX\Autocomplete\Form\BaseEntityAutocompleteType;

#[AsEntityAutocompleteField(
alias: 'sylius_admin_product_bundle_item',
route: 'sylius_admin_entity_autocomplete',
)]
final class ProductBundleItemType extends AbstractResourceType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -38,4 +44,9 @@ public function getBlockPrefix(): string
{
return 'bitbag_sylius_product_bundle_plugin_product_bundle_item';
}

public function getParent(): string
{
return BaseEntityAutocompleteType::class;
}
}
25 changes: 23 additions & 2 deletions src/Form/Type/ProductBundleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace BitBag\SyliusProductBundlePlugin\Form\Type;

use Sylius\Bundle\AdminBundle\Form\Type\AddButtonType;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\UX\LiveComponent\Form\Type\LiveCollectionType;

final class ProductBundleType extends AbstractResourceType
{
Expand All @@ -24,13 +26,32 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
->add('isPackedProduct', CheckboxType::class, [
'label' => 'bitbag_sylius_product_bundle.ui.is_packed_product',
])
->add('productBundleItems', CollectionType::class, [
// ->add('rules', LiveCollectionType::class, [
// 'entry_type' => AutomaticBlacklistingRuleType::class,
// 'allow_add' => true,
// 'allow_delete' => true,
// 'by_reference' => false,
// 'button_add_type' => AddButtonType::class,
// 'button_add_options' => [
// 'label' => 'sylius.ui.add_rule',
// 'types' => $this->rules,
// ],
// 'button_delete_options' => [
// 'label' => false,
// ],
// ])
->add('productBundleItems', LiveCollectionType::class, [
'entry_type' => ProductBundleItemType::class,
'entry_options' => ['label' => false],
// 'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => false,
'button_add_type' => AddButtonType::class,
// 'button_add_options' => [
// 'label' => 'sylius.ui.add_rule',
//// 'types' => $this->rules,
// ],
])
;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Menu/AdminProductFormMenuListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function addItems(ProductMenuBuilderEvent $event): void

$menu
->addChild('bundle')
->setAttribute('template', '@BitBagSyliusProductBundlePlugin/Admin/Product/Tab/_bundle.html.twig')
->setAttribute('template', '@BitBagSyliusProductBundlePlugin/Admin/product/form/side_navigation/bundle.html.twig')
->setLabel('bitbag_sylius_product_bundle.ui.bundle')
;
}
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imports:
- { resource: resources.yml }
- { resource: services.xml }
- {resource: "@BitBagSyliusProductBundlePlugin/Resources/config/twig_hooks/twig_hooks.yaml"}

framework:
messenger:
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/routing/admin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ bitbag_product_bundle_admin_product_create_bundle:
template: "@SyliusAdmin/shared/crud/create.html.twig"
redirect: sylius_admin_product_update
form:
type: Sylius\Bundle\AdminBundle\Form\Type\ProductType
options:
validation_groups:
- sylius
- bitbag_sylius_product_bundle
vars:
subheader: sylius.ui.manage_your_product_catalog
templates:
form: "@SyliusAdmin/Product/form.html.twig"
form: "@SyliusAdmin/product/form.html.twig"
route:
name: bitbag_product_bundle_admin_product_create_bundle

Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/services/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
<argument>%bitbag_sylius_product_bundle.model.product_bundle_item.class%</argument>
<argument>%bitbag_sylius_product_bundle.form.type.product_bundle_item.validation_groups%</argument>
<tag name="form.type" />
<tag name="ux.entity_autocomplete_field" />
</service>

<service id="bitbag_sylius_product_bundle.form.extension.type.product" class="BitBag\SyliusProductBundlePlugin\Form\Extension\ProductTypeExtension">
<tag name="form.type_extension" extended-type="Sylius\Bundle\ProductBundle\Form\Type\ProductType" />
<tag name="form.type_extension" extended-type="Sylius\Bundle\AdminBundle\Form\Type\ProductType" />
</service>
</services>
</container>
57 changes: 57 additions & 0 deletions src/Resources/config/twig_hooks/product/create.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
twig_hooks:
hooks:
'sylius_admin.product.create.content.form.side_navigation':
general:
template: '@SyliusAdmin/product/form/side_navigation/general.html.twig'
configuration:
active: true
channel_pricing:
template: '@SyliusAdmin/product/form/side_navigation/channel_pricing.html.twig'
taxes:
template: '@SyliusAdmin/product/form/side_navigation/taxes.html.twig'
shipping:
template: '@SyliusAdmin/product/form/side_navigation/shipping.html.twig'
inventory:
template: '@SyliusAdmin/product/form/side_navigation/inventory.html.twig'
translations:
template: '@SyliusAdmin/product/form/side_navigation/translations.html.twig'
taxonomy:
template: '@SyliusAdmin/product/form/side_navigation/taxonomy.html.twig'
attributes:
template: '@SyliusAdmin/product/form/side_navigation/attributes.html.twig'
associations:
template: '@SyliusAdmin/product/form/side_navigation/associations.html.twig'
media:
template: '@SyliusAdmin/product/form/side_navigation/media.html.twig'
bundle:
template: '@BitBagSyliusProductBundlePlugin/Admin/product/form/side_navigation/bundle.html.twig'

'sylius_admin.product.create.content.form.sections':
general:
template: '@SyliusAdmin/product/form/sections/general.html.twig'
configuration:
active: true
channel_pricing:
template: '@SyliusAdmin/product/form/sections/channel_pricing.html.twig'
taxes:
template: '@SyliusAdmin/product/form/sections/taxes.html.twig'
shipping:
template: '@SyliusAdmin/product/form/sections/shipping.html.twig'
inventory:
template: '@SyliusAdmin/product/form/sections/inventory.html.twig'
translations:
template: '@SyliusAdmin/product/form/sections/translations.html.twig'
taxonomy:
template: '@SyliusAdmin/product/form/sections/taxonomy.html.twig'
attributes:
template: '@SyliusAdmin/product/form/sections/attributes.html.twig'
associations:
template: '@SyliusAdmin/product/form/sections/associations.html.twig'
media:
template: '@SyliusAdmin/product/form/sections/media.html.twig'
bundle:
template: '@BitBagSyliusProductBundlePlugin/Admin/product/form/sections/bundle.html.twig'

'sylius_admin.product.create.content.form.sections.bundle':
item:
template: '@BitBagSyliusProductBundlePlugin/Admin/Form/productBundleItem.html.twig'
2 changes: 2 additions & 0 deletions src/Resources/config/twig_hooks/twig_hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
imports:
- { resource: "@BitBagSyliusProductBundlePlugin/Resources/config/twig_hooks/product/create.yaml" }
122 changes: 61 additions & 61 deletions src/Resources/views/Admin/Form/productBundleItem.html.twig
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
{% extends '@SyliusUi/Form/theme.html.twig' %}
{#{% extends '@SyliusUi/Form/theme.html.twig' %}#}

{% block collection_widget -%}
{% from '@SyliusResource/Macros/notification.html.twig' import error %}
{% import _self as self %}
{% set attr = attr|merge({'class': attr.class|default ~ ' controls collection-widget'}) %}
{#{% block collection_widget -%}#}
{# {% from '@SyliusResource/Macros/notification.html.twig' import error %}#}
{# {% import _self as self %}#}
{# {% set attr = attr|merge({'class': attr.class|default ~ ' controls collection-widget'}) %}#}

{% apply spaceless %}
<div data-form-type="collection" {{ block('widget_container_attributes') }}
{% if prototype is defined and allow_add %}
data-prototype='{{ self.collection_item(prototype, allow_delete, button_delete_label, prototype.vars.name)|e }}'
data-prototype-name='{{ prototype.vars.name }}'
{%- endif -%}
>
{{ error(form.vars.errors) }}
{# {% apply spaceless %}#}
{# <div data-form-type="collection" {{ block('widget_container_attributes') }}#}
{# {% if prototype is defined and allow_add %}#}
{# data-prototype='{{ self.collection_item(prototype, allow_delete, button_delete_label, prototype.vars.name)|e }}'#}
{# data-prototype-name='{{ prototype.vars.name }}'#}
{# {%- endif -%}#}
{# >#}
{# {{ error(form.vars.errors) }}#}

{% if prototypes|default is iterable %}
{% for key, subPrototype in prototypes %}
<input type="hidden" data-form-prototype="{{ key }}" value="{{ self.collection_item(subPrototype, allow_delete, button_delete_label, '__name__')|e }}" />
{% endfor %}
{% endif %}
{# {% if prototypes|default is iterable %}#}
{# {% for key, subPrototype in prototypes %}#}
{# <input type="hidden" data-form-prototype="{{ key }}" value="{{ self.collection_item(subPrototype, allow_delete, button_delete_label, '__name__')|e }}" />#}
{# {% endfor %}#}
{# {% endif %}#}

<table class="ui stackable celled table">
<thead>
<tr>
<th>{{ 'bitbag_sylius_product_bundle.ui.product_variant'|trans }}</th>
<th>{{ 'bitbag_sylius_product_bundle.ui.quantity'|trans }}</th>
<th>{{ 'bitbag_sylius_product_bundle.ui.delete'|trans }}</th>
</tr>
</thead>
<tbody data-form-collection="list">
{% for child in form %}
{{ self.collection_item(child, allow_delete, button_delete_label, loop.index0) }}
{% endfor %}
</tbody>
</table>
{# <table class="ui stackable celled table">#}
{# <thead>#}
{# <tr>#}
{# <th>{{ 'bitbag_sylius_product_bundle.ui.product_variant'|trans }}</th>#}
{# <th>{{ 'bitbag_sylius_product_bundle.ui.quantity'|trans }}</th>#}
{# <th>{{ 'bitbag_sylius_product_bundle.ui.delete'|trans }}</th>#}
{# </tr>#}
{# </thead>#}
{# <tbody data-form-collection="list">#}
{# {% for child in form %}#}
{# {{ self.collection_item(child, allow_delete, button_delete_label, loop.index0) }}#}
{# {% endfor %}#}
{# </tbody>#}
{# </table>#}

{% if prototype is defined and allow_add %}
<a href="#" class="ui labeled icon button" data-form-collection="add" id="bitbag_add_product_to_bundle_button">
<i class="plus square outline icon"></i>
{{ button_add_label|trans }}
</a>
{% endif %}
</div>
{% endapply %}
{%- endblock collection_widget %}
{# {% if prototype is defined and allow_add %}#}
{# <a href="#" class="ui labeled icon button" data-form-collection="add" id="bitbag_add_product_to_bundle_button">#}
{# <i class="plus square outline icon"></i>#}
{# {{ button_add_label|trans }}#}
{# </a>#}
{# {% endif %}#}
{# </div>#}
{# {% endapply %}#}
{#{%- endblock collection_widget %}#}

{% macro collection_item(form, allow_delete, button_delete_label, index) %}
{% apply spaceless %}
<tr data-form-collection="item" data-form-collection-index="{{ index }}">
<td>
<div id="add_product_to_bundle_autocomplete"> {{ form_row(form.productVariant, {'remote_url': path('bitbag_product_bundle_admin_ajax_product_variants_by_phrase'), 'remote_criteria_type': 'contains', 'remote_criteria_name': 'phrase', 'load_edit_url': path('bitbag_product_bundle_admin_ajax_product_variants_by_codes')}) }} </div>
</td>
<td>
{{ form_row(form.quantity) }}
</td>
{#{% macro collection_item(form, allow_delete, button_delete_label, index) %}#}
{# {% apply spaceless %}#}
{# <tr data-form-collection="item" data-form-collection-index="{{ index }}">#}
{# <td>#}
{# <div id="add_product_to_bundle_autocomplete"> {{ form_row(form.productVariant, {'remote_url': path('bitbag_product_bundle_admin_ajax_product_variants_by_phrase'), 'remote_criteria_type': 'contains', 'remote_criteria_name': 'phrase', 'load_edit_url': path('bitbag_product_bundle_admin_ajax_product_variants_by_codes')}) }} </div>#}
{# </td>#}
{# <td>#}
{# {{ form_row(form.quantity) }}#}
{# </td>#}

{% if allow_delete %}
<td>
<a href="#" data-form-collection="delete" class="ui red labeled icon button" style="margin-bottom: 1em;">
<i class="trash icon"></i>
{{ button_delete_label|trans }}
</a>
</td>
{% endif %}
</tr>
{% endapply %}
{% endmacro %}
{# {% if allow_delete %}#}
{# <td>#}
{# <a href="#" data-form-collection="delete" class="ui red labeled icon button" style="margin-bottom: 1em;">#}
{# <i class="trash icon"></i>#}
{# {{ button_delete_label|trans }}#}
{# </a>#}
{# </td>#}
{# {% endif %}#}
{# </tr>#}
{# {% endapply %}#}
{#{% endmacro %}#}
9 changes: 0 additions & 9 deletions src/Resources/views/Admin/Product/Tab/_bundle.html.twig

This file was deleted.

21 changes: 21 additions & 0 deletions src/Resources/views/Admin/product/form/sections/bundle.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{#{% form_theme form '@BitBagSyliusProductBundlePlugin/Admin/Form/productBundleItem.html.twig' %}#}
{% set form = hookable_metadata.context.form %}
{% set product = hookable_metadata.context.resource %}

<div class="tab pane " id="product-bundle" role="tabpanel" tabindex="0">
<div class="card mb-3">
<div class="card-header">
<h2 id="productBundle" class="card-title">
{{ 'bitbag_sylius_product_bundle.ui.bundle'|trans }}
</h2>
</div>
<div class="card-body">
{{ dump(form) }}
{{ form_row(form.productBundle.isPackedProduct) }}
{{ form_row(form.productBundle.productBundleItems) }}
{# {{ sonata_block_render_event('sylius.admin.product.' ~ action ~ '.tab_bundle', {'form': form }) }}#}
{% hook 'bundle' with { product } %}
</div>
</div>

</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<button
type="button"
class="list-group-item list-group-item-action {% if hookable_metadata.configuration.active|default(false) %}active{% endif %}"
data-bs-toggle="tab"
data-bs-target="#product-bundle"
role="tab"
{{ sylius_test_html_attribute('side-navigation-tab', 'bundle') }}
>
{{ 'bitbag_sylius_product_bundle.ui.bundle'|trans }}
</button>
4 changes: 4 additions & 0 deletions tests/Application/config/routes/sylius_admin.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
sylius_admin:
resource: "@SyliusAdminBundle/Resources/config/routing.yml"
prefix: /admin

live_component:
resource: "@LiveComponentBundle/config/routes.php"
prefix: /_components
4 changes: 4 additions & 0 deletions tests/Application/config/routes/sylius_shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ sylius_shop_default_locale:
methods: [GET]
defaults:
_controller: sylius.controller.shop.locale_switch::switchAction

live_component:
resource: "@LiveComponentBundle/config/routes.php"
prefix: /_components

0 comments on commit 87c52cd

Please sign in to comment.