Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple node select #4

Open
stipic opened this issue Nov 21, 2022 · 2 comments
Open

Multiple node select #4

stipic opened this issue Nov 21, 2022 · 2 comments

Comments

@stipic
Copy link

stipic commented Nov 21, 2022

Hello, great bundle

I have a problem because I need a ManyToMany relationship, but only OneToOne works for me.

I am interested in whether there is a possibility to multiselect nodes?
If it doesn't exist, would you help me fork the package myself and try to repurpose it?

Thanks!

@NicolasPineau
Copy link

NicolasPineau commented Jan 12, 2023

Actually this should not need huge changes, at least the following to test :

  • change input type from radio to checkbox in tree.html.twig
  • something like that in app controller to check :
public function configureFields(string $pageName): iterable
{
  return [
    $pageName === 'index'
      ? AssociationField::new('field', 'Label')
      : TreeField::new('field', 'Label')
          ->setFormTypeOption('choice_label', 'name')
          ->setFormTypeOption('multiple', true)
  ];
}

I've tested on a ManyToMany relation and it seems to be working :)
@jodou92 I'd be glad to propose a PR to finalize and document this feature if you wish.

@NicolasPineau
Copy link

NicolasPineau commented Jan 12, 2023

Here's a diff :

diff --git a/README.md b/README.md
index 13ea4af..2393e30 100644
--- a/README.md
+++ b/README.md
@@ -192,6 +192,30 @@ class MediaCrudController extends AbstractCategorizedCrudController
     }
```
 
+If you wish to use the tree field with a ManyToMany relation, just set field attribute `multiple` to true :
+```php
+<?php
+// src/Controller/Admin/MediaCrudController.php
+namespace App\Controller\Admin;
+
+use Umanit\EasyAdminTreeBundle\Field\TreeField;
+
+class MediaCrudController extends AbstractCategorizedCrudController
+{
+
+    public function configureFields(string $pageName): iterable
+    {   
+        return [
+            // ...
+            $pageName === 'index'
+                ? AssociationField::new('categories', 'My categories')
+                : TreeField::new('categories', 'My categories')
+                    ->setFormTypeOption('multiple', true),
+            // ...
+        ];
+    }
+```
+
 ### Screenshots
 Tree structure on index page
 ![screenshot1](doc/screenshots/1.jpg "Content's entries as a tree structure")
diff --git a/Resources/views/form/themes/tree.html.twig b/Resources/views/form/themes/tree.html.twig
index 4da9d8f..4ceb8b7 100644
--- a/Resources/views/form/themes/tree.html.twig
+++ b/Resources/views/form/themes/tree.html.twig
@@ -1,6 +1,7 @@
 {% block tree_field_widget %}
   <ul class="umanit_easyadmintree_tree_field umanit_easyadmintree_tree_field-container col-md-6 col-xxl-5">
   {% set previous_level = 0 %}
+  {% set inputType = form.vars.multiple ? 'checkbox' : 'radio' %}
   {%- for child in form %}
     {% set level = child.vars.attr['data-level'] is defined ? child.vars.attr['data-level'] : 0 %}
 
@@ -12,7 +13,7 @@
 
     <li>
     <div class="umanit_easyadmintree_tree_field-item {% if child.vars.checked %}umanit_easyadmintree_tree_field-item--active{% endif %}">
-      <input type="radio" id="{{ child.vars.id }}" name="{{ child.vars.full_name }}" class="form-check-input" value="{% if child.vars.value is defined %}{{ child.vars.value }}{% endif %}" {% if child.vars.checked %} checked="checked"{% endif %} />
+      <input type="{{ inputType }}" id="{{ child.vars.id }}" name="{{ child.vars.full_name }}" class="form-check-input" value="{% if child.vars.value is defined %}{{ child.vars.value }}{% endif %}" {% if child.vars.checked %} checked="checked"{% endif %} />
       <label class="form-check-label umanit_easyadmintree_tree_field-item-label" for="{{ child.vars.id }}">
         {% if loop.first %}
           {{ 'umanit.easyadmin.tree.form-field.placeholder' | trans }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants